Sub topic 7
Real-world time zones with ZoneInfo
This chapter assumes you know the difference between naive and aware datetimes, and that you can use timezone.utc and fixed offsets. A fixed offset such as UTC-05:00 cannot describe a place like New York, because New York changes its offset during the year. zoneinfo, added in Python 3.9, fills that gap.
What a ZoneInfo object is
A ZoneInfo object represents an IANA time zone. The IANA database (also called tz or Olson) is a shared, versioned record of every zone's offset history, including daylight saving time (DST) rules. You identify a zone by a key of the form Area/Location, such as America/New_York, Europe/London, or Asia/Tokyo.
ZoneInfo is a tzinfo subclass. You attach it to a datetime the same way you attach timezone.utc.
from datetime import datetime
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
winter = datetime(2026, 1, 15, 9, 0, tzinfo=ny)
summer = datetime(2026, 7, 15, 9, 0, tzinfo=ny)
print(winter.isoformat(), winter.tzname(), winter.dst())
print(summer.isoformat(), summer.tzname(), summer.dst())The output is:
2026-01-15T09:00:00-05:00 EST 0:00:00
2026-07-15T09:00:00-04:00 EDT 1:00:00Both datetimes use the same ZoneInfo object, yet the offset differs. The zone looks up the correct offset for each moment. That lookup is why a fixed timezone(timedelta(hours=-5)) is the wrong tool for a place that observes DST.
These methods report what the zone decided:
utcoffset()returns the offset from UTC as atimedelta.tzname()returns the abbreviation in effect, such asEST.dst()returns the current DST adjustment, which is zero outside DST.
Choosing and validating keys
Use full region keys, not abbreviations. EST and PST are ambiguous in the real world and do not follow DST rules. A key like America/New_York carries the whole history for that place.
A key that does not exist raises ZoneInfoNotFoundError, a subclass of KeyError.
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError, available_timezones
try:
ZoneInfo("America/NewYork")
except ZoneInfoNotFoundError as e:
print("unknown zone:", e)
print("Europe/Paris" in available_timezones())The first call fails because of the typo. The second line prints True, assuming time zone data is available on your system (see below). available_timezones() returns a set of every key Python can find. When a user supplies a zone name, catch the error at that boundary and report a clear message.
Instances are cached
ZoneInfo("Asia/Tokyo") returns the same object each time you call it with the same key.
from zoneinfo import ZoneInfo
a = ZoneInfo("Asia/Tokyo")
b = ZoneInfo("Asia/Tokyo")
print(a is b, a.key)This prints True Asia/Tokyo. The caching makes repeated construction cheap, and datetimes in the same zone compare quickly. The key attribute gives you the zone name back, which is useful when you store a user's zone preference alongside their data.
Attaching versus converting
There are two different operations, and mixing them up is the most common zoneinfo mistake.
- Attaching gives a naive datetime a zone. The wall-clock numbers stay the same. Use
tzinfo=in the constructor orreplace(tzinfo=...). - Converting moves an already-aware datetime to another zone. The instant stays the same and the wall-clock numbers change. Use
astimezone(), which the next chapter covers.
from datetime import datetime
from zoneinfo import ZoneInfo
naive = datetime(2026, 11, 26, 9, 0)
attached = naive.replace(tzinfo=ZoneInfo("America/New_York"))
relabelled = attached.replace(tzinfo=ZoneInfo("Asia/Tokyo"))
print(attached)
print(relabelled)The output is 2026-11-26 09:00:00-05:00 and then 2026-11-26 09:00:00+09:00. The second line is a different moment, 14 hours earlier than the first. Calling replace(tzinfo=...) on an aware datetime relabels it and does not convert it.
Where the data comes from
zoneinfo reads the IANA database from your operating system, usually under /usr/share/zoneinfo. Windows has no such directory, and some minimal containers omit it. In those cases ZoneInfo(...) raises ZoneInfoNotFoundError even for valid keys. Installing the tzdata package from PyPI supplies the data. The tzdata chapter later in this tutorial covers this in detail.
Summary table
| Task | Tool |
|---|---|
| Zone with no DST, fixed offset | `timezone(timedelta(...))` |
| Real place with DST history | `ZoneInfo("Area/Location")` |
| Label a naive datetime | `tzinfo=` or `replace(tzinfo=)` |
| Check a key exists | `available_timezones()` |
Exercise
What do datetime(2026, 7, 15, 9, 0, tzinfo=ZoneInfo("America/New_York")).tzname() and .dst() return, and why is a fixed timezone(timedelta(hours=-5)) not a substitute?
Show answer
tzname() returns EDT and dst() returns 1:00:00, because New York is on daylight saving time in July (offset -04:00). A fixed offset of -5 hours never changes, so it would give the wrong offset for every summer date. ZoneInfo looks up the correct offset for each moment.
Exercise
A naive datetime dt = datetime(2026, 11, 26, 9, 0) is already known to be Tokyo time. Write the line that makes it aware.
Show answer
dt.replace(tzinfo=ZoneInfo("Asia/Tokyo")), or build it directly with datetime(2026, 11, 26, 9, 0, tzinfo=ZoneInfo("Asia/Tokyo")). Both keep the wall-clock time and attach the zone.