Two Events at 1:30 AM: Handle Repeated Hours in Python
When clocks move back, one local clock time can refer to two different instants. Preserve UTC or the original offset before calculating elapsed time or deduplicating events.
By Prakhar Shrivastava · September 29, 2026 · Synthetic worked example
Why matching clock labels are not duplicate events
Imagine a US support platform records a ticket opening at 1:30 AM and closing at 1:30 AM in New York during the autumn clock change. A dashboard that subtracts the clock labels might show zero minutes. The actual duration can be one hour. This is a timestamp interpretation problem, not evidence of an unusually fast resolution.
The two instants below are November 1, 2026 at 05:30 UTC and 06:30 UTC. Converting them to America/New_York gives the same local hour and minute but different offsets. These are synthetic events, not observations from a real business.
Convert known instants with zoneinfo
Run this with Python 3.9 or later and an installed IANA timezone database. Python can use system timezone data or the first-party tzdata package. If ZoneInfo raises ZoneInfoNotFoundError, check your environment’s timezone-data dependency rather than substituting a guessed fixed offset.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
ny = ZoneInfo('America/New_York')
a = datetime(2026, 11, 1, 5, 30, tzinfo=timezone.utc)
b = datetime(2026, 11, 1, 6, 30, tzinfo=timezone.utc)
for t in (a, b):
local = t.astimezone(ny)
print(local.isoformat(), 'fold=', local.fold)
print('Elapsed minutes:', (b-a).total_seconds()/60)Expected output:
2026-11-01T01:30:00-04:00 fold= 0
2026-11-01T01:30:00-05:00 fold= 1
Elapsed minutes: 60.0The offset is part of the timestamp’s meaning. Python marks the second occurrence with fold=1 when converting from UTC. The example subtracts the UTC instants to measure elapsed time. For general timezone-aware inputs, convert both endpoints to UTC before subtracting when the question is elapsed duration.
What if the CSV only says 2026-11-01 01:30?
That value plus the New York timezone is insufficient to choose the occurrence. Adding a timezone label does not recover missing information. Ask for the original UTC timestamp, numeric offset, source sequence, or other reliable evidence. If the source cannot resolve it, flag the row as ambiguous and document how it is excluded or handled.
Do not choose fold=0 for every record merely because it is the default. That would impose an unsupported assumption on the second hour. Likewise, do not remove one row because both have the same local timestamp: separate event IDs may represent two valid actions.
Build an hourly report without losing the repeated hour
Keep a stable UTC timestamp as the event-time key and retain the reporting timezone separately. Label repeated local buckets with their offset, such as 01:00 −04:00 and 01:00 −05:00, if the report needs separate elapsed-hour buckets. Grouping only by a formatted local hour combines both occurrences. That may be intentional for a clock-hour profile, but it answers a different question from an elapsed-hour volume chart.
For UK or European reporting, use the relevant IANA zone, such as Europe/London or Europe/Berlin. Do not copy the New York transition date or offset into those reports. Timezone rules vary by place and can change; keep the runtime’s timezone data current and test the relevant boundary.
Validation before sharing a duration metric
- Check that both endpoints refer to real, identifiable instants and that closing follows opening in UTC.
- Preserve separate event IDs when local display labels match.
- Test both repeated-hour occurrences, ordinary dates, and the spring-forward gap.
- Distinguish elapsed duration from business-hours duration; this example does not exclude nights or weekends.
For spring-forward times that never occurred locally, attaching tzinfo alone is not a complete validation strategy. Prefer source timestamps that already identify instants, then convert for display.
Reference: Python zoneinfo documentation. Continue with daily-user reporting across timezones and parsing US and UK date formats. Date-format ambiguity and repeated-hour ambiguity require different checks.