ImageImage

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.utcnow() should return a timezone aware datetime
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: datetime.datetime.utcnow should return a UTC timestamp
View: 12756
Assigned To: Nosy List: belopolsky, martin.panter, p-ganssle, rtphokie
Priority: normal Keywords:

Created on 2022-01-09 18:37 by rtphokie, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg410160 - (view) Author: Tony Rice (rtphokie) Date: 2022-01-09 18:37
datetime.datetime.utcnow()

returns a timezone naive datetime, this is counter-intuitive since you are logically dealing with a known timezone. I suspect this was implemented this way for fidelity with the rest of datetime.datetime (which returns timezone naive datetime objects).

The workaround (see below) is to replace the missing tzinfo.

Recommendation:
By default datetime.datetime.utcnow() should return a timezone aware datetime (with tzinfo of UTC of course) or at least offer this behavoir as an option, 

e.g.:

datetime.datetime.utcnow(timezone-aware=True)

Workaround:
dt = datetime.utcnow().replace(tzinfo=timezone.utc)
msg410179 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2022-01-10 00:44
Perhaps this is a duplicate of Issue 12756?
msg410183 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2022-01-10 03:21
Yes, this is the documented behavior, including a warning against using UTC now in the documentation!

There is some possibility of removing utcnow entirely as an "attractive nuisance", but I suspect that this will lead to much consternation and hand-wringing, and there are some legitimate uses for `utcnow`, so I haven't made it a high priority to have that particular fight...
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90477
2022-01-10 03:21:41p-gansslesetstatus: open -> closed
resolution: duplicate
messages: + msg410183

stage: resolved
2022-01-10 00:44:38martin.pantersetsuperseder: datetime.datetime.utcnow should return a UTC timestamp

messages: + msg410179
nosy: + martin.panter
2022-01-09 19:21:11AlexWaygoodsetnosy: + belopolsky, p-ganssle

versions: - Python 3.7, Python 3.8
2022-01-09 18:37:40rtphokiecreate