According to ISO-8601, a time of 24:00 on a given date is a valid alternative to 00:00 of the following date, however Python does not support this, raising the following error when attempted: ValueError: hour must be in 0..23.
This bug can be seen from multiple scenarios, specifically anything that internally calls the _check_time_fields function, such as the following:
>>>importdatetime>>>datetime.datetime(2022, 1, 2, 24, 0, 0) # should be equivalent to 2022-01-03 00:00:00Traceback (mostrecentcalllast):
File "<pyshell#2>", line 1, in <module>datetime.datetime(2022, 3, 4, 24, 0, 0)
ValueError: hourmustbein0..23
The fix for this is relatively simple: have an explicit check within _check_time_fields for the scenario where hour == 24 and minute == 0 and second == 0 and microsecond == 0, or more concisely hour == 24 and not any((minute, second, microsecond)), and in this scenario increase the day by one (adjusting the week/month/year as necessary) and set the hour to 0.
As of ISO 8601-1:2019/Amd 1:2022, midnight may be referred to as "00:00:00", corresponding to the instant at the beginning of a calendar day; or "24:00:00", corresponding to the instant at the end of a calendar day.[1] ISO 8601-1:2019 as originally published removed "24:00" as a representation for the end of day although it was permitted in earlier versions of the standard.
Looking at the datetime commit log, I am pretty sure that 24:00:00 support was not removed after the 2019 version but was never there. It is obviously a nuisance.
While the doc for datetime.datetime explicitly disclaims such support ("0 <= hour < 24"), the doc for datetime.datetime.fromisoformat does not. It says "Return a datetime corresponding to a date_string in any valid ISO 8601 format, with the following exceptions:" and lists 4 exceptions. To me, the bug here is the omission of the pretty clearly intended '240000' exception. The fix would be to add the 5th exception to the doc.
The request to add support would then be a feature addition. I have no opinion, but think it less likely to be accepted than rejected.
Bug report
According to ISO-8601, a time of
24:00
on a given date is a valid alternative to00:00
of the following date, however Python does not support this, raising the following error when attempted:ValueError: hour must be in 0..23
.This bug can be seen from multiple scenarios, specifically anything that internally calls the
_check_time_fields
function, such as the following:The fix for this is relatively simple: have an explicit check within
_check_time_fields
for the scenario wherehour == 24 and minute == 0 and second == 0 and microsecond == 0
, or more conciselyhour == 24 and not any((minute, second, microsecond))
, and in this scenario increase the day by one (adjusting the week/month/year as necessary) and set the hour to 0.I imagine the
check_time_args
C function would also have to be updated.Your environment
The text was updated successfully, but these errors were encountered: