Repository navigation
gh-162306: Reject day 0 and year 0 with a 24:00 time in datetime.fromisoformat() - #162307
Conversation
|
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 |
|
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 |
Thanks, Paul! I checked ISO 8601, and I think your reading makes sense. Would it make sense to update the PR to keep accepting |
|
Yeah, I think that is the right thing to do. Thanks @choam2426 |
|
@pganssle Thanks! |
420be25 to
a355855
Compare
|
Thanks @choam2426 for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-163161 is a backport of this pull request to the 3.15 branch. |
|
GH-163162 is a backport of this pull request to the 3.14 branch. |
datetime.fromisoformat()rolled a24:00time over to the next day even when the day was0or the year was0, so"2026-06-00T24:00"returned2026-06-01 00:00and"0000-12-31T24:00"returned0001-01-01 00:00instead of raisingValueError.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 >= 1andyear >= MINYEARbounds in bothModules/_datetimemodule.candLib/_pydatetime.py. With the guard skipped, the constructor raises its usualValueErrorfor these inputs.Tests:
"0000-12-31T24:00:00"is added totest_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.