Sub topic 4
UTC and fixed offsets
This chapter assumes you know the difference between naive and aware datetimes from the previous chapter. It also assumes you are comfortable with timedelta. Here you will use the two simplest kinds of time zone in the standard library: UTC and a fixed offset.
UTC as the reference point
UTC (Coordinated Universal Time) is the zone every other zone is measured against. It has no daylight saving changes, so a UTC time is never ambiguous. Python provides it as timezone.utc.
from datetime import datetime, timezone
stamp = datetime(2026, 11, 26, 11, 7, tzinfo=timezone.utc)
print(stamp)
print(stamp.utcoffset())
print(stamp.tzname())This prints 2026-11-26 11:07:00+00:00, then 0:00:00, then UTC. The tzinfo attribute makes the datetime aware. utcoffset() returns a timedelta, which is how far the local clock is ahead of UTC.
Fixed offsets with timezone
A datetime.timezone object represents a constant offset from UTC. You build it from a timedelta, with an optional name.
from datetime import datetime, timedelta, timezone
ist = timezone(timedelta(hours=5, minutes=30), "IST")
utc_time = datetime(2026, 11, 26, 11, 7, tzinfo=timezone.utc)
local = utc_time.astimezone(ist)
print(utc_time)
print(local)
print(local.utcoffset())
print(local.tzname())
print(utc_time == local)The output is:
2026-11-26 11:07:00+00:00
2026-11-26 16:37:00+05:30
5:30:00
IST
TrueThe two values show different clock readings but describe the same instant. Python compares aware datetimes by converting both to UTC first, so == returns True. Subtracting them gives a zero timedelta.
If you leave out the name, Python generates one. timezone(timedelta(hours=-5)).tzname(None) returns 'UTC-05:00'. The offset must be strictly between -24 and +24 hours.
replace versus astimezone
Two methods look similar but do different jobs. This is a very common source of bugs.
replace(tzinfo=...)keeps the clock reading and swaps the label. The instant changes.astimezone(...)keeps the instant and recalculates the clock reading.
from datetime import datetime, timedelta, timezone
minus5 = timezone(timedelta(hours=-5))
t = datetime(2026, 11, 26, 9, 0, tzinfo=minus5)
print(t.replace(tzinfo=timezone.utc))
print(t.astimezone(timezone.utc))The first line prints 2026-11-26 09:00:00+00:00, which is a different moment from t. The second prints 2026-11-26 14:00:00+00:00, which is the same moment. Use replace only when you know a naive value was really meant to be in that zone. Use astimezone to convert.
A naive datetime passed to astimezone is assumed to be in the system's local zone. That makes the result depend on the machine, so attach a tzinfo yourself before converting.
What fixed offsets cannot do
A timezone object has one offset forever. It knows nothing about daylight saving time or historical rule changes.
from datetime import datetime, timedelta, timezone
fixed = timezone(timedelta(hours=-5))
summer = datetime(2026, 7, 1, 12, tzinfo=fixed)
winter = datetime(2026, 12, 1, 12, tzinfo=fixed)
print(summer.utcoffset() == winter.utcoffset())This prints True. A real zone such as New York would use a different offset in July and December. Fixed offsets suit two cases: data that already carries an offset, such as +05:30 in a timestamp string, and zones that never change their clocks. For a place with daylight saving rules, use ZoneInfo, which the next chapters cover. An offset like -05:00 tells you how to read one moment. It does not tell you what the local time will be next month.
Avoiding deprecated shortcuts
Older code often uses datetime.utcnow() or datetime.utcfromtimestamp(). These return naive datetimes that are secretly in UTC, and they are deprecated as of Python 3.12. Prefer datetime.now(timezone.utc), which returns an aware value that cannot be mistaken for local time.
Exercise
A datetime t is created as datetime(2026, 11, 26, 20, 0, tzinfo=timezone(timedelta(hours=2))). What does t.astimezone(timezone.utc) print, and why does t.replace(tzinfo=timezone.utc) give a different result?
Show answer
astimezone prints 2026-11-26 18:00:00+00:00. It keeps the same instant and subtracts the 2-hour offset. replace keeps the 20:00 clock reading and only changes the label to UTC, giving 2026-11-26 20:00:00+00:00, which is a different instant two hours later.
Exercise
Why is timezone(timedelta(hours=-5)) a poor choice to represent New York time all year?
Show answer
It has a constant offset and ignores daylight saving. New York uses UTC-05:00 in winter and UTC-04:00 in summer, so summer times would be off by one hour. A ZoneInfo("America/New_York") object applies the correct offset for each date.