Skip to content

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

@TizzySaurus

Description

@TizzySaurus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy