10000 Allow ISO-8601 24:00 alternative to midnight on datetime.datetime.fro… · python/cpython@0b4ac70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b4ac70

Browse files
committed
Allow ISO-8601 24:00 alternative to midnight on datetime.datetime.fromisoformat()
1 parent 13bae5b commit 0b4ac70

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Modules/_datetimemodule.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5494,6 +5494,22 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
54945494
goto error;
54955495
}
54965496

5497+
if (
5498+
(hour == 24 && minute == 0 && second == 0 && microsecond == 0) && // provided alternate to midnight of next day
5499+
(month <= 12 && day <= days_in_month(year, month)) // month and day component was previously valid
5500+
) {
5501+
// Calculate midnight of the next day
5502+
hour = 0;
5503+
day += 1;
5504+
if (day > days_in_month(year, month)) {
5505+
day = 1;
5506+
month += 1;
5507+
if (month > 12) {
5508+
month = 1;
5509+
year += 1;
5510+
}
5511+
}
5512+
}
54975513
PyObject *dt = new_datetime_subclass_ex(year, month, day, hour, minute,
54985514
second, microsecond, tzinfo, cls);
54995515

0 commit comments

Comments
 (0)
0