Sub topic 10
Date arithmetic across daylight saving changes
This chapter assumes you know the datetime, timedelta and ZoneInfo basics from the earlier chapters. You should also know what naive and aware datetimes are, and how astimezone and fold work. It focuses on one question: what does "one day later" mean when the clocks change in between?
Two kinds of "one day later"
A timedelta(days=1) always holds 86,400 seconds. A calendar day in a zone with daylight saving time (DST) can last 23, 24 or 25 hours. So "tomorrow at noon" and "24 hours from now" are different requests on a transition day.
- Wall-clock arithmetic moves the fields shown on the clock. Adding one day to noon gives noon the next day, however many real seconds that takes.
- Absolute arithmetic moves along the timeline. Adding 24 hours means exactly 86,400 elapsed seconds, and the local clock reading is whatever results.
Python's default for an aware datetime with a ZoneInfo is wall-clock arithmetic. The next example shows both approaches across the 2026 spring-forward change in New York, which happens on March 8 at 02:00.
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)This prints:
2026-03-08 12:00:00-04:00
2026-03-08 13:00:00-04:00The wall-clock result keeps 12:00 but its offset changed from -05:00 to -04:00. Only 23 real hours passed. The absolute result is exactly 24 hours later, which reads 13:00 on the local clock.
How Python computes the result
Adding a timedelta to an aware datetime does not look at the time zone at all. Python adds the days, seconds and microseconds to the date and time fields, then keeps the same tzinfo. The UTC offset is looked up again from the new fields, and that lookup is why the offset changed from -05:00 to -04:00 above.
Subtraction has a related rule that surprises many people:
- If both datetimes have the same
tzinfoobject, Python subtracts the naive fields and ignores offsets. - If their
tzinfoobjects differ, Python converts both to UTC first.
ZoneInfo("America/New_York") returns a cached object, so two calls give the identical tzinfo. Two datetimes in the same zone therefore always use the first rule.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
before = datetime(2026, 3, 7, 12, 0, tzinfo=tz)
after = datetime(2026, 3, 8, 12, 0, tzinfo=tz)
print(after - before)
elapsed = after.astimezone(timezone.utc) - before.astimezone(timezone.utc)
print(elapsed)The first line prints 1 day, 0:00:00 and the second prints 23:00:00. The first is a calendar difference and the second is the real elapsed time.
Which one should you use?
The right choice depends on what the time means to the people using it.
| Situation | Intent | Approach |
|---|---|---|
| Daily alarm at 07:00 | Same local time each day | Wall-clock |
| Recurring meeting every Tuesday at 10:00 | Same local time | Wall-clock |
| Subscription renewal "in 30 days" | Calendar date | Wall-clock |
| Cache expires in 24 hours | Fixed duration | Absolute |
| Timeout, retry delay, lease | Fixed duration | Absolute |
| Measuring how long a job ran | Elapsed time | Absolute (subtract in UTC) |
The fall-back case
The reverse transition behaves the same way, in the other direction. On November 1, 2026 the clocks in New York go back at 02:00 EDT to 01:00 EST, so that calendar day lasts 25 hours.
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
start = datetime(2026, 10, 31, 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)
print(wall_clock.astimezone(timezone.utc) - start.astimezone(timezone.utc))The output is:
2026-11-01 12:00:00-05:00
2026-11-01 11:00:00-05:00
25:00:00The wall-clock result is noon again, but 25 hours have passed. The absolute result is 11:00 local time.
Landing in a gap or a repeat
Wall-clock arithmetic can produce a time that does not exist, or one that exists twice. Python does not raise an error in either case. It picks an interpretation using fold:
- Gap (nonexistent time):
fold=0uses the offset from before the transition, andfold=1uses the offset from after it. - Repeat (ambiguous time):
fold=0means the first occurrence andfold=1means the second.
timedelta addition always returns a result with fold=0. Python also does not tell you when a time is invalid. The following helper classifies a datetime:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
def classify(dt):
"""Return 'gap', 'ambiguous' or 'ok' for an aware datetime with a ZoneInfo."""
naive = dt.replace(tzinfo=None)
round_trip = dt.astimezone(timezone.utc).astimezone(dt.tzinfo).replace(tzinfo=None)
if round_trip != naive:
return "gap"
if dt.replace(fold=0).utcoffset() != dt.replace(fold=1).utcoffset():
return "ambiguous"
return "ok"
tz = ZoneInfo("America/New_York")
print(classify(datetime(2026, 3, 8, 2, 30, tzinfo=tz)))
print(classify(datetime(2026, 11, 1, 1, 30, tzinfo=tz)))
print(classify(datetime(2026, 11, 26, 9, 0, tzinfo=tz)))This prints gap, ambiguous and ok.
The round-trip test works because a nonexistent time changes when it goes through UTC and back. The 02:30 reading on March 8 comes back as 03:30. Ambiguous times survive the round trip but have different offsets for fold=0 and fold=1.
Resolving a gap
If you add one day to 02:30 on March 7 and land at 02:30 on March 8, that time never happens. The usual convention is to move forward by the size of the gap, so the result is 03:30 EDT. Converting through UTC does exactly that:
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
def same_wall_time(start, days):
"""Add calendar days, keeping the wall-clock time and shifting forward over a gap."""
candidate = start + timedelta(days=days)
return candidate.astimezone(timezone.utc).astimezone(start.tzinfo)
tz = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 2, 30, tzinfo=tz)
raw = start + timedelta(days=1)
print(raw)
print(raw.astimezone(timezone.utc))
print(same_wall_time(start, 1))The output is:
2026-03-08 02:30:00-05:00
2026-03-08 07:30:00+00:00
2026-03-08 03:30:00-04:00The raw result looks valid but carries the wrong offset. It claims a -05:00 offset that no longer applies at that moment. The 07:30 UTC it maps to is 03:30 EDT. Always normalize if you plan to print, store or compare the result.
Resolving a repeat
For ambiguous times, fold=0 picks the first occurrence. That is usually the right default for a schedule, since the job runs once, at the earlier of the two moments. If a user entered the time, you may need to ask which one they meant. The two readings are an hour apart in real time, but same-zone subtraction hides that:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
tz = ZoneInfo("America/New_York")
first = datetime(2026, 11, 1, 1, 30, tzinfo=tz)
second = first.replace(fold=1)
print(second - first)
print(second.astimezone(timezone.utc) - first.astimezone(timezone.utc))The first line prints 0:00:00 and the second prints 1:00:00.
Practical patterns
Keep UTC for storage and durations
Do duration math in UTC. Store the UTC value, and also store the zone name when you need to recreate the local meaning later:
from datetime import datetime, timedelta, timezone
expires_at = datetime.now(timezone.utc) + timedelta(hours=24)
print(expires_at.tzinfo)This prints UTC. A UTC value can never fall in a gap or a repeat, so arithmetic on it is always exact.
Recurring local events
For recurring events, store the local wall time and the zone name, not a UTC instant. Generate each occurrence by combining the date and time in the zone, and normalize each one:
from datetime import date, datetime, time, timedelta, timezone
from zoneinfo import ZoneInfo
def occurrences(first_day, at, zone, count):
tz = ZoneInfo(zone)
for n in range(count):
d = first_day + timedelta(days=n)
local = datetime.combine(d, at, tzinfo=tz)
yield local.astimezone(timezone.utc).astimezone(tz)
for dt in occurrences(date(2026, 3, 7), time(2, 30), "America/New_York", 3):
print(dt)The output is:
2026-03-07 02:30:00-05:00
2026-03-08 03:30:00-04:00
2026-03-09 02:30:00-04:00Building each occurrence from its own date, rather than repeatedly adding to the previous one, avoids carrying a shifted result forward. After the gap, the schedule returns to 02:30 on March 9 instead of staying at 03:30.
Calendar units that timedelta cannot express
timedelta has no months or years, so "one month later" needs date logic instead. Compute the new date with date arithmetic, clamp the day to the length of the month, then combine it with the wall time and the zone as in the generator above. The gap and repeat rules still apply to the combined result.
Common mistakes
- Adding
timedelta(hours=24)to a local datetime and expecting "same time tomorrow". Near a transition, the offset changes and the clock reading shifts by an hour. - Printing a raw wall-clock result in a gap. The time looks valid but has a stale offset, as
rawshowed above. - Subtracting same-zone datetimes to measure elapsed time.
- Ignoring
foldwhen parsing user input for local times, such as "1:30 AM on November 1". - Storing only a UTC instant for a recurring local event. A change in the zone's rules or a DST shift then moves the event on the local clock.
Exercises
Exercise
A cache entry is created at 2026-03-07 12:00 in America/New_York and must expire exactly 24 hours later. Which approach is correct, and what local time does the expiry show?
Show answer
Use absolute arithmetic: convert to UTC, add timedelta(hours=24), and convert back for display. The expiry shows 2026-03-08 13:00-04:00, because the spring-forward change makes the local clock jump by an hour. Wall-clock arithmetic would give 12:00 local time, which is only 23 real hours later.
Exercise
Using the samewalltime helper from this chapter, what does samewalltime(datetime(2026, 11, 1, 1, 30, tzinfo=ZoneInfo("America/New_York")), 0) return, and which fold value does the result have?
Show answer
It returns 2026-11-01 01:30:00-04:00 with fold=0. The time is ambiguous, adding timedelta(days=0) yields fold=0, and the UTC round trip keeps the first occurrence (EDT). To select the second occurrence you would need fold=1, which gives 01:30-05:00.