From 7b3f68d7ae5fc390b9014cfa8969201dc1ff136f Mon Sep 17 00:00:00 2001 From: choam2426 Date: Fri, 20 Nov 2026 13:36:46 +0900 Subject: [PATCH] gh-162306: Reject day 0 and year 0 with a 24:00 time in datetime.fromisoformat() --- Lib/_pydatetime.py | 3 ++- Lib/test/datetimetester.py | 10 +++++++--- .../2026-11-20-13-51-31.gh-issue-162306.ys64EX.rst | 3 +++ Modules/_datetimemodule.c | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-20-13-51-31.gh-issue-162306.ys64EX.rst diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 5a8fec43e668a53..5cb167646d40bd5 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -1957,7 +1957,8 @@ def fromisoformat(cls, date_string): if became_next_day: year, month, day = date_components # Only wrap day/month when it was previously valid - if 1 <= month <= 12 and day <= (days_in_month := _days_in_month(year, month)): + if (year >= MINYEAR and 1 <= month <= 12 and + 1 <= day <= (days_in_month := _days_in_month(year, month))): # Calculate midnight of the next day day += 1 if day > days_in_month: diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 00a9d2bca3f4779..66554a8e5863b6f 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -4020,6 +4020,7 @@ def test_fromisoformat_fails_datetime_valueerror(self): "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 "9999-12-31T24:00:00", # Year out of range ] @@ -4029,9 +4030,12 @@ def test_fromisoformat_fails_datetime_valueerror(self): self.theclass.fromisoformat(bad_str) # days out of range have their own error message, see issue 70647 - with self.assertRaises(ValueError) as msg: - self.theclass.fromisoformat("2009-04-32T24:00:00") - self.assertIn(f"day 32 must be in range 1..30 for month 4 in year 2009", str(msg.exception)) + for day in (0, 32): + with self.subTest(day=day): + with self.assertRaises(ValueError) as msg: + self.theclass.fromisoformat(f"2009-04-{day:02d}T24:00:00") + self.assertIn(f"day {day} must be in range 1..30 for month 4 in year 2009", + str(msg.exception)) def test_fromisoformat_fails_surrogate(self): # Test that when fromisoformat() fails with a surrogate character as diff --git a/Misc/NEWS.d/next/Library/2026-11-20-13-51-31.gh-issue-162306.ys64EX.rst b/Misc/NEWS.d/next/Library/2026-11-20-13-51-31.gh-issue-162306.ys64EX.rst new file mode 100644 index 000000000000000..a5d750775be3d7c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-20-13-51-31.gh-issue-162306.ys64EX.rst @@ -0,0 +1,3 @@ +Fix :meth:`datetime.datetime.fromisoformat` to raise :exc:`ValueError` for a +day or year of ``0`` combined with a ``24:00`` time, instead of returning the +following date. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 091da059afd34b4..07df76e6009fc4f 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -6245,9 +6245,9 @@ datetime_datetime_fromisoformat_impl(PyTypeObject *type, PyObject *string) goto error; } - if ((hour == 24) && (month >= 1 && month <= 12)) { + if ((hour == 24) && (year >= MINYEAR) && (month >= 1 && month <= 12)) { int d_in_month = days_in_month(year, month); - if (day <= d_in_month) { + if (day >= 1 && day <= d_in_month) { if (minute == 0 && second == 0 && microsecond == 0) { // Calculate midnight of the next day hour = 0;