GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-103857: Deprecate utcnow and utcfromtimestamp by pganssle · Pull Request #103858 · python/cpython

Conversation

@pganssle

@pganssle pganssle commented Apr 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Per the reasoning in #103857.

To do:

  • Add tests
  • Clean up internal uses of utcnow and utcfromtimestamp
  • Issue: Deprecate utcnow and utcfromtimestamp #103857

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pganssle
pganssle requested a review from abalkin as a code owner April 25, 2023 23:04
@bedevere-bot bedevere-bot mentioned this pull request Apr 25, 2023
Deprecate utcnow and utcfromtimestamp #103857
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Apr 25, 2023
@pganssle
pganssle requested review from a team, berkerpeksag and erlend-aasland as code owners April 25, 2023 23:59
erlend-aasland
erlend-aasland approved these changes Apr 26, 2023
View reviewed changes

@erlend-aasland erlend-aasland left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (fixing the remaining test failures seems straight-forward.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/datetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/datetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Apr 26, 2023
@pganssle
pganssle force-pushed the deprecate_utcnow branch 2 times, most recently from 6294fc7 to 66a82a0 Compare April 26, 2023 16:00
ambv
ambv reviewed Apr 26, 2023
View reviewed changes

@ambv ambv left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, my nit is about the wordiness of the warnings.

Outside of that, there's the question of mixing tz-aware and tz-naive datetimes when it comes to comparisons and arithmetic operations. It might be somewhat disruptive for users to replace one with the other to discover TypeErrors in their code due to mixing naive and tz-aware datetimes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/datetime.py
Comment on lines +1805 to +1810
warnings.warn("datetime.utcfromtimestamp() is deprecated and scheduled "
"for removal in a future version. It is instead "
"recommended that users use timezone-aware objects to "
"represent datetimes in UTC. To get such an object from "
"a timestamp, use datetime.fromtimestamp(t, datetime.UTC).",
DeprecationWarning,
stacklevel=2)

@ambv ambv Apr 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this a little wordy for a warning, and tentative. The deprecated function will be removed in the future so we are not recommending an alternative, we are telling people to use an alternative. How about this:

Suggested change
warnings.warn("datetime.utcfromtimestamp() is deprecated and scheduled "
"for removal in a future version. It is instead "
"recommended that users use timezone-aware objects to "
"represent datetimes in UTC. To get such an object from "
"a timestamp, use datetime.fromtimestamp(t, datetime.UTC).",
DeprecationWarning,
stacklevel=2)
warnings.warn("datetime.utcfromtimestamp() is deprecated and will be "
"removed in a future version. Use timezone-aware objects to "
"represent datetimes in UTC: "
"datetime.fromtimestamp(t, datetime.UTC)",
DeprecationWarning,
stacklevel=2)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@pganssle pganssle Apr 26, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think it was a bit wordy when I wrote it. My only reservation here is that users still can use naïve objects as they want, they just need to do datetime.now(UTC).replace(tzinfo=None).

I suppose this subtle distinction is probably going to be lost on most people, so I'll just go with your wording.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@erlend-aasland erlend-aasland Apr 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that Łukasz's suggestion is an improvement. I think it is ok to suggest the slightly disruptive change; we want people to use tz-aware objects1.

Footnotes

  1. Providing a drop-in replacement does not promote using tz-aware objects. ↩

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pganssle pganssle Apr 26, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that Łukasz's suggestion is an improvement. I think it is ok to suggest the slightly disruptive change; we want people to use tz-aware objects

I mean, in either case we're suggesting the change, the question is how best to communicate to users that the thing we're suggesting is not a drop-in replacement for the existing workflow. I tend to assume that when I see a deprecation warning with a code snippet in it, the code snippet is how you achieve the same thing in the new version. Ideally we'd make it clear that we are saying, "The thing you are doing now is not something you should continue doing, here's a new way to do it but you need to understand the implications of making this change."

That said, I'm not convinced that it's possible to convey this succinctly, so probably best to just leave it at Łukasz's wording.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Lib/datetime.py
Comment on lines +1824 to +1828
warnings.warn("datetime.utcnow() is deprecated and scheduled for "
"removal in a future version. It is instead recommended "
"that users use timezone-aware objects to represent "
"datetimes in UTC. To get such an object representing "
"the current time, use datetime.now(datetime.UTC).",
DeprecationWarning,
stacklevel=2)

@ambv ambv Apr 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Suggested change
warnings.warn("datetime.utcnow() is deprecated and scheduled for "
"removal in a future version. It is instead recommended "
"that users use timezone-aware objects to represent "
"datetimes in UTC. To get such an object representing "
"the current time, use datetime.now(datetime.UTC).",
DeprecationWarning,
stacklevel=2)
warnings.warn("datetime.utcnow() is deprecated and will be removed "
"in a future version. Use timezone-aware objects to "
"represent datetimes in UTC: datetime.now(datetime.UTC)",
DeprecationWarning,
stacklevel=2)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_datetimemodule.c
Comment on lines +5147 to +5153
PyErr_WarnEx(
PyExc_DeprecationWarning,
"datetime.utcnow() is deprecated and scheduled for removal in a future "
"version. It is instead recommended that users use timezone-aware "
"objects to represent datetimes in UTC. To get such an object "
"representing the current time, use datetime.now(datetime.UTC).",
2
);

@ambv ambv Apr 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here about replacing the warning with the shorter version.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_datetimemodule.c
Comment on lines +5191 to +5196
PyErr_WarnEx(
PyExc_DeprecationWarning,
"datetime.utcfromtimestamp() is deprecated and scheduled for removal "
"in a future version. It is instead recommended that users use "
"timezone-aware objects to represent datetimes in UTC. To get such an "
"object from a timestamp, use datetime.fromtimestamp(t, datetime.UTC).",
2
);

@ambv ambv Apr 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pganssle

pganssle commented Apr 26, 2023

Copy link
Copy Markdown
Member Author

Outside of that, there's the question of mixing tz-aware and tz-naive datetimes when it comes to comparisons and arithmetic operations. It might be somewhat disruptive for users to replace one with the other to discover TypeErrors in their code due to mixing naive and tz-aware datetimes.

Yes, this was somewhat hard to convey in the wording here. I am not trying to suggest that datetime.now(UTC) is a drop-in replacement for datetime.utcnow(). The drop-in replacement is datetime.now(UTC).replace(tzinfo=None). We're deprecating utcnow() because you should be using aware objects to represent datetimes that are actually aware, but it is a breaking change if you change your interfaces that return naïve datetimes into ones that return aware datetimes (which is why we're deprecating these instead of just making them return aware datetimes 😅). This is one reason I worded the warning the way I did.

