Sub topic 10
Date arithmetic across daylight saving changes
This chapter assumes you know timedelta, aware datetimes, ZoneInfo, and astimezone. It covers what happens when you add a duration to a datetime in a zone that changes its clock.
Two meanings of "one day later"
"Add one day" can mean two different things:
- Wall-clock arithmetic: the same clock reading on the next calendar day. Noon today becomes noon tomorrow.
- Absolute arithmetic: exactly 24 hours of elapsed time later.
On most days these agree. On a day when the clock jumps, they differ by the size of the jump, usually one hour.
Python's + on an aware datetime does wall-clock arithmetic. It adds the timedelta to the local fields and keeps the same tzinfo. It does not look at UTC offsets.
Seeing the difference
New York moves its clocks forward at 02:00 on 2026-03-08, so that day has only 23 hours.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 12, 0, tzinfo=tz)
wall_clock = start + timedelta(days=1)
absolute = (start.astimezone(timezone.utc) + timedelta(days=1)).astimezone(tz)
print(wall_clock)
print(absolute)The output is:
2026-03-08 12:00:00-04:00
2026-03-08 13:00:00-04:00The wall-clock result keeps 12:00, and the offset changes from -05:00 to -04:00 by itself. The absolute result goes through UTC, where no clocks jump. It lands at 13:00 local time because 24 real hours passed.
Subtracting is also affected
Subtraction has a trap. If both datetimes share the same tzinfo object, Python ignores their offsets and subtracts the local fields. If their tzinfo objects differ, it uses the real UTC offsets.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 12, 0, tzinfo=tz)
later = start + timedelta(days=1)
print(later - start)
elapsed = later.astimezone(timezone.utc) - start.astimezone(timezone.utc)
print(elapsed)The output is:
1 day, 0:00:00
23:00:00The first line is a wall-clock difference. Only 23 hours really passed. Converting both values to UTC first gives the true elapsed time.
Choosing between the two
| You want to say | Use | Example |
|---|---|---|
| "Same time tomorrow" | Wall-clock: `dt + timedelta(days=1)` | A daily 09:00 meeting, a store opening |
| "In 24 hours" | Absolute: convert to UTC, add, convert back | A token that expires in 24 hours, a retry timer |
| "How long between these?" | Subtract in UTC | Job duration, time since last login |
Ask whether a person's calendar or a physical clock drives the rule. Calendar rules (meetings, reminders) are wall-clock. Physical rules (expiry, rate limits) are absolute.
Landing in a gap or a fold
Wall-clock arithmetic can produce a local time that does not exist or that occurs twice. For example, adding one day to 02:30 on 2026-03-07 gives 02:30 on 2026-03-08, which falls in the skipped hour.
ZoneInfo does not raise an error here. It follows the PEP 495 rules for the fold attribute. Under those rules a nonexistent time with fold=0 is read with the offset from before the change. An ambiguous time is read as the first occurrence unless you set fold=1. The result is usually a valid instant, but it may not be the hour you meant.
To get a clean value, round-trip through UTC. This converts the result to a real instant and back to a valid local time.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 2, 30, tzinfo=tz)
naive_sum = start + timedelta(days=1)
normalized = naive_sum.astimezone(timezone.utc).astimezone(tz)
print(naive_sum)
print(normalized)The first line prints the nonexistent 02:30 with an offset of -05:00. The second prints 03:30 with an offset of -04:00, which is a time that really occurred.
Summary of the rules
+ timedeltaon an aware datetime moves the local clock fields and does not track real elapsed time.- For exact durations, do the arithmetic in UTC and convert back only for display.
- Subtract in UTC when you need elapsed time.
- Normalize with a UTC round trip when wall-clock arithmetic may land in a gap.
Exercise
A cache entry is created at 2026-11-01 00:30 in America/New_York (EDT, before clocks go back at 02:00). It must expire exactly 3 hours later in real time. Should you compute the expiry with created + timedelta(hours=3) on the local datetime, or through UTC? Why?
Show answer
Compute it through UTC. The local clock repeats the 01:00 hour that night, so 3 real hours after 00:30 EDT is 02:30 EST. Local addition gives 03:30 on the wall clock, which is 4 real hours later. Converting to UTC, adding 3 hours, and converting back gives the correct 02:30 EST.