Sub topic 13
Naive vs aware datetimes and time zones
This chapter assumes you can create datetime objects, do arithmetic with timedelta, and use strftime and strptime. It covers the difference between naive and aware datetimes, and how to convert between time zones.
Naive and aware datetimes
A naive datetime has no time zone information. Its tzinfo attribute is None, so 12:00 could mean noon anywhere. An aware datetime carries a tzinfo object, so it points to one exact moment in time.
from datetime import datetime, timezone
naive = datetime(2026, 11, 26, 12, 0)
aware = datetime(2026, 11, 26, 12, 0, tzinfo=timezone.utc)
print(naive.tzinfo) # None
print(aware.tzinfo) # UTC
print(aware) # 2026-11-26 12:00:00+00:00datetime.now() returns a naive local time. datetime.now(timezone.utc) returns an aware UTC time. Prefer the aware form for anything you store or send to another system.
Converting between zones
timezone.utc is a fixed zone with no offset. For named regions such as Asia/Tokyo, use zoneinfo.ZoneInfo. It reads the IANA time zone database, which includes daylight saving rules. The astimezone() method converts an aware datetime to another zone and keeps the same moment in time.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
meeting = datetime(2026, 11, 26, 12, 0, tzinfo=timezone.utc)
print(meeting.astimezone(ZoneInfo("Asia/Tokyo"))) # 2026-11-26 21:00:00+09:00The offset for a named zone depends on the date. The same UTC time gives different local times in summer and winter:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
summer = datetime(2026, 7, 1, 12, 0, tzinfo=timezone.utc)
winter = datetime(2026, 11, 26, 12, 0, tzinfo=timezone.utc)
ny = ZoneInfo("America/New_York")
print(summer.astimezone(ny)) # 2026-07-01 08:00:00-04:00
print(winter.astimezone(ny)) # 2026-11-26 07:00:00-05:00On some systems, especially Windows, ZoneInfo needs the tzdata package (pip install tzdata) because the operating system does not provide the zone database.
Mixing naive and aware values
Python refuses to compare or subtract a naive datetime and an aware one, because the result would be a guess.
from datetime import datetime, timezone
meeting = datetime(2026, 11, 26, 12, 0, tzinfo=timezone.utc)
naive = datetime(2026, 11, 26, 12, 0)
try:
print(meeting - naive)
except TypeError as exc:
print("Cannot mix naive and aware:", exc)The error message is can't subtract offset-naive and offset-aware datetimes. Ordering comparisons such as < raise the same error. Using == does not raise. It returns False.
Attaching a zone and converting are different operations
replace(tzinfo=...)labels a naive datetime. It does not change the clock reading. Use it only when you know which zone the naive value was recorded in.astimezone(...)converts an aware datetime. The clock reading changes and the moment stays the same.
from datetime import datetime
from zoneinfo import ZoneInfo
naive = datetime(2026, 11, 26, 12, 0)
paris = naive.replace(tzinfo=ZoneInfo("Europe/Paris"))
print(paris) # 2026-11-26 12:00:00+01:00Calling astimezone() on a naive datetime assumes it is in the system's local zone. That makes results depend on the machine running the code.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Subtracting naive from aware | `TypeError` | Make both aware |
| Using `replace(tzinfo=...)` to convert | Wrong moment in time | Use `astimezone()` |
| Storing local times | Ambiguous values during daylight saving changes | Store UTC |
| Fixed offsets like `timezone(timedelta(hours=-5))` for a region | Wrong in summer | Use `ZoneInfo` |
Exercise
A naive datetime datetime(2026, 11, 26, 9, 0) was recorded in America/New_York. Write the line that makes it aware in that zone, then the line that converts it to UTC. What UTC time results?
Show answer
ny = naive.replace(tzinfo=ZoneInfo("America/New_York")) followed by ny.astimezone(timezone.utc). New York is UTC-5 on 26 November, so the result is 2026-11-26 14:00:00+00:00.