This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author huonw
Recipients huonw
Date 2021-12-23.23:27:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1640302049.86.0.995317132949.issue46169@roundup.psfhosted.org>
In-reply-to
Content
The documentation suggests that two datetimes that represent the same moment should compare equal, even if they have different timezones: "If both comparands are aware and have different tzinfo attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from self.utcoffset())." (below https://docs.python.org/3/library/datetime.html#datetime.datetime.fold)

This doesn't seem to be true for == with ZoneInfo timezones, even though it is true for <= and >=, and -, meaning these seem to violate mathematical laws/expectations:

    from zoneinfo import ZoneInfo
    from datetime import datetime, timezone

    dt_utc = datetime(2020, 11, 1, 8, tzinfo=timezone.utc)
    dt_local = dt_utc.astimezone(ZoneInfo("America/Los_Angeles"))
    print(f"{dt_local == dt_utc = }")
    print(f"{dt_local <= dt_utc = }")
    print(f"{dt_local >= dt_utc = }")
    print(f"{dt_local - dt_utc = }")

Output:

    dt_local == dt_utc = False
    dt_local <= dt_utc = True
    dt_local >= dt_utc = True
    dt_local - dt_utc = datetime.timedelta(0)

Tested with:

- macOS 11.4; Python 3.9.7, 3.10.1, 3.11.0a3
- Linux; via docker image python:3.9.7

Full test including comparisons to python-dateutil (same behaviour as ZoneInfo) and pytz (== gives True as expected) is attached.

Comparing timestamps for exact equality is potentially unusual, but we use it extensively in tests for some time-handling functions.
History
Date User Action Args
2021-12-23 23:27:29huonwsetrecipients: + huonw
2021-12-23 23:27:29huonwsetmessageid: <1640302049.86.0.995317132949.issue46169@roundup.psfhosted.org>
2021-12-23 23:27:29huonwlinkissue46169 messages
2021-12-23 23:27:29huonwcreate