Another option is to just say, "Use timezone-aware datetimes instead" and not include the code for how to do that, so that they are forced to look up how to do that and are less likely to think it's a drop-in replacement? I suspect that doing so would be a somewhat frustrating user expreience, though. Tough call.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pganssle

pganssle commented Apr 26, 2023

Copy link
Copy Markdown
Member Author

@ambv (or others), what do you think of this wording?

datetime.utcnow() is deprecated and scheduled for removal in a future version.
Instead, Use timezone-aware objects to represent datetimes in UTC:
datetime.now(datetime.UTC). This is not a drop-in replacement.

Alternatively, This will not be a backwards-compatible change. or something to that effect?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pganssle
pganssle force-pushed the deprecate_utcnow branch from 66a82a0 to 3c7c703 Compare April 26, 2023 20:55
pganssle added 5 commits April 27, 2023 10:30
@pganssle
Deprecate utcnow and utcfromtimestamp
d73e4b9
@pganssle
Add news entry for utcnow deprecation
b9c08cf
@pganssle
Remove internal uses of utcnow
261d1d9
@pganssle
Remove all internal uses of utcfromtimestamp
97eff7f
@pganssle
Adjust message to be less wordy
b223173
@pganssle
pganssle force-pushed the deprecate_utcnow branch from 688bb05 to b223173 Compare April 27, 2023 16:30
@pganssle

pganssle commented Apr 27, 2023

Copy link
Copy Markdown
Member Author

OK, I'm going to merge this when CI passes, using Łukasz's wording, unless there are some objections.

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pganssle
pganssle merged commit 0b7fd8f into python:main Apr 27, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Apr 27, 2023
@sobolevn sobolevn mentioned this pull request Apr 27, 2023
crash in "./python.exe -We -m test -v test_datetime -m test_folds" #103944
Closed
ankit-gautam23
ankit-gautam23 reviewed Apr 28, 2023
View reviewed changes

@ankit-gautam23 ankit-gautam23 left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

EmmmaTech reacted with confused emoji
All reactions
  • 😕 1 reaction
