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.

classification
Title: Same-moment datetimes with different ZoneInfo timezones are not considered ==
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, belopolsky, huonw
Priority: normal Keywords:

Created on 2021-12-23 23:27 by huonw, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
tz.py huonw, 2021-12-23 23:27
dt_equality.py ajaksu2, 2021-12-24 03:29
Messages (2)
msg409113 - (view) Author: Huon Wilson (huonw) * Date: 2021-12-23 23:27
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.
msg409119 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2021-12-24 03:29
Confirmed for 3.11 in Windows. 

The C datetime code can be trivially fixed so your equality test returns True, but there are two Python tests that depend on current behavior so it might not be so easy. They were added with current code in issue 24773, to implement PEP 495 -- Local Time Disambiguation.

Also, using a timezone implementation from the test suite makes the equality work, so maybe the bug depends on zoneinfo.

If you change datetimemodule.c line 5761 from "diff = 1;" to "diff = 0;", two tests fail and your code works.

See attached file for the tests that fail and using the timezone implementation mentioned above.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90327
2021-12-24 14:33:12ajaksu2setnosy: + belopolsky
2021-12-24 03:29:51ajaksu2setfiles: + dt_equality.py
nosy: + ajaksu2
messages: + msg409119

2021-12-23 23:27:29huonwcreate