zoneinfo — IANA time zone support¶
Added in version 3.9.
Source code: Lib/zoneinfo
The zoneinfo module provides a concrete time zone implementation to
support the IANA time zone database as originally specified in PEP 615. By
default, zoneinfo uses the system’s time zone data if available; if no
system time zone data is available, the library will fall back to using the
first-party tzdata package available on PyPI.
See also
Availability: not WASI.
This module does not work or is not available on WebAssembly. See WebAssembly platforms for more information.
Using ZoneInfo¶
ZoneInfo is a concrete implementation of the datetime.tzinfo
abstract base class, and is intended to be attached to tzinfo, either via
the constructor, the datetime.replace
method or datetime.astimezone:
>>> from zoneinfo import ZoneInfo
>>> import datetime as dt
>>> when = dt.datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
>>> print(when)
2020-10-31 12:00:00-07:00
>>> when.tzname()
'PDT'
Datetimes constructed in this way are compatible with datetime arithmetic and handle daylight saving time transitions with no further intervention:
>>> when_add = when + dt.timedelta(days=1)
>>> print(when_add)
2020-11-01 12:00:00-08:00
>>> when_add.tzname()
'PST'
These time zones also support the fold attribute
introduced in PEP 495. During offset transitions which induce ambiguous
times (such as a daylight saving time to standard time transition), the offset
from before the transition is used when fold=0, and the offset after
the transition is used when fold=1, for example:
>>> when = dt