Explain DST fold ambiguity with two explicit instants
Explain DST fold ambiguity with two explicit instants
During the 2020 autumn DST transition in Los Angeles, the local clock displayed 01:30 twice. Starting with explicit UTC instants removes any ambiguity: 08:30 UTC converts to 2020-11-01T01:30:00-07:00, while 09:30 UTC converts to 2020-11-01T01:30:00-08:00. They share the same wall-clock fields but have different offsets and fold values. fold=0 identifies the earlier occurrence and fold=1 the later occurrence.
The example derives both local values from UTC, then asserts their wall-clock portions match, their folds differ, and the source instants are one hour apart. That last assertion is made on the UTC values: comparisons or subtraction between aware datetimes that share a tzinfo object have rules that can be surprising around a fold. Treat the UTC instant, or another unambiguous stored representation, as the value used to order events.
ZoneInfo supplies IANA timezone rules, so this result depends on the available timezone database retaining those historical rules. It is not a promise about future political time changes, and a naive datetime(2020, 11, 1, 1, 30) alone cannot select either instant. zoneinfo was added in Python 3.9; on systems without system timezone data, its documented data-source configuration may require the tzdata package. Official zoneinfo documentation and fold behavior in datetime describe the model.
AI-assistance disclosure: AI helped draft this educational example; the assertions establish only these two named historical instants.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
los_angeles = ZoneInfo("America/Los_Angeles")
first_utc = datetime(2020, 11, 1, 8, 30, tzinfo=timezone.utc)
second_utc = datetime(2020, 11, 1, 9, 30, tzinfo=timezone.utc)
first_local = first_utc.astimezone(los_angeles)
second_local = second_utc.astimezone(los_angeles)
assert first_local.replace(tzinfo=None) == second_local.replace(tzinfo=None)
assert (first_local.fold, second_local.fold) == (0, 1)
assert second_utc - first_utc == timedelta(hours=1)
print(first_local.isoformat(), f"fold={first_local.fold}")
print(second_local.isoformat(), f"fold={second_local.fold}")
2020-11-01T01:30:00-07:00 fold=0
2020-11-01T01:30:00-08:00 fold=1
Comments
Post a Comment