Resolve Repeated Local Times with fold
Core idea
When a clock moves backward, a local clock reading can occur twice. With ZoneInfo, fold=0 selects the earlier occurrence in an overlap and fold=1 the later one. Convert from UTC to avoid ambiguity when the instant is already known. Forward jumps can create nonexistent local times; simply constructing a datetime does not validate that local reading.
from datetime import datetime, timezonefrom zoneinfo import ZoneInfoz = ZoneInfo("Europe/Stockholm")for fold in (0, 1): local = datetime(2024, 10, 27, 2, 30, tzinfo=z, fold=fold) print(local.astimezone(timezone.utc).isoformat())The repeated local clock reading represents two different UTC instants.
Measure elapsed time across overlap
from datetime import datetime, timezonefrom zoneinfo import ZoneInfoz = ZoneInfo("Europe/Stockholm")a = datetime(2024, 10, 27, 2, 30, tzinfo=z, fold=0)b = a.replace(fold=1)print((b-a).total_seconds())print((b.astimezone(timezone.utc)-a.astimezone(timezone.utc)).total_seconds())Subtraction with the same tzinfo ignores fold and offset changes; conversion to UTC measures the elapsed hour.
Common mistake
Do not assume fold fixes nonexistent local times. Validate user-entered local times by a deliberate policy, including round trips and explicit ambiguity handling. The examples demonstrate a historical overlap, not a complete scheduler.
Practice
Convert 00:30 and 01:30 UTC on 2024-10-27 into Europe/Stockholm and inspect fold and offset.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ambiguous local times
Check your understanding of ambiguous times and fold.
What is printed?
from datetime import datetime,timezonefrom zoneinfo import ZoneInfoa=datetime(2024,10,27,1,30,tzinfo=timezone.utc)print(a.astimezone(ZoneInfo("Europe/Stockholm")).fold)What is printed?
from datetime import datetimefrom zoneinfo import ZoneInfoa=datetime(2024,10,27,2,30,tzinfo=ZoneInfo("Europe/Stockholm"))print(a==a.replace(fold=1))How should elapsed time across a DST overlap be calculated?
Does constructing a zone-aware datetime prove a local time exists?