Skip to content

Commit 6006f8f

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

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
@@ -1936,7 +1936,8 @@ def fromisoformat(cls, date_string):
19361936
if became_next_day:
19371937
year, month, day = date_components
19381938
# Only wrap day/month when it was previously valid
1939-
if 1 <= month <= 12 and day <= (days_in_month := _days_in_month(year, month)):
1939+
if (year >= MINYEAR and 1 <= month <= 12 and
1940+
1 <= day <= (days_in_month := _days_in_month(year, month))):
19401941
# Calculate midnight of the next day
19411942
day += 1
19421943
if day > days_in_month:

‎Lib/test/datetimetester.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,6 +4126,7 @@ def test_fromisoformat_fails_datetime_valueerror(self):
41264126
"2009-04-01T25:30:45", # Hour out of range
41274127
"2009-00-01T24:00:00", # Month below range
41284128
"2009-13-01T24:00:00", # Month out of range
4129+
"0000-12-31T24:00:00", # Year below range
41294130
"9999-12-31T24:00:00", # Year out of range
41304131
]
41314132

@@ -4135,9 +4136,12 @@ def test_fromisoformat_fails_datetime_valueerror(self):
41354136
self.theclass.fromisoformat(bad_str)
41364137

41374138
# days out of range have their own error message, see issue 70647
4138-
with self.assertRaises(ValueError) as msg:
4139-
self.theclass.fromisoformat("2009-04-32T24:00:00")
4140-
self.assertIn(f"day 32 must be in range 1..30 for month 4 in year 2009", str(msg.exception))
4139+
for day in (0, 32):
4140+
with self.subTest(day=day):
4141+
with self.assertRaises(ValueError) as msg:
4142+
self.theclass.fromisoformat(f"2009-04-{day:02d}T24:00:00")
4143+
self.assertIn(f"day {day} must be in range 1..30 for month 4 in year 2009",
4144+
str(msg.exception))
41414145

41424146
def test_fromisoformat_fails_surrogate(self):
41434147
# 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
@@ -6215,9 +6215,9 @@ datetime_datetime_fromisoformat_impl(PyTypeObject *type, PyObject *string)
62156215
goto error;
62166216
}
62176217

6218-
if ((hour == 24) && (month >= 1 && month <= 12)) {
6218+
if ((hour == 24) && (year >= MINYEAR) && (month >= 1 && month <= 12)) {
62196219
int d_in_month = days_in_month(year, month);
6220-
if (day <= d_in_month) {
6220+
if (day >= 1 && day <= d_in_month) {
62216221
if (minute == 0 && second == 0 && microsecond == 0) {
62226222
// Calculate midnight of the next day
62236223
hour = 0;

0 commit comments

Comments
 (0)