@tacaswell tacaswell mentioned this pull request May 18, 2023
deal with upcoming deprecations in CPython matplotlib/matplotlib#25912
Closed
@pganssle pganssle mentioned this pull request Jun 9, 2023
datetime utcnow deprecation leads to type confusion #105544
Closed
@wjandrea wjandrea mentioned this pull request Jul 4, 2023
datetime UTC deprecation warnings use the name "datetime" inconsistently #106392
Closed
@JavierUhagon JavierUhagon mentioned this pull request May 27, 2024
Modify timestamp generation to remove deprecation warning locustio/locust#2738
Merged
This was referenced May 7, 2026
feat: RBAC + database migration layer langflow-ai/openrag#1538
Merged
Добавлен функционал RAG и прописана ручка для работы с LLM citec-spbu/pirogovasproject#45
Merged
This was referenced May 21, 2026
Move OptiX denoiser CI in-repo Ahmed-Hindy/h_denoise_utils#28
Merged
FE: dashboard page changes dhruvbatra810/card-game#3
Merged
This was referenced Jun 3, 2026
Track and added missing files niruban-codes/Emora#79
Merged
feat: implement LLM analysis pipeline and REST API integration cse-project2-lg/AI#7
Merged
Task-W2,3 Supporter09/PizzaHust#8
Open
feat(session): session-context guidance layer (MVP) topoteretes/cognee#2989
Merged
feat: SceneSpeak AI demo initial implementation WsyHLT/-XEngineer-scenespeak-ai#1
Merged
🎨 Palette: Scope job action loading states to individual items harinish45/ai-job-agent#15
Open
Persist saved remote clients in the database karanhudia/borg-ui#639
Merged
This was referenced Jun 13, 2026
feat(K1): View Incoming Orders — read-only kitchen queue Supporter09/PizzaHust#33
Merged
feat: add authentication and resolve fetch issues priyanshu5ingh/VISUALAIZE#183
Open
fix: propagate dynamic api network errors and update site_reference dynamically HiDiHo01/home-assistant-frank_energie#162
Merged
This was referenced Jun 24, 2026
🧪 Add test coverage for memory_mcp _parse_memory_value edge cases sheepdestroyer/LLM-Routing#61
Merged
Release 4.5.53 Lumiwealth/lumibot#1087
Merged
Backfill relational contacts from legacy flat fields bendstaples7/B-B-Commercial-RE-Underwriting#69
Merged
Implement VIT Lifecycle Manager & Dependency Orchestrator (TRACK-003) nemesistip-cloud/vit#301
Merged
This was referenced Jul 3, 2026
(micro-fix)Add AWS CloudWatch MCP tool aden-hive/hive#7304
Closed
feat: intelligent download system with platform detection JesusQuijada34/packagemaker#3
Closed
Fix stale SRI hashes blocking HTMX and Chart.js JJKW1984/golden#4
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jul 20, 2026
Genie 1709/story/built in system redhat-community-ai-tools/UnifAI#239
Closed
This was referenced Aug 1, 2026
Bounty Claim: Fix for issue #1727 moorcheh-ai/memanto#1754
Closed
improvements pavanakarthik12/aurix#2
Merged
feat: separate Nova and Marmot device groups tinhpr9/Aotscript#1
Merged
feat(scheduler): remote-run scheduler for acestor.remote nightly cron dsih-artpark/acestor#116
Merged
feat(skyhook): Add protocol layer, device profiles, and orchestration framework timerloggedout-spec/termux-monorepo#33
Closed
This was referenced Aug 10, 2026
chore: the audit tail — frontmatter nit, skill sizes, and settled decisions Vycari/maintainerd#20
Merged
feat: Buttons Flow runtime with research-deck example autonoco/buttons#285
Merged
@LeonCB LeonCB mentioned this pull request Aug 30, 2026
DeprecationWarning: datetime.datetime.utcnow() is deprecated (Python 3.12+) aquatix/ns-api#42
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv ambv left review comments
@erlend-aasland erlend-aasland erlend-aasland approved these changes
@abalkin abalkin Awaiting requested review from abalkin
@berkerpeksag berkerpeksag Awaiting requested review from berkerpeksag
+1 more reviewer
@ankit-gautam23 ankit-gautam23 ankit-gautam23 left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@pganssle @ambv @erlend-aasland @ankit-gautam23 @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.