Handle repeated wall-clock times around daylight-saving transitions.
How It Works
At a fall-back transition, one local time can occur twice with different UTC offsets. The fold attribute distinguishes the earlier occurrence (fold=0) from the later one (fold=1). Nonexistent spring-forward times require explicit application handling.
Step-by-Step Example
from datetime import datetime
from zoneinfo import ZoneInfo
new_york = ZoneInfo('America/New_York')
first_time = datetime(2026, 11, 1, 1, 30, tzinfo=new_york, fold=0)
second_time = datetime(2026, 11, 1, 1, 30, tzinfo=new_york, fold=1)
print(first_time.utcoffset() != second_time.utcoffset())
Understanding the Example
- The expression "ZoneInfo('America/New_York')" supplies the value stored as 'new_york'. This value is then used or checked by the later statements.
- The expression 'datetime(2026, 11, 1, 1, 30, tzinfo=new_york, fold=0)' supplies the value stored as 'first_time'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
True
Where This Fits in a Real Program
For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.
Practice: Try It Yourself
Prefer storing aware UTC instants and retaining the original named timezone for recurring events.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Scheduling repeated local times without recording which occurrence was intended.
- Store both UTC instants for a repeated local time during a fall-back transition.
Key Takeaways
- Handle repeated wall-clock times around daylight-saving transitions.
- At a fall-back transition, one local time can occur twice with different UTC offsets.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.