Sub topic 9
Daylight saving time: gaps, repeats, and fold
This chapter assumes you can already build aware datetimes with ZoneInfo and convert them with astimezone. It covers what happens when a local clock time does not exist, or exists twice, and how the fold attribute lets you say which occurrence you mean.
Why local time is not a straight line
UTC moves forward one second at a time. Local time in a zone with daylight saving time (DST) does not. Twice a year the zone's UTC offset changes, and the wall clock jumps.
- Spring forward: clocks skip an interval. Local times inside it never happen. This interval is a gap.
- Fall back: clocks repeat an interval. Local times inside it happen twice. This is a fold, also called an ambiguous or repeated time.
In America/New_York in 2026:
| Date | Clock change | Effect on local time |
|---|---|---|
| 2026-03-08 | 02:00 EST becomes 03:00 EDT | Local times 02:00 to 02:59 do not exist |
| 2026-11-01 | 02:00 EDT becomes 01:00 EST | Local times 01:00 to 01:59 occur twice |
DST is not the only cause. Any change to a zone's offset, such as a government moving to a new standard time, produces a gap or a fold. The size of the shift also varies. Most zones shift by one hour, but Australia's Australia/Lord_Howe shifts by 30 minutes.
The fold attribute
Every datetime and time object has a fold attribute that is either 0 or 1. It defaults to 0. It was added by PEP 495 in Python 3.6, so it is available in every Python version zoneinfo supports.
fold only matters when the local time is ambiguous or skipped. In a repeated interval, it chooses which occurrence you mean:
fold=0is the first occurrence, before the clocks go back (the earlier moment on the UTC timeline).fold=1is the second occurrence, after the clocks go back.
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. The two objects have the same year, month, day, hour, and minute. They differ by fold, and so by their UTC offset: -04:00 for the first and -05:00 for the second. They are one hour apart on the UTC timeline.
Outside a gap or fold, fold has no effect on the offset. Setting fold=1 on a normal time is harmless, and fixed-offset zones such as timezone.utc ignore it entirely.
Conversions set fold for you
When astimezone converts a UTC moment into a zone, it sets fold correctly. You rarely set it by hand when the data comes from UTC.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
for hour in (5, 6):
utc = datetime(2026, 11, 1, hour, 30, tzinfo=timezone.utc)
local = utc.astimezone(tz)
print(utc.isoformat(), "->", local.isoformat(), "fold =", local.fold)The output is:
2026-11-01T05:30:00+00:00 -> 2026-11-01T01:30:00-04:00 fold = 0
2026-11-01T06:30:00+00:00 -> 2026-11-01T01:30:00-05:00 fold = 1Two different UTC moments produce the same wall-clock reading. Only fold and the offset tell them apart. This is the reason to store UTC: a UTC value always identifies one moment, and a local time sometimes does not.
What happens inside a gap
A skipped time cannot be a real moment, but Python lets you construct one. zoneinfo does not raise an error. It picks an offset using fold:
fold=0uses the offset in effect before the transition.fold=1uses the offset in effect after the transition.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
# 02:30 on 2026-03-08 never happens: clocks jump from 02:00 EST to 03:00 EDT.
gap = datetime(2026, 3, 8, 2, 30, tzinfo=tz)
print(gap.isoformat())
print(gap.replace(fold=1).isoformat())
print(gap.astimezone(timezone.utc).astimezone(tz).isoformat())
print(gap.replace(fold=1).astimezone(timezone.utc).astimezone(tz).isoformat())The output is:
2026-03-08T02:30:00-05:00
2026-03-08T02:30:00-04:00
2026-03-08T03:30:00-04:00
2026-03-08T01:30:00-05:00Read the four lines in turn:
- With
fold=0, the nonexistent 02:30 gets the old offset,-05:00. - With
fold=1, it gets the new offset,-04:00. - Converting the
fold=0value through UTC gives 07:30 UTC, which is 03:30 EDT. This moves the time forward past the gap. - Converting the
fold=1value through UTC gives 06:30 UTC, which is 01:30 EST. This moves the time backward before the gap.
The invalid datetime object itself stays in your program. Printing it, formatting it, or storing its isoformat() string preserves a time that never occurred. The error only shows up when you convert or compare it.
Detecting gaps and folds
You can detect both cases by comparing the offsets that fold=0 and fold=1 select. If they match, the time is normal. If they differ, the time is in a gap or a fold. In a fold, the earlier occurrence has the larger offset, such as -04:00 compared with -05:00. In a gap, the offset before the transition is the smaller one.
from datetime import datetime
from zoneinfo import ZoneInfo
def classify(dt):
"""Return 'normal', 'repeated', or 'skipped' for an aware datetime."""
off0 = dt.replace(fold=0).utcoffset()
off1 = dt.replace(fold=1).utcoffset()
if off0 == off1:
return "normal"
return "repeated" if off0 > off1 else "skipped"
tz = ZoneInfo("America/New_York")
print(classify(datetime(2026, 6, 1, 12, 0, tzinfo=tz)))
print(classify(datetime(2026, 11, 1, 1, 30, tzinfo=tz)))
print(classify(datetime(2026, 3, 8, 2, 30, tzinfo=tz)))This prints normal, repeated, and skipped.
The function assumes the zone's offset gets smaller when clocks go back, which is true for DST changes. It is a heuristic that works for the transitions you will meet in practice.
Choosing a policy
Once you can classify a time, decide what your program should do with each case. There is no universally right answer, so pick a policy per feature and write it down.
| Situation | Common policies |
|---|---|
| Repeated time from user input | Ask the user, default to the first occurrence (`fold=0`), or reject it |
| Skipped time from user input | Reject it, shift forward past the gap, or shift backward before it |
| Repeated time from stored UTC | Nothing to decide. Conversion sets `fold` for you |
| Recurring event ("every day at 02:30") | Decide per occurrence. Many calendars shift forward on gap days and use the first occurrence on fold days |
Here is a helper that applies a strict policy for user input and an optional shift-forward policy for gaps:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
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"
def resolve_local(naive, tz, shift_gaps=False):
"""Attach tz to a naive local time and return an unambiguous UTC datetime."""
dt = naive.replace(tzinfo=tz)
kind = classify(dt)
if kind == "repeated":
raise ValueError(f"{naive} is ambiguous in {tz.key}; choose fold=0 or fold=1")
if kind == "skipped":
if not shift_gaps:
raise ValueError(f"{naive} does not exist in {tz.key}")
# fold=0 uses the old offset, so the UTC round trip lands after the gap.
return dt.astimezone(timezone.utc)
return dt.astimezone(timezone.utc)
tz = ZoneInfo("America/New_York")
print(resolve_local(datetime(2026, 3, 8, 2, 30), tz, shift_gaps=True))
try:
resolve_local(datetime(2026, 11, 1, 1, 30), tz)
except ValueError as e:
print(e)The first call prints 2026-03-08 07:30:00+00:00. Converting that back to New York gives 03:30 EDT, so the time moved forward by one hour. The second call prints the ambiguity message. A caller that wants the second occurrence passes naive.replace(fold=1) instead, and the helper would use dt = naive.replace(tzinfo=tz) with that fold preserved. That would need the function to keep naive.fold, so adapt it to your needs.
Gotchas
Same-zone comparison ignores fold
When two aware datetimes share the same tzinfo object, Python compares their wall-clock fields and ignores the offset and fold. This produces a surprising result for the two occurrences of a repeated time:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
first = datetime(2026, 11, 1, 1, 30, tzinfo=tz)
second = first.replace(fold=1)
print(first == second)
print(second - first)
print(second.astimezone(timezone.utc) - first.astimezone(timezone.utc))This prints True, 0:00:00, and 1:00:00. The two values are equal and zero seconds apart when compared in their own zone, and an hour apart on the UTC timeline. Convert to UTC before you compare or subtract times that might fall near a transition.
The attribute is not part of most constructors
fold is a keyword-only argument of the datetime constructor, so datetime(2026, 11, 1, 1, 30, tzinfo=tz, fold=1) works. Methods that build a new object from an existing one, such as replace, keep fold unless you pass a new value. Operations that do arithmetic, such as adding a timedelta, reset it to 0 in the result.
Naive datetimes carry fold but cannot use it
A naive datetime has a fold attribute, but with no time zone it has no offset to select. It is only meaningful once you attach a zone.
Time zone data decides what counts as a transition
Which instants are gaps and folds comes from the IANA time zone database. If the system's data is old, your program can classify a time wrongly after a government changes its rules. The next chapters cover arithmetic across these changes and where the data comes from.
Exercise
A user in Europe/Paris types the local time 2026-10-25 02:30. Clocks in Paris go back from 03:00 CEST to 02:00 CET on that date. Is this time normal, repeated, or skipped, and which fold value picks the later moment?
Show answer
It is repeated, because the interval from 02:00 to 02:59 happens twice. The first occurrence is in CEST (fold=0) and the second is in CET (fold=1), so fold=1 selects the later moment.
Exercise
You build datetime(2026, 3, 8, 2, 30, tzinfo=ZoneInfo("America/New_York")) and convert it to UTC with astimezone. What UTC time do you get, and why is the result not 06:30 or the local time you typed?
Show answer
You get 07:30 UTC. The time falls in a gap, so with fold=0 Python uses the offset before the transition, -05:00, and 02:30 at -05:00 is 07:30 UTC. Converting that back gives 03:30 EDT.
Exercise
Two datetimes in the same ZoneInfo zone represent the two occurrences of 01:30 on a fall-back day. Why does first == second return True, and how do you compare them correctly?
Show answer
Python ignores tzinfo offsets and fold when both values share the same tzinfo object, so it compares only the wall-clock fields. Convert both to UTC with astimezone(timezone.utc) before comparing or subtracting, which shows they are one hour apart.