Looking up strptime codes every time an API hands you a timestamp gets old. For ISO 8601 strings, which is what most APIs and logs use, there’s a shortcut.
from datetime import datetime, timezone
t = datetime.fromisoformat("2026-11-30T13:05:00Z")
print(t) # 2026-11-30 13:05:00+00:00
print(t.astimezone()) # same moment in your local zone
now = datetime.now(timezone.utc)
print(now.isoformat(timespec="seconds"))
fromisoformat reads the date, the time, fractions of seconds and the offset, including a trailing Z for UTC. The result is timezone aware, so you can compare it with other aware datetimes or convert it with astimezone().
Going the other way, isoformat(timespec="seconds") gives a clean timestamp without microseconds, which is what I’d want in file names and log lines.
This only got this flexible in Python 3.11. On older versions fromisoformat accepted little more than what isoformat() itself produced, and a Z raised a ValueError. If your script has to run on an old server, check python3 --version first. Also note that a string without an offset gives you a naive datetime, and comparing a naive one with an aware one raises a TypeError.
Further reading: datetime.fromisoformat