-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Comments
Hello! 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? |
If you want to have import datetime as dt
from datetime import datetime
# ...
datetime.now(dt.UTC) |
# 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. |
Bug report
Bug description:
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).
CPython versions tested on:
3.12
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: