From 6006f8fa1f6df3fef538c5e216acde2b9aabf2bd Mon Sep 17 00:00:00 2001 From: choam2426 Date: Fri, 20 Nov 2026 13:36:46 +0900 Subject: [PATCH 1/3] 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 735569c654bf67..33279b9f0084ae 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -1936,7 +1936,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 2e2e64ff09f7bb..83504a26cbeb11 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -4126,6 +4126,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 ] @@ -4135,9 +4136,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 00000000000000..a5d750775be3d7 --- /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 ae390955e0d709..63bbc204848d7b 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -6215,9 +6215,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; From d2e66d4562f8e956ea21b99cfc3fa77cd7194bea Mon Sep 17 00:00:00 2001 From: choam2426 Date: Sat, 21 Nov 2026 16:49:07 +0900 Subject: [PATCH 2/3] Keep accepting year 0 with a 24:00 time that rolls over into year 1 --- Lib/_pydatetime.py | 3 +-- Lib/test/datetimetester.py | 1 - .../Library/2026-11-20-13-51-31.gh-issue-162306.ys64EX.rst | 2 +- Modules/_datetimemodule.c | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 33279b9f0084ae..2f2602a4ba7de9 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -1936,8 +1936,7 @@ def fromisoformat(cls, date_string): if became_next_day: year, month, day = date_components # Only wrap day/month when it was previously valid - if (year >= MINYEAR and 1 <= month <= 12 and - 1 <= day <= (days_in_month := _days_in_month(year, month))): + if 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 83504a26cbeb11..d2b79bfa163d91 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -4126,7 +4126,6 @@ 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 ] 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 index a5d750775be3d7..78e0bb43b0eb1e 100644 --- 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 @@ -1,3 +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 +day 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 63bbc204848d7b..b93d737564833f 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -6215,7 +6215,7 @@ datetime_datetime_fromisoformat_impl(PyTypeObject *type, PyObject *string) goto error; } - if ((hour == 24) && (year >= MINYEAR) && (month >= 1 && month <= 12)) { + if ((hour == 24) && (month >= 1 && month <= 12)) { int d_in_month = days_in_month(year, month); if (day >= 1 && day <= d_in_month) { if (minute == 0 && second == 0 && microsecond == 0) { From a3558551c78a96a772edd3fc2382935478504925 Mon Sep 17 00:00:00 2001 From: choam2426 Date: Sat, 21 Nov 2026 16:51:16 +0900 Subject: [PATCH 3/3] Test that year 0 with a 24:00 time rolls over into year 1 --- Lib/test/datetimetester.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index d2b79bfa163d91..7f75384551852a 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -4033,7 +4033,8 @@ def test_fromisoformat_datetime_examples(self): tzinfo=timezone(timedelta(seconds=10)))), ('2025-01-02T24:00:00', self.theclass(2025, 1, 3, 0, 0, 0)), ('2025-01-31T24:00:00', self.theclass(2025, 2, 1, 0, 0, 0)), - ('2025-12-31T24:00:00', self.theclass(2026, 1, 1, 0, 0, 0)) + ('2025-12-31T24:00:00', self.theclass(2026, 1, 1, 0, 0, 0)), + ('0000-12-31T24:00:00', self.theclass(1, 1, 1, 0, 0, 0)), ] for input_str, expected in examples: