[New-bugs-announce] [issue46319] datetime.utcnow() should return a timezone aware datetime

Tony Rice report at bugs.python.org
Sun Jan 9 13:37:40 EST 2022


New submission from Tony Rice <rtphokie at gmail.com>:

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)

----------
components: Library (Lib)
messages: 410160
nosy: rtphokie
priority: normal
severity: normal
status: open
title: datetime.utcnow() should return a timezone aware datetime
type: behavior
versions: Python 3.10, Python 3.11, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46319>
_______________________________________


More information about the New-bugs-announce mailing list