ZoneInfo fold Disambiguates DST Fall-Back
Resolve ambiguous fall-back clock times with datetime fold=0 and fold=1 under ZoneInfo, and detect nonexistent spring-forward gaps via UTC round-trips.
Code
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
# Fall-back overlap on 2026-11-01: 01:30 occurs twice (EDT -04:00 then EST -05:00)
first_pass = datetime(2026, 11, 1, 1, 30, tzinfo=ny, fold=0)
second_pass = datetime(2026, 11, 1, 1, 30, tzinfo=ny, fold=1)
print("fold=0:", first_pass.isoformat(), "-> UTC:", first_pass.astimezone(timezone.utc).isoformat())
print("fold=1:", second_pass.isoformat(), "-> UTC:", second_pass.astimezone(timezone.utc).isoformat())
print("gap hours:", (second_pass.astimezone(timezone.utc) - first_pass.astimezone(timezone.utc)).seconds // 3600)
# Spring-forward gap on 2026-03-08: 02:30 never exists on local clocks
def is_valid_local_wall_time(naive_dt: datetime, tz: ZoneInfo) -> bool:
aware = naive_dt.replace(tzinfo=tz)
roundtrip = aware.astimezone(timezone.utc).astimezone(tz)
return roundtrip.replace(tzinfo=None) == naive_dt
for candidate in (datetime(2026, 3, 8, 1, 45), datetime(2026, 3, 8, 2, 30)):
print(candidate.isoformat(), "exists:", is_valid_local_wall_time(candidate, ny))Output
Captured from a sandboxed run of this snippet in the build pipeline.
fold=0: 2026-11-01T01:30:00-04:00 -> UTC: 2026-11-01T05:30:00+00:00
fold=1: 2026-11-01T01:30:00-05:00 -> UTC: 2026-11-01T06:30:00+00:00
gap hours: 1
2026-03-08T01:45:00 exists: True
2026-03-08T02:30:00 exists: FalseWalkthrough notes — ZoneInfo fold Disambiguates DST Fall-Back
ZoneInfo fold Disambiguates DST Fall-Back reduces to the function is_valid_local_wall_time.
for candidate in (datetime(2026, 3, 8, 1, 45), datetime(2026, 3, 8, 2, 30)) executes element-by-element transformation across input values.
Watch out for
- Relies solely on core standard library primitives so copy-paste runs reliably.
Applying it
Central callable declaration specifies def is_valid_local_wall_time(naive_dt: datetime, tz: ZoneInfo) -> bool
Output lines are prefixed (fold=0:, fold=1:, gap hours:), letting console output reflect every executed branch directly.
is_valid_local_wall_time is the public entry point; everything else in the file exists to support it.
Statements that matter
def is_valid_local_wall_time(naive_dt: datetime, tz: ZoneInfo) -> bool:
aware = naive_dt.replace(tzinfo=tz)
roundtrip = aware.astimezone(timezone.utc).astimezone(tz)
return roundtrip.replace(tzinfo=None) == naive_dt
Interactive Tools for This Topic
FAQ
- Does this work in a bare project with no dependencies file?
- Yes — a bare project runs it as-is, since nothing needs declaring. Every import resolves inside the standard library — `datetime`, `zoneinfo`. The file defines `is_valid_local_wall_time` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.