Title: `TimedRotatingFileHandler` misnames backups around DST changes · Issue #160433 · python/cpython · GitHub
Open Graph Title: `TimedRotatingFileHandler` misnames backups around DST changes · Issue #160433 · python/cpython
X Title: `TimedRotatingFileHandler` misnames backups around DST changes · Issue #160433 · python/cpython
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 affec...
Open Graph 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 ...
X 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 ...
Opengraph URL: https://github.com/python/cpython/issues/160433
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`TimedRotatingFileHandler` misnames backups around DST changes","articleBody":"### Bug report\n\n### Summary\n\nWith 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.\n\n### Reproduction Code\n\nUnix only (`time.tzset()` and the `America/New_York` timezone are required). \n\nHourly rollover across the start of DST:\n\n```python\nimport logging, logging.handlers, os, tempfile, time\nfrom unittest import mock\n\nos.environ[\"TZ\"] = \"America/New_York\"\ntime.tzset()\n\nd = tempfile.mkdtemp()\nfn = os.path.join(d, \"app.log\")\nstart = time.mktime((2026, 3, 7, 23, 0, 0, 0, 0, -1))\nnow = [start]\nwith mock.patch(\"time.time\", lambda: now[0]):\n h = logging.handlers.TimedRotatingFileHandler(fn, when=\"H\", delay=True)\n for i in range(6):\n now[0] = start + i * 3600\n msg = time.strftime(\"%H:%M%Z\", time.localtime(now[0]))\n h.emit(logging.makeLogRecord({\"msg\": msg}))\nh.close()\nfor name in sorted(os.listdir(d)):\n with open(os.path.join(d, name)) as f:\n print(name, f.read().split())\n```\n\nMidnight rollover when the first record on the day DST ends arrives after the change:\n\n```python\nimport logging, logging.handlers, os, tempfile, time\nfrom unittest import mock\n\nos.environ[\"TZ\"] = \"America/New_York\"\ntime.tzset()\n\nd = tempfile.mkdtemp()\nfn = os.path.join(d, \"app.log\")\nts = lambda *args: time.mktime(args + (0, 0, -1))\nnow = [ts(2026, 10, 30, 8, 0, 0)]\nwith mock.patch(\"time.time\", lambda: now[0]):\n h = logging.handlers.TimedRotatingFileHandler(fn, when=\"midnight\", delay=True)\n for day in [(2026, 10, 30), (2026, 10, 31), (2026, 11, 1),\n (2026, 11, 2), (2026, 11, 3)]:\n for hour in (9, 18):\n now[0] = ts(*day, hour, 0, 0)\n msg = time.strftime(\"%m-%d_%H:%M%Z\", time.localtime(now[0]))\n h.emit(logging.makeLogRecord({\"msg\": msg}))\nh.close()\nfor name in sorted(os.listdir(d)):\n with open(os.path.join(d, name)) as f:\n print(name, f.read().split())\n```\n\nSparse records with a 23:30 daily rollover:\n\n```python\nimport datetime, logging, logging.handlers, os, tempfile, time\nfrom unittest import mock\n\nos.environ[\"TZ\"] = \"America/New_York\"\ntime.tzset()\n\nd = tempfile.mkdtemp()\nfn = os.path.join(d, \"app.log\")\nts = lambda *args: time.mktime(args + (0, 0, -1))\nnow = [ts(2026, 3, 1, 12, 0, 0)]\nwith mock.patch(\"time.time\", lambda: now[0]):\n h = logging.handlers.TimedRotatingFileHandler(\n fn, when=\"midnight\", atTime=datetime.time(23, 30), delay=True)\n for args in [(2026, 3, 1, 12, 0, 0), (2026, 4, 1, 12, 0, 0),\n (2026, 10, 1, 12, 0, 0), (2026, 12, 1, 12, 0, 0)]:\n now[0] = ts(*args)\n msg = time.strftime(\"%m-%d_%H:%M%Z\", time.localtime(now[0]))\n h.emit(logging.makeLogRecord({\"msg\": msg}))\nh.close()\nfor name in sorted(os.listdir(d)):\n with open(os.path.join(d, name)) as f:\n print(name, f.read().split())\n```\n\n### Actual Behavior\n\nHourly 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`:\n\n```text\napp.log ['05:00EDT']\napp.log.2026-03-07_23 ['23:00EST']\napp.log.2026-03-08_00 ['00:00EST']\napp.log.2026-03-08_03 ['01:00EST']\napp.log.2026-03-08_04 ['03:00EDT', '04:00EDT']\n```\n\nMidnight 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`:\n\n```text\napp.log ['11-03_09:00EST', '11-03_18:00EST']\napp.log.2026-10-30 ['10-30_09:00EDT', '10-30_18:00EDT']\napp.log.2026-11-01 ['10-31_09:00EDT', '10-31_18:00EDT', '11-01_09:00EST', '11-01_18:00EST']\napp.log.2026-11-02 ['11-02_09:00EST', '11-02_18:00EST']\n```\n\nWith sparse records and `atTime`, the March 1 record is saved under March 1 rather than February 28:\n\n```text\napp.log ['12-01_12:00EST']\napp.log.2026-03-01 ['03-01_12:00EST']\napp.log.2026-03-31 ['04-01_12:00EDT']\napp.log.2026-09-30 ['10-01_12:00EDT']\n```\n\n### Expected Behavior\n\nThe `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.\n\n### Root Cause\n\n`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.\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160564\n* gh-160577\n* gh-160578\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/lpyu001","@type":"Person","name":"lpyu001"},"datePublished":"2026-10-23T14:24:41Z","url":"https://github.com/python/cpython/issues/160433"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e9050f6e-5769-7ac9-2d64-c5f3d325d9ad |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD72:33943:106999C:15C62BA:6B064FF8 |
| html-safe-nonce | 12e8d4300fe81c385102092468a55ac7dc16096ef6d1976148f38ef53cd80691 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDcyOjMzOTQzOjEwNjk5OUM6MTVDNjJCQTo2QjA2NEZGOCIsInZpc2l0b3JfaWQiOiI2ODQzMTQ5MzY5OTIwMDgxOTEyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 63bbdb9f44e13eaae7c3345bbc7c951bfeb4588132530bf16250c951ced54c07 |
| hovercard-subject-tag | issue:5999583730 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/160433/issue_layout |
| twitter:image | https://opengraph.githubassets.com/18cc1da09c23ece9558c05ff418cd3d3523ae76ff83dc91c4469a61bb0ec4de4/python/cpython/issues/160433 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/18cc1da09c23ece9558c05ff418cd3d3523ae76ff83dc91c4469a61bb0ec4de4/python/cpython/issues/160433 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lpyu001 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width