On this page
zoneinfo
Prev: zlib | Up: stdlib_module
Rel: datetime, calendar, collections, heapq, bisect
Ref: docs.python.org — zoneinfo
Tags: #public #python3 #stdlib
Import
import zoneinfo
Overview
IANA timezone database support (ZoneInfo, available_timezones) built into Python 3.9+, replacing legacy third-party pytz. Pair ZoneInfo("America/Los_Angeles") with datetime to convert UTC timestamps to real-world local wall times with accurate historical and future DST transitions.
Quick Start
from datetime import UTC, datetime
from zoneinfo import ZoneInfo
utc_now = datetime(2026, 7, 1, 18, 0, tzinfo=UTC)
local = utc_now.astimezone(ZoneInfo("America/Los_Angeles"))
assert local.hour == 11 and local.tzname() == "PDT"
Implementation Walkthrough
1. Converting Between Timezones & Handling DST Ambiguity (fold)
During autumn "fall-back" transitions, the same local wall time occurs twice; fold=0 selects the first (DST) occurrence and fold=1 selects the second (standard) occurrence:
from datetime import UTC, datetime
from zoneinfo import ZoneInfo
la = ZoneInfo("America/Los_Angeles")
first = datetime(2026, 11, 1, 1, 30, fold=0, tzinfo=la)
second = datetime(2026, 11, 1, 1, 30, fold=1, tzinfo=la)
assert first.astimezone(UTC) != second.astimezone(UTC)
2. Validating User-Supplied IANA Timezone Keys
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
def parse_tz(name: str) -> ZoneInfo:
try:
return ZoneInfo(name)
except ZoneInfoNotFoundError as exc:
raise ValueError(f"Unknown IANA timezone: {name!r}") from exc
assert parse_tz("Asia/Tokyo").key == "Asia/Tokyo"
Gotchas & Boundaries
- Windows ships without a system IANA tzdata database: Add the official
tzdatapackage to dependencies if your app targets Windows. - Attach
ZoneInfodirectly viatzinfo=ZoneInfo(...)—unlike legacypytz, you do NOT need.localize()or.normalize(). - Store timestamps in UTC (
datetime.UTC), convert withZoneInfoat the edges: Never persist local naive timestamps to databases.
Common Functions
- zoneinfo.available_timezones() —
available_timezones()— Returns a set containing all available time zones. - zoneinfo.reset_tzpath() —
reset_tzpath(to=None)— Reset global TZPATH.
Classes & Objects
- zoneinfo.InvalidTZPathWarning — Warning raised if an invalid path is specified in PYTHONTZPATH. (Methods:
.add_note(),.with_traceback()) - zoneinfo.ZoneInfo —
zoneinfo.ZoneInfoclass. (Methods:.clear_cache(),.dst(),.from_file(),.fromutc(),.no_cache(),.tzname()) - zoneinfo.ZoneInfoNotFoundError — Exception raised when a ZoneInfo key is not found. (Methods:
.add_note(),.with_traceback())
Constants & Attributes
zoneinfo.TZPATH(tuple) =('/usr/share/zoneinfo', '/usr/lib/zoneinfo', '/usr/share/...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/array
- tuts/python3/stdlib_module/bisect
- tuts/python3/stdlib_module/calendar
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/copy
- tuts/python3/stdlib_module/datetime
- tuts/python3/stdlib_module/enum
- tuts/python3/stdlib_module/graphlib
- tuts/python3/stdlib_module/heapq
- tuts/python3/stdlib_module/pprint
- tuts/python3/stdlib_module/reprlib
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/time
- tuts/python3/stdlib_module/types
- tuts/python3/stdlib_module/weakref
- tuts/python3/stdlib_module/zlib
Related notes
- tuts/python3/stdlib_module/bisect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/datetime Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/heapq Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/array links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/copy links to this page; shared tags: #stdlib