Title: `zoneinfo`: pure-Python parser accepts a POSIX TZ footer with no STD offset (C rejects) · Issue #152212 · python/cpython · GitHub
Open Graph Title: `zoneinfo`: pure-Python parser accepts a POSIX TZ footer with no STD offset (C rejects) · Issue #152212 · python/cpython
X Title: `zoneinfo`: pure-Python parser accepts a POSIX TZ footer with no STD offset (C rejects) · Issue #152212 · python/cpython
Description: Bug description A POSIX TZ string requires an offset after the std abbreviation (e.g. EST5). When the offset is missing and the std field is just an abbreviation (AAA, A, AA, B, ...), the two zoneinfo implementations disagree: The C acce...
Open Graph Description: Bug description A POSIX TZ string requires an offset after the std abbreviation (e.g. EST5). When the offset is missing and the std field is just an abbreviation (AAA, A, AA, B, ...), the two zonei...
X Description: Bug description A POSIX TZ string requires an offset after the std abbreviation (e.g. EST5). When the offset is missing and the std field is just an abbreviation (AAA, A, AA, B, ...), the two zonei...
Opengraph URL: https://github.com/python/cpython/issues/152212
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`zoneinfo`: pure-Python parser accepts a POSIX TZ footer with no STD offset (C rejects)","articleBody":"### Bug description\n\nA POSIX TZ string requires an offset after the std abbreviation (e.g. `EST5`). When the\noffset is missing and the std field is just an abbreviation (`AAA`, `A`, `AA`, `B`, ...),\nthe two `zoneinfo` implementations disagree:\n\n- The C accelerator (`_zoneinfo`) raises `ValueError: Invalid STD offset`.\n- The pure-Python parser (`zoneinfo._zoneinfo`) silently accepts it and builds a\n fixed-offset-0 zone named after the abbreviation.\n\nReproduced by embedding the footer in a minimal TZif v2 file and loading it through\n`ZoneInfo.from_file` against both implementations:\n\n```pycon\n>>> import io, struct, datetime as dt\n>>> import _zoneinfo # C accelerator\n>>> import zoneinfo._zoneinfo # pure-Python reference\n>>>\n>>> def tzif(footer): # minimal TZif v2: 0 transitions, 1 ttinfo (UTC), footer\n... def block():\n... return (b\"TZif\" + b\"\\x32\" + b\"\\x00\" * 15\n... + struct.pack(\">6l\", 0, 0, 0, 0, 1, 4)\n... + struct.pack(\">lbb\", 0, 0, 0) + b\"UTC\\x00\")\n... return block() + block() + b\"\\n\" + footer.encode() + b\"\\n\"\n...\n>>> # C accelerator: rejects the offset-less std field\n>>> _zoneinfo.ZoneInfo.from_file(io.BytesIO(tzif(\"AAA\")), key=\"AAA\")\nTraceback (most recent call last):\n ...\nValueError: Invalid STD offset in b'AAA'\n>>>\n>>> # pure-Python: accepts it as a fixed offset-0 zone\n>>> zi = zoneinfo._zoneinfo.ZoneInfo.from_file(io.BytesIO(tzif(\"AAA\")), key=\"AAA\")\n>>> zi.utcoffset(dt.datetime(2025, 1, 15, 12))\ndatetime.timedelta(0)\n>>> zi.tzname(dt.datetime(2025, 1, 15, 12))\n'AAA'\n```\n\nThe same divergence occurs for `A`, `AA`, `B`, and any other bare std abbreviation.\n\n### Root cause\n\n`Lib/zoneinfo/_zoneinfo.py`, in `_parse_tz_str`, the std-offset branch (around L669-675 on\n`main`):\n\n```python\n if std_offset := m.group(\"stdoff\"):\n try:\n std_offset = _parse_tz_delta(std_offset)\n except ValueError as e:\n raise ValueError(f\"Invalid STD offset in {tz_str}\") from e\n else:\n std_offset = 0 # \u003c-- treats a missing std offset as 0\n```\n\nWhen the regex captures a std abbreviation but no `stdoff` group, the `else` branch\ndefaults the offset to `0` instead of raising. The C accelerator has no such default and\nraises `Invalid STD offset` when the std offset is absent.\n\n### Spec\n\nPOSIX.1-2024 (Issue 8), §8.3 \"Other Environment Variables\" (the `TZ` rule format) gives\nthe std field as `std offset` and states that the offset following `std` *shall be\nrequired*; only the `dst` offset is optional (DST then defaults to one hour ahead of std).\nRFC 8536 §3.3 (\"TZif Footer\"), which governs the embedded TZ-string footer of a TZif file,\nspecifies that the footer uses the POSIX `TZ` grammar from Base Definitions §8.3; its\nextensions (§3.3.1) only widen the offset hour range and add year-round-DST syntax; none\nrelax the required std offset. So a footer like `AAA` is not a valid POSIX TZ string, and\nthe C accelerator's rejection is the correct behavior. (For reference, macOS libc likewise\ntreats `TZ=AAA` as invalid and falls back to UTC: `tzname=('UTC', 'UTC')`, offset 0.)\n\n### Suggested fix\n\nMake the pure-Python parser reject a missing std offset, matching the C accelerator. The\n`else` branch becomes a `raise` with the same message wording the accelerator uses:\n\n```python\n else:\n raise ValueError(f\"Invalid STD offset in {tz_str}\")\n```\n\nThis is non-breaking for real data: across the full IANA database (598 zones loaded\nthrough the pure parser) no zone is newly rejected, and well-formed strings such as `EST5`,\n`\u003cABC>5`, and `AAA5` continue to parse identically on both implementations. A PR with the\none-line fix and a regression test (covering `AAA`, `A`, `AA`, `B` in `test_invalid_tzstr`,\nwhich runs against both `TZStrTest` and `CTZStrTest`) follows.\n\n### Environment\n\n- Reproduced on `main` (3.16.0a0).\n- The pure-Python parser is used whenever the `_zoneinfo` C extension is unavailable, and\n is also reachable directly via `zoneinfo._zoneinfo`.\n- The same `else: std_offset = 0` code is present on 3.13, 3.14, and 3.15, which are\n likewise affected.\n\nThis is a correctness/parity issue, not a security issue.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152213\n* gh-152377\n* gh-152378\n* gh-152379\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-25T15:13:56Z","url":"https://github.com/python/cpython/issues/152212"}
| 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:cce14afd-5542-a057-1472-e951be6bb105 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8C0:DB09F:42D750B:5532AF1:6B023AA1 |
| html-safe-nonce | 3c96a261e4e1db101a5025e0d1975705cd8cc85f402dd4a410da77b7f01c99fc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEMwOkRCMDlGOjQyRDc1MEI6NTUzMkFGMTo2QjAyM0FBMSIsInZpc2l0b3JfaWQiOiIyODIyNDg2OTY3ODAyMDE4NDY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 75bc77000aca7baef50193d662154cc06d2f1bd1c90204401c22ac7dea88cd9b |
| hovercard-subject-tag | issue:4745068326 |
| 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/152212/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ab20c97209293f8dea687c2b43cc51bcfd05a34df1719cb180eec72009aac6f8/python/cpython/issues/152212 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ab20c97209293f8dea687c2b43cc51bcfd05a34df1719cb180eec72009aac6f8/python/cpython/issues/152212 |
| og:image:alt | Bug description A POSIX TZ string requires an offset after the std abbreviation (e.g. EST5). When the offset is missing and the std field is just an abbreviation (AAA, A, AA, B, ...), the two zonei... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width