Python Glossary Русский

Python 3.9+zoneinfoterm

zoneinfo.ZoneInfo

time zones

An IANA time zone such as 'Europe/Moscow' that accounts for daylight saving time and historical changes.

Syntax

zoneinfo.ZoneInfo(key)

Pass it to tzinfo= or astimezone(), not pytz-style localize. Objects are cached: ZoneInfo(k) is ZoneInfo(k) is True. Windows has no system database: without the tzdata package (pip install tzdata) every key raises ZoneInfoNotFoundError, a subclass of KeyError. For a fixed offset with no rules, datetime.timezone is enough.

Examples

from datetime import datetime
from zoneinfo import ZoneInfo
msk = ZoneInfo('Europe/Moscow')
dt = datetime(2024, 1, 15, 12, 0, tzinfo=msk)
print(dt.utcoffset())  # → 3:00:00
print(dt.astimezone(ZoneInfo('UTC')))  # → 2024-01-15 09:00:00+00:00
print(ZoneInfo('Europe/Moscow') is msk)  # → True
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
try:
    ZoneInfo('Mars/Base')
except ZoneInfoNotFoundError as e:
    print(isinstance(e, KeyError))  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy