Two 1:30 AMs: report time without losing the actual instant

Two 1:30 AMs: report time without losing the actual instant

Wenlo Posted on Nov 16 AI-assisted An event report can show 1:30 AM twice without containing a duplicate. During the autumn daylight-saving transition in New York, the local clock repeats an hour. Store the actual instants with offsets, then derive the local display and reporting date. Run the example All illustrative records are fictional. Save the following as timezone_report.py. The complete script was run in PythonIDE on iPhone with America/New_York time-zone data available; the screenshots show the actual source and output. from datetime import datetime, timezone from collections import Counter from zoneinfo import ZoneInfo, ZoneInfoNotFoundError # Fictional UTC events across a daylight-saving transition. records = [ ('A', '2026-11-01T03:30:00+00:00'), ('B', '2026-11-01T05:30:00+00:00'), ('C', '2026-11-01T06:30:00+00:00'), ('D', '2026-11-01T07:15:00+00:00'), ] def report(): try: zone = ZoneInfo('America/New_York') except ZoneInfoNotFoundError: print('Time-zone data unavailable in this runtime.') print('Cannot verify this regional conversion here.') return converted = [] print('FICTIONAL EVENT TIMES') for name, text in records: instant = datetime.fromisoformat(text) assert instant.utcoffset() is not None local = instant.astimezone(zone) converted.append((name, instant, local)) print(name, local.strftime('%Y-%m-%d %H:%M %Z'), f'offset={local.utcoffset()}, fold={local.fold}') days = Counter(local.date() for _, _, local in converted) print('\nEVENTS BY LOCAL DATE') for day, count in sorted(days.items()): print(day.isoformat(), count) first, second = converted[1], converted[2] # Compare instants in UTC, not repeated wall-clock text. elapsed = (second[1] - first[1]).total_seconds() / 60 print('\nBetween the two 01:30 events:', elapsed, 'minutes') assert first[2].hour == second[2].hour == 1 assert first[2].fold == 0 and second[2].fold == 1 assert elapsed == 60 for _, instant, local in converted: assert local.astimezone(timezone.utc) == instant print('UTC round-trip checks passed.') report() Enter fullscreen mode Exit fullscreen mode Understand the checks and their limits The fixture starts with explicit UTC offsets. astimezone converts the instant; replacing tzinfo would only relabel a wall-clock value and is not a conversion. The two 01:30 observations should have different offsets and fold values, while their UTC timestamps remain one hour apart. The earliest event belongs to October 31 locally, even though every UTC input is dated November 1. The actual iPhone run displayed B at 01:30 EDT with fold=0 and C at 01:30 EST with fold=1. Their elapsed time was 60 minutes. The local-date report counted one event on October 31 and three on November 1, and every UTC round-trip assertion passed. UTC round-trip assertions check that display conversion did not change the instant. The sample catches missing regional time-zone data and reports that limitation instead of inventing an offset. Zone rules come from the installed database and can change; this example is not a scheduler for ambiguous local input. Try one change Group by the UTC date instead and compare totals. Then explain what extra information you would need if someone supplied only “2026-11-01 01:30” with no offset. I build PythonIDE, a Python coding app for iPhone and iPad. The example is intended to teach a workflow you can inspect and adapt. AI assisted with drafting and translation. The example was checked in PythonIDE. PythonIDE on the App Store

Original Source

Read the full article at Dev →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.