Python Glossary Русский

all Python 3datetimeterm

datetime.tzinfo

time zone

An abstract base class for time-zone information; the concrete implementation is timezone.

Syntax

class MyTz(datetime.tzinfo): ...

The class is abstract: an instance on its own does nothing, and a subclass must supply utcoffset(), dst() and tzname(). Writing your own subclass is almost never necessary — timezone covers fixed offsets and ZoneInfo from zoneinfo covers real zones with DST rules. In everyday code tzinfo mostly shows up in the dt.tzinfo is None check that tells a naive datetime from an aware one.

Examples

import datetime
print(issubclass(datetime.timezone, datetime.tzinfo))   # → True
print(datetime.timezone.utc.tzname(None))   # → UTC
print(datetime.timezone(datetime.timedelta(hours=3)).utcoffset(None))   # → 3:00:00
d = datetime.datetime(2024, 5, 1, 12, 0, tzinfo=datetime.timezone.utc)
print(d.astimezone(datetime.timezone(datetime.timedelta(hours=3))).hour)   # → 15
print(datetime.datetime(2024, 5, 1).tzinfo)   # → None
print(datetime.tzinfo().utcoffset(None))   # → NotImplementedError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy