Skip to content

Commit 04a275a

Browse files
committed
Fix differing error msg in datetime.fromisoformat implementations when 24hrs has non-zero time component(s)
1 parent 5dc3d69 commit 04a275a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Lib/_pydatetime.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,14 @@ def _parse_isoformat_time(tstr):
433433

434434
hour, minute, second, microsecond = time_comps
435435
became_next_day = False
436+
error_from_components = False
436437
if (hour == 24):
437-
if not all(time_comp == 0 for time_comp in time_comps[1:]):
438-
raise ValueError("minute, second, and microsecond must be 0 when hour is 24")
439-
440-
hour = 0
441-
time_comps[0] = hour
442-
became_next_day = True
438+
if all(time_comp == 0 for time_comp in time_comps[1:]):
439+
hour = 0
440+
time_comps[0] = hour
441+
became_next_day = True
442+
else:
443+
error_from_components = True
443444

444445
tzi = None
445446
if tz_pos == len_str and tstr[-1] == 'Z':
@@ -473,7 +474,7 @@ def _parse_isoformat_time(tstr):
473474

474475
time_comps.append(tzi)
475476

476-
return time_comps, became_next_day
477+
return time_comps, became_next_day, error_from_components
477478

478479
# tuple[int, int, int] -> tuple[int, int, int] version of date.fromisocalendar
479480
def _isoweek_to_gregorian(year, week, day):
@@ -1881,11 +1882,14 @@ def fromisoformat(cls, date_string):
18811882

18821883
if tstr:
18831884
try:
1884-
time_components, became_next_day = _parse_isoformat_time(tstr)
1885+
time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr)
18851886
except ValueError:
18861887
raise ValueError(
18871888
f'Invalid isoformat string: {date_string!r}') from None
18881889
else:
1890+
if error_from_components:
1891+
raise ValueError("minute, second, and microsecond must be 0 when hour is 24")
1892+
18891893
if became_next_day:
18901894
year, month, day = date_components
18911895
# Only wrap day/month when it was previously valid

0 commit comments

Comments
 (0)