Repository navigation
TimedRotatingFileHandler misnames backups around DST changes #160433
Description
Bug report
Summary
With local time (utc=False), TimedRotatingFileHandler can give a backup the wrong date or hour when a daylight saving time (DST) change is involved. For fixed-duration rollovers such as when='H' and when='D', the affected interval spans the change. For when='midnight', a record logged well after the scheduled rollover can also cause the wrong name. If the computed name already exists, the handler skips that rotation, advances the next rollover time, and later resumes rotating. Records from separate intervals can then end up in the same backup.
Reproduction Code
Unix only (time.tzset() and the America/New_York timezone are required).
Hourly rollover across the start of DST:
import logging, logging.handlers, os, tempfile, time
from unittest import mock
os.environ["TZ"] = "America/New_York"
time.tzset()
d = tempfile.mkdtemp()
fn = os.path.join(d, "app.log")
start = time.mktime((2026, 3, 7, 23, 0, 0, 0, 0, -1))
now = [start]
with mock.patch("time.time", lambda: now[0]):
h = logging.handlers.TimedRotatingFileHandler(fn, when="H", delay=True)
for i in range(6):
now[0] = start + i * 3600
msg = time.strftime("%H:%M%Z", time.localtime(now[0]))
h.emit(logging.makeLogRecord({"msg": msg}))
h.close()
for name in sorted(os.listdir(d)):
with open(os.path.join(d, name)) as f:
print(name, f.read().split())Midnight rollover when the first record on the day DST ends arrives after the change:
import logging, logging.handlers, os, tempfile, time
from unittest import mock
os.environ["TZ"] = "America/New_York"
time.tzset()
d = tempfile.mkdtemp()
fn = os.path.join(d, "app.log")
ts = lambda *args: time.mktime(args + (0, 0, -1))
now = [ts(2026, 10, 30, 8, 0, 0)]
with mock.patch("time.time", lambda: now[0]):
h = logging.handlers.TimedRotatingFileHandler(fn, when="midnight", delay=True)
for day in [(2026, 10, 30), (2026, 10, 31), (2026, 11, 1),
(2026, 11, 2), (2026, 11, 3)]:
for hour in (9, 18):
now[0] = ts(*day, hour, 0, 0)
msg = time.strftime("%m-%d_%H:%M%Z", time.localtime(now[0]))
h.emit(logging.makeLogRecord({"msg": msg}))
h.close()
for name in sorted(os.listdir(d)):
with open(os.path.join(d, name)) as f:
print(name, f.read().split())Sparse records with a 23:30 daily rollover:
import datetime, logging, logging.handlers, os, tempfile, time
from unittest import mock
os.environ["TZ"] = "America/New_York"
time.tzset()
d = tempfile.mkdtemp()
fn = os.path.join(d, "app.log")
ts = lambda *args: time.mktime(args + (0, 0, -1))
now = [ts(2026, 3, 1, 12, 0, 0)]
with mock.patch("time.time", lambda: now[0]):
h = logging.handlers.TimedRotatingFileHandler(
fn, when="midnight", atTime=datetime.time(23, 30), delay=True)
for args in [(2026, 3, 1, 12, 0, 0), (2026, 4, 1, 12, 0, 0),
(2026, 10, 1, 12, 0, 0), (2026, 12, 1, 12, 0, 0)]:
now[0] = ts(*args)
msg = time.strftime("%m-%d_%H:%M%Z", time.localtime(now[0]))
h.emit(logging.makeLogRecord({"msg": msg}))
h.close()
for name in sorted(os.listdir(d)):
with open(os.path.join(d, name)) as f:
print(name, f.read().split())Actual Behavior
Hourly rollover saves the 01:00EST interval as _03. The next interval also maps to _03, so that rotation is skipped. Rotation resumes later, with 03:00EDT and 04:00EDT together in _04:
app.log ['05:00EDT']
app.log.2026-03-07_23 ['23:00EST']
app.log.2026-03-08_00 ['00:00EST']
app.log.2026-03-08_03 ['01:00EST']
app.log.2026-03-08_04 ['03:00EDT', '04:00EDT']
Midnight rollover skips the rotation on November 1 because it computes the already used October 30 name for the October 31 interval. The next rotation puts October 31 and November 1 in _11-01:
app.log ['11-03_09:00EST', '11-03_18:00EST']
app.log.2026-10-30 ['10-30_09:00EDT', '10-30_18:00EDT']
app.log.2026-11-01 ['10-31_09:00EDT', '10-31_18:00EDT', '11-01_09:00EST', '11-01_18:00EST']
app.log.2026-11-02 ['11-02_09:00EST', '11-02_18:00EST']
With sparse records and atTime, the March 1 record is saved under March 1 rather than February 28:
app.log ['12-01_12:00EST']
app.log.2026-03-01 ['03-01_12:00EST']
app.log.2026-03-31 ['04-01_12:00EDT']
app.log.2026-09-30 ['10-01_12:00EDT']
Expected Behavior
The doRollover() docstring says a backup is named for the start of its interval. In the hourly example, the 01:00EST record should be in _01, followed by separate _03 and _04 backups. In the midnight example, each day from October 30 through November 2 should have its own backup. In the sparse atTime example, the first backup should be dated February 28.
Root Cause
doRollover() starts with t = self.rolloverAt - self.interval, then compares the DST state of t with that of the record that triggered rotation. When they differ, it shifts the time used for the filename by one hour. This adjustment runs for every when value. For fixed-duration rollovers, t is already the interval start, so the shift can misname it. For midnight and weekly rollovers, the triggering record can arrive long after rolloverAt; its DST state need not match the DST state at the scheduled boundary.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
lpyu001 commentedon Oct 23, 2026
I'll work on a fix for this issue.
lpyu001 commentedon Oct 23, 2026
Due to the PR limit recently introduced by the community, I'm currently unable to open a new PR. If anyone is interested in fixing this issue, please feel free to submit a PR.