I am comparing two timestamps parsing. One is:
datetime.datetime.strptime("2022-10-20 13:13:13 UTC", "%Y-%m-%d %H:%M:%S %Z")
which returns datetime.datetime(2022, 10, 20, 13, 13, 13)
.
Note that it neither fail (i.e. it parses the UTC
part) nor add a time zone to the resulting object.
The second parsing is:
datetime.datetime.strptime("2022-10-20 13:13:13 00:00", "%Y-%m-%d %H:%M:%S %z")
which returns datetime.datetime(2022, 10, 20, 13, 13, 13, tzinfo=datetime.timezone.utc)
with the correct time zone.
As far as I understand the technical note #6 here, both should yield the same results.
I don't understand the difference, nor how the output of the 1st case is the expected one and aligns with the documentation. I would love to have an explanation on the first case. PS: I would like to avoid using dateutil
.
EDIT: I'll try to focus my question. How can I parse the string "2022-10-20 13:13:13 UTC"
and get a time zone aware datetime
object?
CodePudding user response:
If you look at the code for the input you are using it does not get into creating a timezone aware object as gmtoff
is None
for the input you give. You do get UTC
for tzname
but the code does not make use of it as it does not satisfy the other condition.
You have to combine %Z
along with %z
to get the value you would expect. The docs mean something like this
datetime.datetime.strptime("2022-10-20 13:13:13 UTC 0000", "%Y-%m-%d %H:%M:%S %Z%z")
Which does give
2022-10-20 13:13:13 00:00
CodePudding user response:
Using only the standard library, you cannot parse UTC
directly, i.e. to get an aware datetime object. %Z
directive will simply make the parser ignore it. However, you can replace it with Z
(zulu time == UTC), which can be parsed with %z
directive:
from datetime import datetime
s = "2022-10-20 13:13:13 UTC"
dt = datetime.strptime(s.replace("UTC", "Z"), "%Y-%m-%d %H:%M:%S %z")
print(dt)
print(repr(dt))
# 2022-10-20 13:13:13 00:00
# datetime.datetime(2022, 10, 20, 13, 13, 13, tzinfo=datetime.timezone.utc)
Or, as I've described here, use fromisoformat
after you replace it with 00:00
:
dt = datetime.fromisoformat(s.replace("UTC", " 00:00"))
print(dt)
print(repr(dt))
# 2022-10-20 13:13:13 00:00
# datetime.datetime(2022, 10, 20, 13, 13, 13, tzinfo=datetime.timezone.utc)