Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

datetime.timezone

Creates a time zone with a fixed offset from UTC.

Version: Python 3.14; repository image, production runtime not attested

Syntax

datetime.timezone(offset)
datetime.timezone(offset, name)

Creates a time zone with a fixed offset from UTC.

A fixed offset models no seasonal clock changes. Converting an aware timestamp preserves the instant while changing its local clock reading.

Use a fixed UTC offset expressed as a timedelta with absolute magnitude below 24 hours. An optional name controls tzname. The offset never changes seasonally; timezone.utc has zero offset. At UTC+02:00 noon corresponds to 10:00 UTC, and astimezone preserves that instant. An offset of a whole day raises ValueError.

from datetime import datetime, timedelta, timezone
z = timezone(timedelta(hours=2), "Office")
t = datetime(2025, 7, 1, 12, tzinfo=z)
assert t.tzname() == "Office" and t.dst() is None and t.astimezone(timezone.utc).hour == 10
try:
    timezone(timedelta(days=1))
except ValueError:
    pass
else:
    raise AssertionError

Examples

import datetime
zone = datetime.timezone(datetime.timedelta(hours=2))
t = datetime.datetime(2025, 7, 1, 12, tzinfo=zone)
print(t.isoformat())
print(t.astimezone(datetime.timezone.utc).isoformat())

Result

2025-07-01T12:00:00+02:00
2025-07-01T10:00:00+00:00

Use a fixed UTC offset expressed as a timedelta with absolute magnitude below 24 hours. An optional name controls tzname. The offset never changes seasonally; timezone.utc has zero offset. At UTC+02:00 noon corresponds to 10:00 UTC, and astimezone preserves that instant. An offset of a whole day raises ValueError.

Sources