Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationWarning provides incorrect guidance for timezone-aware datetime (datetime.UTC instead of timezone.utc) #126581

Closed
ch2ohchohch2oh66 opened this issue Nov 8, 2024 · 3 comments

Comments

@ch2ohchohch2oh66
Copy link

ch2ohchohch2oh66 commented Nov 8, 2024

Bug report

Bug description:

from datetime import datetime
time_with_warning = datetime.utcnow()
time_with_error = datetime.now(datetime.UTC)
time_is_correct = datetime.now(timezone.utc)

When using datetime.datetime.utcnow(), Python raises a DeprecationWarning. The warning suggests using datetime.datetime.now(datetime.UTC) to create a timezone-aware UTC datetime object. However, this suggestion is incorrect because datetime.UTC does not exist in Python's datetime module. The correct approach should be datetime.datetime.now(timezone.utc).

image
image

CPython versions tested on:

3.12

Operating systems tested on:

Windows

@ch2ohchohch2oh66 ch2ohchohch2oh66 added the type-bug An unexpected behavior, bug, or error label Nov 8, 2024
@Eclips4
Copy link
Member

Eclips4 commented Nov 8, 2024

Hello!
It looks like there's something wrong with your IDE (PyCharm) because datetime.UTC is actually present in both implementations:

eclips4@nixos ~> python3.12
Python 3.12.5 (main, Aug  6 2024, 19:08:49) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _datetime  # C implementation
>>> _datetime.UTC
datetime.timezone.utc
>>> import _pydatetime  # Python implementation
>>> _pydatetime.UTC
datetime.timezone.utc

Am I missing something?

@Eclips4 Eclips4 added the pending The issue will be closed if no feedback is provided label Nov 8, 2024
@brianschubert
Copy link
Contributor

brianschubert commented Nov 8, 2024

from datetime import datetime

datetime.UTC lives on the datetime module, but it looks like you're trying to access it on the datetime.datetime class.

If you want to have datetime reference the class, you can import the module under a different name:

import datetime as dt
from datetime import datetime
# ...
datetime.now(dt.UTC)

@ch2ohchohch2oh66
Copy link
Author

ch2ohchohch2oh66 commented Nov 8, 2024

# This is correct
import datetime
t = datetime.datetime.now(datetime.UTC)

# This is incorrect
from datetime import datetime
t = datetime.now(datetime.UTC)

# This is also correct
from datetime import datetime, timezone, UTC
t1 = datetime.now(UTC)
t2 = datetime.now(timezone.utc)

Thank you, everyone. I have found the root of the problem.

@Eclips4 Eclips4 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2024
@Eclips4 Eclips4 removed type-bug An unexpected behavior, bug, or error pending The issue will be closed if no feedback is provided labels Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants