Lesson 5 of 9 · 27m
DST Edge Cases: Missing, Ambiguous, and Impossible Local Times
Handle spring-forward gaps, fall-back repeats, transition instants, and arithmetic that crosses daylight-saving boundaries.
Daylight saving time is not a seasonal nuisance; it is a discontinuity deliberately inserted into civil time. On one night a local hour vanishes. Months later an hour happens twice. If your scheduler assumes every local time exists exactly once, it will eventually skip an invoice run, double-send a reminder, or schedule a job at a time the calendar never contained.
The two failure shapes
In America/New_York, spring forward creates a gap:
2026-03-08 01:59:59 EST UTC-5
2026-03-08 03:00:00 EDT UTC-402:30 does not exist. Autumn fallback creates a fold:
2026-11-01 01:59:59 EDT UTC-4
2026-11-01 01:00:00 EST UTC-501:30 exists twice. A correct system must choose policy: reject the input, shift forward, shift backward, choose the earlier occurrence, choose the later occurrence, or preserve enough context for a human to decide.
Make policy explicit in code
Python's timezone-aware arithmetic exposes the problem but does not magically choose business policy:
from datetime import datetime
from zoneinfo import ZoneInfo
zone = ZoneInfo("America/New_York")
ambiguous = datetime(2026, 11, 1, 1, 30, tzinfo=zone, fold=0)
later = datetime(2026, 11, 1, 1, 30, tzinfo=zone, fold=1)
print(ambiguous.utcoffset(), later.utcoffset())fold=0 is the first occurrence and fold=1 the second. That is representation, not business policy. For reminders, you may choose the first occurrence so users are not reminded late. For a once-nightly cleanup job, you may prefer exactly one execution and skip the second. Write the policy beside the scheduler, not in tribal memory.
Arithmetic is not commutative
Adding 24 hours is not always the same as adding one calendar day in local civil time:
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
z = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 9, 0, tzinfo=z)
print(start + timedelta(hours=24))If your intent is "same local time tomorrow," perform calendar arithmetic in local time with a library that understands date units. If your intent is "exactly 86,400 seconds later," perform elapsed arithmetic in UTC. Both are valid; confusing them is not.
The missing local hour does not mean time stops. UTC continues normally, and an event at 2026-03-08T07:30:00Z is valid even though the New York label 02:30 never occurred.
Field notes
- Never write tests only in UTC; UTC hides the entire class of civil-time discontinuities.
- Use transition dates from more than one hemisphere because southern zones shift in opposite months.
Australia/Lord_Howeshifts by 30 minutes, destroying assumptions that all DST offsets are one hour.- Decide user-facing wording for skipped times. Silently moving a meeting is sometimes worse than explaining that the requested time is invalid.
Was this lesson helpful?