Title: datetime.fromisoformat() (C) drops the sub-second part of a UTC offset, breaking round-trips · Issue #152079 · python/cpython · GitHub
Open Graph Title: datetime.fromisoformat() (C) drops the sub-second part of a UTC offset, breaking round-trips · Issue #152079 · python/cpython
X Title: datetime.fromisoformat() (C) drops the sub-second part of a UTC offset, breaking round-trips · Issue #152079 · python/cpython
Description: Bug report Bug description: The C implementation of datetime.fromisoformat() silently drops the sub-second part of a UTC offset whenever the offset's whole-second part is zero, returning timezone.utc. This breaks round-trips: a value the...
Open Graph Description: Bug report Bug description: The C implementation of datetime.fromisoformat() silently drops the sub-second part of a UTC offset whenever the offset's whole-second part is zero, returning timezone.u...
X Description: Bug report Bug description: The C implementation of datetime.fromisoformat() silently drops the sub-second part of a UTC offset whenever the offset's whole-second part is zero, returning timezo...
Opengraph URL: https://github.com/python/cpython/issues/152079
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"datetime.fromisoformat() (C) drops the sub-second part of a UTC offset, breaking round-trips","articleBody":"# Bug report\n\n### Bug description:\n\nThe C implementation of `datetime.fromisoformat()` silently drops the sub-second part of a\nUTC offset whenever the offset's whole-second part is zero, returning `timezone.utc`. This\nbreaks round-trips: a value the module itself produces via `isoformat()` is not parsed\nback faithfully.\n\n```python\n>>> from datetime import datetime, timezone, timedelta\n>>> dt = datetime(2020, 6, 15, 12, 34, 56, tzinfo=timezone(timedelta(microseconds=1)))\n>>> s = dt.isoformat()\n>>> s\n'2020-06-15T12:34:56+00:00:00.000001'\n>>> datetime.fromisoformat(s).utcoffset() # C accelerator\ndatetime.timedelta(0) # the 1-microsecond offset is gone\n```\n\nThe pure-Python implementation is correct:\n\n```python\n>>> import _pydatetime\n>>> _pydatetime.datetime.fromisoformat(s).utcoffset()\ndatetime.timedelta(microseconds=1)\n```\n\nThe negative case `'...-00:00:00.000001'` is dropped the same way.\n\nThis is not a non-standard format: `isoformat()` is documented to emit\n`+HH:MM[:SS[.ffffff]]` and `fromisoformat()` is documented to accept it (\"Time zone\noffsets may have fractional seconds\"), so this is documented round-trippable data being\nsilently dropped.\n\n### Root cause\n\n`Modules/_datetimemodule.c`, `tzinfo_from_isoformat_results()` short-circuits to UTC on the\nwhole-second offset alone, ignoring the parsed sub-second component:\n\n```c\n// Create a timezone from offset in seconds (0 returns UTC)\nif (tzoffset == 0) {\n return Py_NewRef(CONST_UTC(NO_STATE));\n}\n```\n\nWhen `tzoffset == 0` but `tz_useconds != 0`, the sub-second part is discarded. The\npure-Python implementation checks all offset components before collapsing to UTC.\n\n### Suggested fix\n\nOnly short-circuit to UTC when both the whole-second and sub-second parts are zero:\n\n```c\nif (tzoffset == 0 && tz_useconds == 0) {\n return Py_NewRef(CONST_UTC(NO_STATE));\n}\n```\n\nThe fall-through path already builds the correct `timezone(timedelta(...))`. A plain\n`+00:00` offset still returns `timezone.utc`. I confirmed against a full C-vs-pure-Python\ndifferential that the only inputs whose behaviour changes are exactly these zero-whole-\nsecond sub-second offsets, with no other divergence introduced. I have a patch and a\nround-trip regression test (running under both implementations) ready.\n\nThis is a sibling to #152060, a separate `fromisoformat()` defect in the pure-Python\nimplementation.\n\n### CPython versions tested on:\n\n3.16 (main, built from source)\n\n### Operating systems tested on:\n\nmacOS\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152087\n* gh-152174\n* gh-152175\n* gh-152176\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-24T13:09:23Z","url":"https://github.com/python/cpython/issues/152079"}
| 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:cd8fdd1a-1302-f714-f930-346e307afd41 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 967A:2A98A:1CB713:241B83:6B02029B |
| html-safe-nonce | 86432a4030eb03a121492a8f4765f14995963034105d51d6f6ea9cda2e3efbb8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjdBOjJBOThBOjFDQjcxMzoyNDFCODM6NkIwMjAyOUIiLCJ2aXNpdG9yX2lkIjoiNDEwNDI3ODc2ODU1NjgzNTQ4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | db729c48c5d1cd5a59365dae000b5d0cedeb9aa00cbaec8a0eaee33b6b893faa |
| hovercard-subject-tag | issue:4735108937 |
| 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/152079/issue_layout |
| twitter:image | https://opengraph.githubassets.com/81793f6c65cc69d87813e4e827a524cfdd27f2ffbd3f02de4d103c2b5e0ebe3f/python/cpython/issues/152079 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/81793f6c65cc69d87813e4e827a524cfdd27f2ffbd3f02de4d103c2b5e0ebe3f/python/cpython/issues/152079 |
| og:image:alt | Bug report Bug description: The C implementation of datetime.fromisoformat() silently drops the sub-second part of a UTC offset whenever the offset's whole-second part is zero, returning timezone.u... |
| 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