Skip to content

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

Merged
StanFromIreland merged 3 commits into
python:mainfrom
choam2426:fromisoformat-24-lower-bounds
Dec 4, 2026
Merged

StanFromIreland merged 3 commits into
python:mainfrom
choam2426:fromisoformat-24-lower-bounds

Conversation

@choam2426

Copy link
Copy Markdown
Contributor

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.

Comment thread Lib/test/datetimetester.py Outdated
"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

@pganssle

Copy link
Copy Markdown
Member

I think a relevant question here is what ISO 8601 says exactly, because this is a weird edge case. The way I think of these >23:59 times is usually as conceptually belonging to the previous day even though they canonicalize to the next day. In that conception the 2026-06-00 date being invalid would win.

That said these do map to valid date times, so depending on how the spec is worded it might be reasonable to accept them.

I think the "0000-12-31T24:00:00" one unambiguously we should accept, since the date is valid in the proleptic Gregorian calendar and the string maps to a datetime in the range of valid Python datetimes.

@choam2426

Copy link
Copy Markdown
Contributor Author

I think a relevant question here is what ISO 8601 says exactly, because this is a weird edge case. The way I think of these >23:59 times is usually as conceptually belonging to the previous day even though they canonicalize to the next day. In that conception the 2026-06-00 date being invalid would win.

That said these do map to valid date times, so depending on how the spec is worded it might be reasonable to accept them.

I think the "0000-12-31T24:00:00" one unambiguously we should accept, since the date is valid in the proleptic Gregorian calendar and the string maps to a datetime in the range of valid Python datetimes.

Thanks, Paul! I checked ISO 8601, and I think your reading makes sense. Would it make sense to update the PR to keep accepting 0000-12-31T24:00:00 and only reject day 00?

@pganssle

Copy link
Copy Markdown
Member

Yeah, I think that is the right thing to do. Thanks @choam2426

@choam2426

choam2426 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@pganssle Thanks!
Updated: 0000-12-31T24:00:00 is accepted again (returns datetime(1, 1, 1), with a test), and only day 00 with 24:00 is rejected.

@choam2426
choam2426 requested a review from eendebakpt November 25, 2026 05:29
@choam2426
choam2426 force-pushed the fromisoformat-24-lower-bounds branch from 420be25 to a355855 Compare December 4, 2026 05:49
@StanFromIreland StanFromIreland added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Dec 4, 2026
@StanFromIreland
StanFromIreland merged commit 3850e10 into python:main Dec 4, 2026
65 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @choam2426 for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

GH-163161 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Dec 4, 2026
@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

GH-163162 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants