Python zoneinfo and fold: distinguish two identical local times
Resolve a repeated local hour with Python zoneinfo. Compare fold values, recover both UTC instants, and detect ambiguous or nonexistent wall-clock inputs.
- Author
- Salon Rai
- 2 min read
A log entry dated November 1, 2020 at 01:30 in Los Angeles can describe two different instants. The clocks went back that morning, so the wall clock reached 01:30 twice. Python’s zoneinfo represents the two occurrences with fold=0 and fold=1. Convert them to Coordinated Universal Time (UTC) before measuring elapsed time or assigning an event identity.
Construct the two occurrences
This example uses Python 3.9 or newer and the IANA time zone database. On systems that do not provide a zone database, install the Python tzdata package. Use an area name such as America/Los_Angeles so historical transitions come from the zone rules. A fixed offset alone cannot tell you when the region changed clocks. Python zoneinfo documentation
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
la = ZoneInfo("America/Los_Angeles")
first = datetime(2020, 11, 1, 1, 30, tzinfo=la, fold=0)
second = first.replace(fold=1)
for local in (first, second):
print(local.isoformat(), local.astimezone(timezone.utc).isoformat())
| Occurrence | Local timestamp | UTC timestamp |
|---|---|---|
| First | 2020-11-01 01:30 −07:00 | 2020-11-01 08:30 +00:00 |
| Second | 2020-11-01 01:30 −08:00 | 2020-11-01 09:30 +00:00 |
A request at the first 01:30 and a response at the second 01:30 are an hour apart. If all you received was a local date, time and region, the available fields carry insufficient information to select the actual event. Look for a recorded offset, a UTC timestamp or a documented business rule that distinguishes the occurrences.
Compare instants where the clock actually advanced
With the same tzinfo object, Python’s datetime comparison and subtraction use wall-time semantics for these two values. The following continuation surprises developers who use a local timestamp as a dictionary key. The two keys collapse into one. Converting to UTC first preserves both events. Python datetime rules
from datetime import timedelta
assert first == second
assert second - first == timedelta(0)
assert len({first, second}) == 1
earlier = first.astimezone(timezone.utc)
later = second.astimezone(timezone.utc)
assert later - earlier == timedelta(hours=1)
assert len({earlier, later}) == 2
Calling replace(tzinfo=timezone.utc) on the local time would keep the wall-clock digits and relabel them as UTC. Use astimezone when you need to preserve an existing instant. Attaching a zone with replace is useful earlier, when interpreting a naive local input whose region is known.
Validate a time entered in a form
Clocks also move forward, leaving some local readings with no corresponding instant. Merely constructing a datetime with fold=1 doesn’t validate the input. To enumerate valid interpretations, try both fold values, convert each candidate to UTC, and convert back. Retain the instants that recover the requested wall-clock fields. Deduplicate in UTC so ordinary times appear once. PEP 495 on folds and gaps
def possible_instants(wall, zone):
if wall.tzinfo is not None:
raise ValueError("Expected a local time without a zone")
instants = set()
for fold in (0, 1):
candidate = wall.replace(tzinfo=zone, fold=fold)
utc = candidate.astimezone(timezone.utc)
restored = utc.astimezone(zone).replace(tzinfo=None)
if restored == wall:
instants.add(utc)
return sorted(instants)
for wall in (
datetime(2020, 11, 1, 1, 30),
datetime(2020, 11, 1, 12, 0),
datetime(2020, 3, 8, 2, 30),
):
print(wall, len(possible_instants(wall, la))) # 2, 1, 0
For a booking form, two candidates can become two choices with visible UTC offsets. For a nonexistent time, ask for another reading or display the scheduling system’s documented adjustment. Automatically moving every invalid time forward by one hour assumes a transition size and a business policy. Neither follows from the input field.
Store the fields that answer the next question
A completed transaction usually needs an instant. Store UTC and keep the original zone or offset when it matters for receipts or auditing. A recurring appointment such as “09:00 every Thursday in Los Angeles” needs the wall-time rule and the region. Adding a fixed number of seconds to last week’s UTC instant can shift the appointment to a different local hour after a transition.
Next steps
Keep regression cases for the repeated hour, an ordinary hour and the spring gap. Assert the expected number of instants and the exact UTC offsets. When a UI displays those instants, also check how the date looks in the reader’s zone. The React time zone walkthrough shows a separate display bug that can occur even after storage is correct.

Written by