Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is below MINYEAR (which is one), so it seems correct to reject this case. This is not very long standing behavior either (24h support was added in gh-102450, python 3.14).

@pganssle

"9999-12-31T24:00:00", # Year out of range
]

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading