Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

datetime.tzinfo

Provides the interface for defining UTC offsets and daylight-saving behavior for aware date-time objects.

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

Syntax

class Fixed(datetime.tzinfo): ...

Provides the interface for defining UTC offsets and daylight-saving behavior for aware date-time objects.

A custom fixed zone can return zero daylight-saving adjustment. Its offset is used when formatting aware timestamps.

Subclass tzinfo to supply time-zone rules. utcoffset(dt) returns the total offset including DST, strictly within ±24 hours, or None if unknown. dst(dt) returns the seasonal component, zero when inactive; tzname(dt) supplies a display name. A custom fromutc may be needed for conversion rules. The sample's +03:00 offset and zero DST produce an aware 07:00 timestamp. fromutc receives a datetime whose fields express UTC and whose tzinfo is the receiver itself. It returns the corresponding local datetime in that zone.

from datetime import datetime, timedelta, tzinfo
class Office(tzinfo):
    def utcoffset(self, dt): return timedelta(hours=3)
    def dst(self, dt): return timedelta(0)
    def tzname(self, dt): return "OFF"
t = datetime(2025, 1, 2, 7, tzinfo=Office())
assert (t.utcoffset(), t.dst(), t.tzname()) == (timedelta(hours=3), timedelta(0), "OFF")
z = t.tzinfo
fields = datetime(2025, 1, 2, 7, tzinfo=z)
assert z.fromutc(fields) == datetime(2025, 1, 2, 10, tzinfo=z)

Examples

import datetime
class Fixed(datetime.tzinfo):
    def utcoffset(self, dt): return datetime.timedelta(hours=3)
    def dst(self, dt): return datetime.timedelta(0)

t = datetime.datetime(2025, 1, 2, 7, tzinfo=Fixed())
print(t.isoformat())
print(t.dst())

Result

2025-01-02T07:00:00+03:00
0:00:00

Subclass tzinfo to supply time-zone rules. utcoffset(dt) returns the total offset including DST, strictly within ±24 hours, or None if unknown. dst(dt) returns the seasonal component, zero when inactive; tzname(dt) supplies a display name. A custom fromutc may be needed for conversion rules. The sample's +03:00 offset and zero DST produce an aware 07:00 timestamp. fromutc receives a datetime whose fields express UTC and whose tzinfo is the receiver itself. It returns the corresponding local datetime in that zone.

Sources