A little example:
$ python
Python 3.13.2 (main, Feb 5 2025, 08:05:21) [GCC 14.2.1 20250128] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.strptime("2025-UTC", "%Y-%Z")
datetime.datetime(2025, 1, 1, 0, 0)
>>> datetime.datetime.strptime("2025-+0000", "%Y-%z")
datetime.datetime(2025, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
However reading the documentation, I would expect %Z
passed to strptime
to return an aware object (for the values it supports), as %z
does:
Even stranger, the internal function called by datetime.datetime.strptime
: _strptime parses the UTC
as expected:
>>> from _strptime import _strptime
>>> _strptime("2025-UTC", "%Y-%Z")
((2025, 1, 1, 0, 0, 0, 2, 1, 0, 'UTC', None), 0, 0)
So why doesn’t the high-level function return an aware object if the low-level function parsed the data?
I witnessed this behavior with python 3.5, 3.7, 3.10, 3.12 and 3.13 so I assume it isn’t a bug, but a feature? And so, what is the expected use/behavior of %Z
in datetime.datetime.strptime
?
Did I miss something in the documentation or is it a bit unclear as to strptime
is returning a naive or an aware object?