@@ -4636,8 +4636,12 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
4636
4636
return NULL ;
4637
4637
}
4638
4638
4639
- if (hour == 24 && minute == 0 && second == 0 && microsecond == 0 ) {
4640
- hour = 0 ;
4639
+ if (hour == 24 ) {
4640
+ if (minute == 0 && second == 0 && microsecond == 0 ) {
4641
+ hour = 0 ;
4642
+ } else {
4643
+ goto invalid_iso_midnight ;
4644
+ }
4641
4645
}
4642
4646
4643
4647
PyObject * t ;
@@ -4651,6 +4655,10 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
4651
4655
Py_DECREF (tzinfo );
4652
4656
return t ;
4653
4657
4658
+ invalid_iso_midnight :
4659
+ PyErr_SetString (PyExc_ValueError , "minute, second, and microsecond must be 0 when hour is 24" );
4660
+ return NULL ;
4661
+
4654
4662
invalid_string_error :
4655
4663
PyErr_Format (PyExc_ValueError , "Invalid isoformat string: %R" , tstr );
4656
4664
return NULL ;
@@ -5494,20 +5502,21 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
5494
5502
goto error ;
5495
5503
}
5496
5504
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 ;
5505
+ if ((hour == 24 ) && (month <= 12 && day <= days_in_month (year , month ))) {
5506
+ if (minute == 0 && second == 0 && microsecond == 0 ) {
5507
+ // Calculate midnight of the next day
5508
+ hour = 0 ;
5509
+ day += 1 ;
5510
+ if (day > days_in_month (year , month )) {
5511
+ day = 1 ;
5512
+ month += 1 ;
5513
+ if (month > 12 ) {
5514
+ month = 1 ;
5515
+ year += 1 ;
5516
+ }
5510
5517
}
5518
+ } else {
5519
+ goto invalid_iso_midnight ;
5511
5520
}
5512
5521
}
5513
5522
PyObject * dt = new_datetime_subclass_ex (year , month , day , hour , minute ,
@@ -5517,6 +5526,10 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
5517
5526
Py_DECREF (dtstr_clean );
5518
5527
return dt ;
5519
5528
5529
+ invalid_iso_midnight :
5530
+ PyErr_SetString (PyExc_ValueError , "minute, second, and microsecond must be 0 when hour is 24" );
5531
+ return NULL ;
5532
+
5520
5533
invalid_string_error :
5521
5534
PyErr_Format (PyExc_ValueError , "Invalid isoformat string: %R" , dtstr );
5522
5535
0 commit comments