Title: datetime.fromisoformat() hits a debug-build assertion on an out-of-range month combined with a 24:00 time · Issue #151770 · python/cpython · GitHub
Open Graph Title: datetime.fromisoformat() hits a debug-build assertion on an out-of-range month combined with a 24:00 time · Issue #151770 · python/cpython
X Title: datetime.fromisoformat() hits a debug-build assertion on an out-of-range month combined with a 24:00 time · Issue #151770 · python/cpython
Description: Bug report datetime.fromisoformat() reaches days_in_month() with month == 0 when the string combines an out-of-range (below 1) month with a 24:00 time, tripping a debug-build assertion. On a release build the same input raises ValueError...
Open Graph Description: Bug report datetime.fromisoformat() reaches days_in_month() with month == 0 when the string combines an out-of-range (below 1) month with a 24:00 time, tripping a debug-build assertion. On a releas...
X Description: Bug report datetime.fromisoformat() reaches days_in_month() with month == 0 when the string combines an out-of-range (below 1) month with a 24:00 time, tripping a debug-build assertion. On a releas...
Opengraph URL: https://github.com/python/cpython/issues/151770
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"datetime.fromisoformat() hits a debug-build assertion on an out-of-range month combined with a 24:00 time","articleBody":"# Bug report\n\n`datetime.fromisoformat()` reaches `days_in_month()` with `month == 0` when the string combines an out-of-range (below 1) month with a `24:00` time, tripping a debug-build assertion. On a release build the same input raises `ValueError`, so the behaviour is inconsistent between build configurations.\n\n## Reproduction\n\n```python\n>>> from datetime import datetime\n>>> datetime.fromisoformat(\"2009-00-01T24:00:00\")\n```\n\nOn a `--with-pydebug` build (C implementation):\n\n```\nAssertion failed: (month >= 1), function days_in_month, file Modules/_datetimemodule.c, line 441.\n```\n\n(SIGABRT). The pure-Python implementation in `Lib/_pydatetime.py` raises `AssertionError` on the same input. On a release / `-DNDEBUG` build, both implementations correctly raise `ValueError: month must be in 1..12, not 0`.\n\n## Root cause\n\nThe `24:00` midnight-rollover guard validates the upper month bound but not the lower one before calling `days_in_month()`:\n\n- `Modules/_datetimemodule.c`, `datetime_datetime_fromisoformat_impl`: `if ((hour == 24) && (month \u003c= 12))`\n- `Lib/_pydatetime.py`, `datetime.fromisoformat`: `if month \u003c= 12 and day \u003c= (days_in_month := _days_in_month(year, month))`\n\nThe parser does not range-check the month before this point, so `month == 0` flows into `days_in_month(year, 0)` and hits `assert(month >= 1)`. The matching upper-bound input `\"2009-13-01T24:00:00\"` is already rejected by the `month \u003c= 12` check; only the lower bound is missing.\n\n## Suggested fix\n\nAdd the `month >= 1` lower bound to the guard in both implementations (mirroring `check_date_args`'s existing `month \u003c 1 || month > 12` validation), so the `24:00` path is only taken for an in-range month and the normal `ValueError` is raised otherwise. I'll open a PR.\n\nThis was found while adding a fuzz harness for `fromisoformat`.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-151771\n* gh-151809\n* gh-151810\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-20T07:18:35Z","url":"https://github.com/python/cpython/issues/151770"}
| 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:f6550ef5-3162-562d-dfaa-3e6a6129107e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9A6:215B92:31700FF:41DEC17:6B01EC46 |
| html-safe-nonce | a74ea0d8e0625e6376f3e71649e128214254dec7e7accf4785f7de469aa496bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUE2OjIxNUI5MjozMTcwMEZGOjQxREVDMTc6NkIwMUVDNDYiLCJ2aXNpdG9yX2lkIjoiNDY1NDYzMjg0MTMyNDM5MTQ5NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d7eab8c69348730a156cfc89602f531017fd4937bb261748db0398ff7dcfac67 |
| hovercard-subject-tag | issue:4705546609 |
| 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/151770/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f90c88372ad1d486790b8cb08aaece95a269d57aa02d953aa2eabf8891d0eeba/python/cpython/issues/151770 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f90c88372ad1d486790b8cb08aaece95a269d57aa02d953aa2eabf8891d0eeba/python/cpython/issues/151770 |
| og:image:alt | Bug report datetime.fromisoformat() reaches days_in_month() with month == 0 when the string combines an out-of-range (below 1) month with a 24:00 time, tripping a debug-build assertion. On a releas... |
| 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