Sub topic 9
Daylight saving time: gaps, repeats, and fold
This chapter assumes you can attach a ZoneInfo to a datetime and convert it with astimezone. You also need to know the difference between naive and aware datetimes.
Why local times can break
Daylight saving time (DST) moves the clocks twice a year. In the United States in 2026 the changes happen at 02:00 local time:
- On March 8, clocks jump from 02:00 to 03:00. Every local time from 02:00 to 02:59 is skipped. This is a gap.
- On November 1, clocks go back from 02:00 to 01:00. Every local time from 01:00 to 01:59 happens twice. This is a fold, or repeated period.
A naive "year, month, day, hour, minute" value therefore doesn't always identify exactly one instant. A local time can match zero instants (gap) or two (repeat).
The fold attribute
Every datetime has a fold attribute that is either 0 or 1. It defaults to 0. It matters only when the local time is ambiguous, and it tells Python which occurrence you mean.
from datetime import datetime
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
# 01:30 on 2026-11-01 happens twice because clocks go back at 02:00 EDT.
first = datetime(2026, 11, 1, 1, 30, tzinfo=tz)
second = first.replace(fold=1)
print(first.tzname(), second.tzname())This prints EDT EST. With fold=0 you get the first occurrence, which is still on daylight time (UTC-4). With fold=1 you get the second occurrence, on standard time (UTC-5). The two datetimes are one hour apart in real time even though their wall-clock fields are identical.
Nonexistent times
Python does not raise an error for a time inside a gap. It accepts the value and applies an offset by rule. With fold=0 it uses the offset from before the change. With fold=1 it uses the offset from after the change.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
gap = datetime(2026, 3, 8, 2, 30, tzinfo=tz)
print(gap.isoformat())
normalized = gap.astimezone(timezone.utc).astimezone(tz)
print(normalized)The first line prints 2026-03-08T02:30:00-05:00. That moment is really 07:30 UTC, and 07:30 UTC is 03:30 EDT on the local clock. The second line prints 2026-03-08 03:30:00-04:00. A round trip through UTC moves the value to a time that does exist.
Detecting gaps and repeats
You can classify a local time by comparing the offsets that fold=0 and fold=1 produce. If they match, the time is unambiguous.
from datetime import datetime
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
def classify(dt):
off0 = dt.replace(fold=0).utcoffset()
off1 = dt.replace(fold=1).utcoffset()
if off0 == off1:
return "normal"
return "repeated" if off0 > off1 else "skipped"
print(classify(datetime(2026, 11, 26, 9, 0, tzinfo=tz)))
print(classify(datetime(2026, 11, 1, 1, 30, tzinfo=tz)))
print(classify(datetime(2026, 3, 8, 2, 30, tzinfo=tz)))The output is normal, repeated, skipped. In a repeated hour, the fold=0 offset (-4 hours) is larger than the fold=1 offset (-5 hours). In a gap the order is reversed.
Common mistakes
- Assuming
foldchanges the printed fields. It doesn't. It changes only the offset and name that Python derives, so printutcoffset()ortzname()to see its effect. - Comparing datetimes that share a
tzinfo. When two aware datetimes have the sametzinfoobject, Python compares their wall-clock fields and ignores the offset.first < secondin the fold example can therefore beFalseeven thoughsecondis a real hour later. Convert both to UTC first when order matters. - Scheduling at a fixed local time. A job at 02:30 local time runs zero times on one day a year and may be ambiguous on another. Store UTC instants, or choose a safe time such as midday.
Exercise
Exercise
In America/New_York, datetime(2026, 11, 1, 1, 30, tzinfo=tz) and the same value with fold=1 have identical fields. Which is later in real time, and what is the gap between them?
Show answer
The fold=1 datetime is later. The fold=0 value is 01:30 EDT (05:30 UTC) and the fold=1 value is 01:30 EST (06:30 UTC). They are one hour apart in real time. Convert both to UTC with astimezone(timezone.utc) to see the difference.
Exercise
Why does datetime(2026, 3, 8, 2, 30, tzinfo=tz) print an offset of -05:00, and what does it become after a round trip through UTC?
Show answer
Local time 02:30 doesn't exist on March 8, so Python applies the offset from before the change (EST, -05:00) because fold=0. That is 07:30 UTC. Converting back to New York gives 03:30 EDT, written 2026-03-08 03:30:00-04:00.