Skip to content

Commit 7b3f68d

Browse files
committed
gh-162306: Reject day 0 and year 0 with a 24:00 time in datetime.fromisoformat()
1 parent b09a7e9 commit 7b3f68d

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

‎Lib/_pydatetime.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,8 @@ def fromisoformat(cls, date_string):
19571957
if became_next_day:
19581958
year, month, day = date_components
19591959
# Only wrap day/month when it was previously valid
1960-
if 1 <= month <= 12 and day <= (days_in_month := _days_in_month(year, month)):
1960+
if (year >= MINYEAR and 1 <= month <= 12 and
1961+
1 <= day <= (days_in_month := _days_in_month(year, month))):
19611962
# Calculate midnight of the next day
19621963
day += 1
19631964
if day > days_in_month:

‎Lib/test/datetimetester.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4020,6 +4020,7 @@ def test_fromisoformat_fails_datetime_valueerror(self):
40204020
"2009-04-01T25:30:45", # Hour out of range
40214021
"2009-00-01T24:00:00", # Month below range
40224022
"2009-13-01T24:00:00", # Month out of range
4023+
"0000-12-31T24:00:00", # Year below range
40234024
"9999-12-31T24:00:00", # Year out of range
40244025
]
40254026

@@ -4029,9 +4030,12 @@ def test_fromisoformat_fails_datetime_valueerror(self):
40294030
self.theclass.fromisoformat(bad_str)
40304031

40314032
# days out of range have their own error message, see issue 70647
4032-
with self.assertRaises(ValueError) as msg:
4033-
self.theclass.fromisoformat("2009-04-32T24:00:00")
4034-
self.assertIn(f"day 32 must be in range 1..30 for month 4 in year 2009", str(msg.exception))
4033+
for day in (0, 32):
4034+
with self.subTest(day=day):
4035+
with self.assertRaises(ValueError) as msg:
4036+
self.theclass.fromisoformat(f"2009-04-{day:02d}T24:00:00")
4037+
self.assertIn(f"day {day} must be in range 1..30 for month 4 in year 2009",
4038+
str(msg.exception))
40354039

40364040
def test_fromisoformat_fails_surrogate(self):
40374041
# Test that when fromisoformat() fails with a surrogate character as
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`datetime.datetime.fromisoformat` to raise :exc:`ValueError` for a
2+
day or year of ``0`` combined with a ``24:00`` time, instead of returning the
3+
following date.

‎Modules/_datetimemodule.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6245,9 +6245,9 @@ datetime_datetime_fromisoformat_impl(PyTypeObject *type, PyObject *string)
62456245
goto error;
62466246
}
62476247

6248-
if ((hour == 24) && (month >= 1 && month <= 12)) {
6248+
if ((hour == 24) && (year >= MINYEAR) && (month >= 1 && month <= 12)) {
62496249
int d_in_month = days_in_month(year, month);
6250-
if (day <= d_in_month) {
6250+
if (day >= 1 && day <= d_in_month) {
62516251
if (minute == 0 && second == 0 && microsecond == 0) {
62526252
// Calculate midnight of the next day
62536253
hour = 0;

0 commit comments

Comments
 (0)