Skip to content

gh-162306: Reject day 0 and year 0 with a 24:00 time in datetime.fromisoformat() - #162307

Open
choam2426 wants to merge 1 commit into
python:mainfrom
choam2426:fromisoformat-24-lower-bounds
Open

choam2426 wants to merge 1 commit into
python:mainfrom
choam2426:fromisoformat-24-lower-bounds

Conversation

@choam2426

Copy link
Copy Markdown

datetime.fromisoformat() rolled a 24:00 time over to the next day even when the day was 0 or the year was 0, so "2026-06-00T24:00" returned 2026-06-01 00:00 and "0000-12-31T24:00" returned 0001-01-01 00:00 instead of raising ValueError.

The rollover guard already checks the month range and the upper day bound (the month lower bound came with gh-151770). This adds the missing day >= 1 and year >= MINYEAR bounds in both Modules/_datetimemodule.c and Lib/_pydatetime.py. With the guard skipped, the constructor raises its usual ValueError for these inputs.

Tests: "0000-12-31T24:00:00" is added to test_fromisoformat_fails_datetime_valueerror, and the existing day-32 check now also covers day 0.

I used an AI coding assistant to help with this change. I reviewed the analysis, the patch and the tests.

@python-cla-bot

python-cla-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@gabrielrbl

Copy link
Copy Markdown

Independently verified on Linux x86-64 with a CPython debug build at 7b3f68d. The new regression test fails on the parent for both day zero and year zero, and passes on the PR in both the C and pure-Python implementations. I also checked every valid month-end rollover from 2000 through 2399. All 4,800 produced midnight on the first of the next month in each implementation. The full test_datetime suite passed with the same 33 skips as on the parent.

"2009-04-01T25:30:45", # Hour out of range
"2009-00-01T24:00:00", # Month below range
"2009-13-01T24:00:00", # Month out of range
"0000-12-31T24:00:00", # Year below range

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is below MINYEAR (which is one), so it seems correct to reject this case. This is not very long standing behavior either (24h support was added in gh-102450, python 3.14).

@pganssle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants