Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

datetime library doesn't support valid ISO-8601 alternative for midnight #102450

Copy link
Copy link
@TizzySaurus

Description

@TizzySaurus
Issue body actions

Bug report

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:

>>> import datetime
>>> datetime.datetime(2022, 1, 2, 24, 0, 0)  # should be equivalent to 2022-01-03 00:00:00
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    datetime.datetime(2022, 3, 4, 24, 0, 0)
ValueError: hour must be in 0..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.

I imagine the check_time_args C function would also have to be updated.

Your environment

  • CPython versions tested on: 3.9.12, 3.10.7, 3.11.2 (presumably applies to all)
  • Operating system and architecture: MacOS Ventura arm64 (presumably applies to all)

Linked PRs

decorator-factory, weddige and imrehg

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    Status

    Done
    Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.