1

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?

Dror
  • 12,174
  • 21
  • 90
  • 160
  • Replace 'UTC' with 'Z', then parse with `'%z'` (lower-case z)? – FObersteiner Nov 14 '22 at 17:23
  • @FObersteiner This is pretty much the 2nd case I mentioned. As no offset is specified in your suggestion the input is parsed as UTC. But it doesn't answer my question. – Dror Nov 15 '22 at 06:18
  • 1
    Concerning *technical note #6*, it doesn't say that the result will be aware. So to answer your question: `%Z` simply ***ignores*** specifiers such as 'UTC'. Imho, the docs could be more clear here, since naive datetime in Python means *local time* implicitly - Which can lead to unexpected results if you e.g. default = UTC (as it is in other languages). – FObersteiner Nov 15 '22 at 08:57

1 Answers1

1

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)
FObersteiner
  • 22,500
  • 8
  • 42
  • 72