Sub topic 4
UTC and fixed offsets
This chapter covers the simplest kind of aware datetime: one whose time zone is UTC or a constant offset from UTC. Later chapters build on it with real zones that change their offset during the year.
It assumes you know what a naive and an aware datetime are (the previous chapter), basic timedelta arithmetic, and Python 3.9 or later. Unless a note says otherwise, the examples run on any version from 3.9.
What UTC and an offset mean
UTC (Coordinated Universal Time) is the shared reference clock that civil time is defined against. It has no daylight saving changes and no regional variation. Every other clock is described by how far it sits from UTC.
An offset is that distance, written as +HH:MM or -HH:MM. A clock showing 19:30 with offset +05:30 shows the same moment as a UTC clock showing 14:00. The rule is:
local time = UTC time + offset
UTC time = local time - offsetAn offset says nothing about where you are or which rules govern your clock. It is only a number attached to one specific reading of the clock. The same place can have different offsets at different times of year.
Creating aware datetimes with `timezone`
The standard library provides datetime.timezone. It is a tzinfo subclass for zones that never change. It has two forms:
timezone.utcis the singleton for UTC.timezone(offset, name=None)takes atimedeltaand an optional name.
from datetime import datetime, timedelta, timezone
utc_dt = datetime(2026, 11, 26, 14, 0, tzinfo=timezone.utc)
print(utc_dt)
print(utc_dt.utcoffset())
print(utc_dt.tzname())
ist = timezone(timedelta(hours=5, minutes=30), "IST")
local = utc_dt.astimezone(ist)
print(local)
print(local.utcoffset())
print(local.tzname())
print(local == utc_dt)
print(local - utc_dt)This prints:
2026-11-26 14:00:00+00:00
0:00:00
UTC
2026-11-26 19:30:00+05:30
5:30:00
IST
True
0:00:00The two datetimes show different clock readings but compare equal. Their difference is zero. Aware datetimes represent instants, and Python compares the instants, not the printed fields.
If you leave out the name, tzname() builds one from the offset:
from datetime import datetime, timedelta, timezone
est = timezone(timedelta(hours=-5))
dt = datetime(2026, 11, 26, 9, 0, tzinfo=est)
print(dt.tzname())
print(dt.tzinfo)
print(dt.isoformat())The output is UTC-05:00, then UTC-05:00, then 2026-11-26T09:00:00-05:00.
Since Python 3.11 there is also an alias, datetime.UTC, for timezone.utc. Use timezone.utc if your code must run on 3.9 or 3.10.
Ways to get a UTC datetime
| Expression | Result | Notes |
|---|---|---|
| `datetime.now(timezone.utc)` | Aware UTC datetime | The recommended way to get the current time |
| `dt.astimezone(timezone.utc)` | Aware UTC datetime | Converts an existing aware datetime |
| `datetime(..., tzinfo=timezone.utc)` | Aware UTC datetime | Use only when the fields are already UTC |
| `datetime.utcnow()` | Naive datetime | Deprecated since 3.12. Avoid it |
The last row is a common trap. utcnow() returns UTC clock fields but with no tzinfo. Python then treats the value as naive, and later calls such as astimezone() or timestamp() interpret it as local time. That silently shifts the result by your machine's offset.
The same instant in several offsets
This diagram shows one instant seen through three offsets.
All three displays refer to the same moment. Converting between them changes the fields and the attached offset, never the instant.
`replace(tzinfo=...)` versus `astimezone(...)`
Two methods attach or change a time zone, and they do different things.
replace(tzinfo=tz)keeps the clock fields and swaps the label. It changes which instant the datetime represents.astimezone(tz)keeps the instant and recomputes the fields for the new zone.
from datetime import datetime, timedelta, timezone
naive = datetime(2026, 11, 26, 9, 0)
minus5 = timezone(timedelta(hours=-5))
labelled = naive.replace(tzinfo=minus5)
print(labelled)
print(labelled.astimezone(timezone.utc))
mislabelled = naive.replace(tzinfo=timezone.utc)
print(mislabelled)
print(mislabelled == labelled)Output:
2026-11-26 09:00:00-05:00
2026-11-26 14:00:00+00:00
2026-11-26 09:00:00+00:00
FalseUse replace(tzinfo=...) to declare what a naive value already meant, for example when you know a legacy timestamp was recorded in UTC. Use astimezone to change how an existing instant is displayed.
Offsets are not time zones
A fixed offset is a constant. A real time zone is a set of rules that yields different offsets at different times. The difference shows up as soon as you compare a fixed -05:00 with New York.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
fixed = timezone(timedelta(hours=-5))
ny = ZoneInfo("America/New_York")
for month in (1, 7):
f = datetime(2026, month, 15, 12, 0, tzinfo=fixed)
z = datetime(2026, month, 15, 12, 0, tzinfo=ny)
print(f.isoformat(), z.isoformat())This prints:
2026-01-15T12:00:00-05:00 2026-01-15T12:00:00-05:00
2026-07-15T12:00:00-05:00 2026-07-15T12:00:00-04:00In January the two agree, because New York is on standard time. In July New York is on daylight time (-04:00) while the fixed offset stays at -05:00. If you label a New York timestamp with timezone(timedelta(hours=-5)), it will be an hour wrong for roughly eight months of the year.
Use a fixed offset when the offset really is constant:
- UTC itself.
- An offset you received with a timestamp and want to keep as it was, such as the
+05:30in an ISO 8601 string. - Regions that do not observe daylight saving, when you accept that political changes to their offset would not be reflected.
Use ZoneInfo (covered in a later chapter) when you need "what is the local time in this place at that date".
Offsets that arrive in parsed text
When you parse an ISO 8601 string that ends in an offset, fromisoformat builds a fixed-offset timezone for you.
from datetime import datetime, timezone
dt = datetime.fromisoformat("2026-11-26T19:30:00+05:30")
print(dt.tzinfo)
print(dt.utcoffset())
print(dt.astimezone(timezone.utc).isoformat())The output is UTC+05:30, then 5:30:00, then 2026-11-26T14:00:00+00:00.
The parsed datetime keeps the offset it arrived with, but it has no zone name or rules. You know the offset at that instant and nothing else. You cannot reliably add six months to it and expect the right local time, because the offset carries no daylight saving rules.
Limits and edge cases
Fixed offsets have a few rules worth knowing before you hit them in production.
- The offset must be strictly between -24 and +24 hours.
timezone(timedelta(hours=24))raisesValueError. Real-world offsets range from-12:00to+14:00. - Offsets need not be whole hours.
+05:30(India),+05:45(Nepal) and+08:45(a Western Australian locality) all exist. Always build offsets withtimedelta(hours=..., minutes=...), never by treating the offset as an integer number of hours. - Sub-minute offsets are allowed since Python 3.7. You will rarely need them, but historical zones used them, and
utcoffset()can return values such as0:19:32. - A name is optional and only cosmetic. Two
timezoneobjects with the same offset but different names compare equal, and datetimes using them compare by instant. - Negative offsets print as a negative
timedelta.timezone(timedelta(hours=-5)).utcoffset(None)istimedelta(hours=-5), whose printed form is-1 day, 19:00:00. This is correct, but it looks odd. For display, preferisoformat()orstrftime("%z").
The first limit is easy to check:
from datetime import timedelta, timezone
try:
timezone(timedelta(hours=24))
except ValueError as e:
print(type(e).__name__)
print(timezone(timedelta(hours=-5)).utcoffset(None))This prints ValueError, then -1 day, 19:00:00.
Subtraction and comparison between offsets
Arithmetic between aware datetimes works in absolute time. Subtracting two aware datetimes gives the real elapsed time, whatever offsets they carry.
from datetime import datetime, timedelta, timezone
tokyo = timezone(timedelta(hours=9))
paris = timezone(timedelta(hours=1))
a = datetime(2026, 11, 26, 23, 0, tzinfo=tokyo)
b = datetime(2026, 11, 26, 16, 0, tzinfo=paris)
print(a - b)
print(a < b)
print(sorted([a, b])[0] is a)Here a is 14:00 UTC and b is 15:00 UTC. The output is -1 day, 23:00:00 (that is, minus one hour), then True, then True. Because every comparison goes through UTC, you can mix datetimes with different fixed offsets in the same list and sort it correctly. Comparing an aware value with a naive one still raises TypeError, as the previous chapter showed.
Adding a timedelta to a fixed-offset datetime keeps the same offset and moves the fields. This is safe because the offset never changes, so wall-clock and absolute arithmetic give the same answer. That equivalence is exactly what breaks for real zones with daylight saving, which a later chapter covers.
Practical guidance
- Use
timezone.utcas your default zone for stored and exchanged times. - Keep offsets that arrive in input if you need to show the sender's local time, but convert to UTC before comparing or storing.
- Never use a fixed offset to stand in for a named place. Reach for
ZoneInfowhen the rules matter. - Avoid
utcnow()andutcfromtimestamp(). Both return naive values. Usenow(timezone.utc)andfromtimestamp(ts, tz=timezone.utc).
Exercise
A log line says 2026-11-26T23:30:00+09:00. Write code that parses it and prints the equivalent UTC time in ISO format. What does it print?
Show answer
Parse with datetime.fromisoformat, then convert with astimezone(timezone.utc):
from datetime import datetime, timezone
dt = datetime.fromisoformat("2026-11-26T23:30:00+09:00")
print(dt.astimezone(timezone.utc).isoformat())
It prints 2026-11-26T14:30:00+00:00. Subtracting the +09:00 offset moves 23:30 back nine hours to 14:30 UTC on the same date.
Exercise
A colleague stores New York meeting times by calling .replace(tzinfo=timezone(timedelta(hours=-5))) on naive datetimes. For which part of the year are the stored instants wrong, and by how much?
Show answer
They are wrong while New York observes daylight saving time (from the second Sunday in March to the first Sunday in November). In that period New York's offset is -04:00, but the code labels the times -05:00, so each stored instant is one hour later than the real one. In winter the offsets coincide and the data is correct. The fix is to attach ZoneInfo("America/New_York") instead of a fixed offset.
Exercise
What is the difference between dt.replace(tzinfo=timezone.utc) and dt.astimezone(timezone.utc) when dt is an aware datetime with offset +02:00 showing 10:00?
Show answer
replace keeps the fields and relabels them, producing 10:00+00:00, which is a different instant (two hours later than the original). astimezone keeps the instant and recomputes the fields, producing 08:00+00:00, which equals the original datetime.