Skip to content

datetime: Comparision does not correctly account for folded time #146236

@jepler

Description

@jepler

Bug report

Bug description:

The documentation of datetime ordering predicates says both that "datetime1 is considered less than datetime2 when datetime1 precedes datetime2 in time, taking into account the time zone" but also "If both comparands are aware, and have the same tzinfo attribute, the tzinfo and fold attributes are ignored"

I believe that ignoring fold, though documented, is incorrect, and contradicts the earlier "precedes ... in time" explanation of what the expression dt1 < dt2 means. If avoiding a conversion to UTC is an important performance optimization, a possibility is to continue to avoid it provided that the fold= and tzinfo= values are the same in both comparands.

>>> from datetime import datetime
>>> import zoneinfo
>>> la = zoneinfo.ZoneInfo("America/Los_Angeles")
>>> t1 = datetime.fromtimestamp(1604221200.0, tz=la)
>>> t2 = datetime.fromtimestamp(1604217660.0, tz=la)
>>> t1 < t2
True
>>> t1.timestamp() < t2.timestamp()
False

Tested on: 3.13.5, 3.15.0a2

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions