How Daylight Saving Time Causes Duplicate and Missing Hours
Adding 86400 seconds to a local calendar event fails twice every year in countries that observe Daylight Saving Time (DST). On transition weekends, a local 9:00 AM daily alarm shifts to 8:00 AM or 10:00 AM because not every civil day lasts 24 hours.
Two DST Edge Cases That Break Software
- Spring Forward (23-Hour Day): When clocks jump from 1:59:59 AM directly to 3:00:00 AM, the local hour between 2:00 AM and 2:59 AM never exists. Scheduling a job at 2:30 AM causes it to skip or crash.
- Fall Back (25-Hour Day): When clocks turn backward from 1:59:59 AM to 1:00:00 AM, the hour from 1:00 AM to 1:59 AM happens twice. Storing a local wall string like
2026-11-01 01:30:00without an offset leaves software guessing whether it happened the first or second time.
How to Fix DST Bugs in Python and Databases
Never compute recurring civil calendar events by adding 86400 to a Unix timestamp. Instead, store the local wall time plus the IANA timezone name, then recalculate the Unix integer for each date using a timezone-aware library. In Python, the fold attribute (introduced in PEP 495) distinguishes between the first (fold=0) and second (fold=1) pass of a repeated local hour.
from datetime import datetime
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
first_pass = datetime(2026, 11, 1, 1, 30, tzinfo=ny, fold=0)
second_pass = datetime(2026, 11, 1, 1, 30, tzinfo=ny, fold=1)
print(int(first_pass.timestamp()), int(second_pass.timestamp()))
# Difference is exactly 3600 seconds