Skip to content

Commit 0b92a1c

Browse files
committed
Fix differing error msg in datetime.fromisoformat implementations when 24hrs has non-zero time component(s)
1 parent c71764f commit 0b92a1c

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
@@ -465,13 +465,14 @@ def _parse_isoformat_time(tstr):
465465

466466
hour, minute, second, microsecond = time_comps
467467
became_next_day = False
468+
error_from_components = False
468469
if (hour == 24):
469-
if not all(time_comp == 0 for time_comp in time_comps[1:]):
470-
raise ValueError("minute, second, and microsecond must be 0 when hour is 24")
471-
472-
hour = 0
473-
time_comps[0] = hour
474-
became_next_day = True
470+
if all(time_comp == 0 for time_comp in time_comps[1:]):
471+
hour = 0
472+
time_comps[0] = hour
473+
became_next_day = True
474+
else:
475+
error_from_components = True
475476

476477
tzi = None
477478
if tz_pos == len_str and tstr[-1] == 'Z':
@@ -505,7 +506,7 @@ def _parse_isoformat_time(tstr):
505506

506507
time_comps.append(tzi)
507508

508-
return time_comps, became_next_day
509+
return time_comps, became_next_day, error_from_components
509510

510511
# tuple[int, int, int] -> tuple[int, int, int] version of date.fromisocalendar
511512
def _isoweek_to_gregorian(year, week, day):
@@ -1912,11 +1913,14 @@ def fromisoformat(cls, date_string):
19121913

19131914
if tstr:
19141915
try:
1915-
time_components, became_next_day = _parse_isoformat_time(tstr)
1916+
time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr)
19161917
except ValueError:
19171918
raise ValueError(
19181919
f'Invalid isoformat string: {date_string!r}') from None
19191920
else:
1921+
if error_from_components:
1922+
raise ValueError("minute, second, and microsecond must be 0 when hour is 24")
1923+
19201924
if became_next_day:
19211925
year, month, day = date_components
19221926
# Only wrap day/month when it was previously valid

0 commit comments

Comments
 (0)