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: datetime.__eq__ returns true when timezones don't match
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Woodz, belopolsky, mark.dickinson, p-ganssle
Priority: normal Keywords:

Created on 2021-02-16 04:52 by Woodz, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg387087 - (view) Author: Richard Wise (Woodz) Date: 2021-02-16 04:52
from datetime import datetime, timezone, timedelta

datetime_in_sgt = datetime(2021, 2, 16, 8, 0, 0, tzinfo=timezone(timedelta(hours=8)))
datetime_in_utc = datetime(2021, 2, 16, 0, 0, 0, tzinfo=timezone.utc)

print(datetime_in_sgt == datetime_in_utc)

Expected: False
Actual: True

Although these two datetimes represent the same instant on the timeline, they are not identical because they use different timezones. This means that when unit testing timezone handling, tests will incorrectly pass despite data being returned in UTC instead of the requested timezone, so we need to write code such as this:

# Timestamp comparison
self.assertEqual(datetime_in_sgt, datetime_in_utc)
# Timezone comparison
self.assertEqual(datetime_in_sgt.tzinfo, datetime_in_utc.tzinfo)

This is confusing and non-intuitive.

For examples of how other languages handle such comparison, can refer to: https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#equals-java.lang.Object- and 
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#equals-java.lang.Object-
msg387100 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-02-16 10:00
Hi Richard. Thanks for the report. Python's working as designed (and as documented and tested) here. IOW, the behaviour is deliberate - this isn't a bug.

Any change in behaviour would have to target 3.10 at the earliest (and it's already quite late in the release process for 3.10). A couple of questions: (1) what would you want the comparisons `datetime_in_sgt <= datetime_in_utc` and `datetime_in_utc <= datetime_in_sgt` to give? (2) How would you propose to change the behaviour without breaking existing code that makes use of the current behaviour?

> This is confusing and non-intuitive.

That's a rather subjective judgement. I'd personally find the behaviour you suggest non-intuitive (and I find the behaviour of Java's order comparisons on ZonedDateTime to be highly non-intuitive). We'd need something a bit more objective and/or widely supported to justify a behaviour change.
msg387207 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-02-18 09:54
I'm going to close here. While changing the behaviour isn't completely out of the question, it would need (a) a clear proposal on how to deal with order comparisons and backwards compatibility, and (b) strong support from the community. A PEP is probably the best way forward, for anyone who wants to go this route.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87403
2021-02-18 09:54:45mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg387207

stage: resolved
2021-02-16 10:09:46xtreaksetnosy: + belopolsky, p-ganssle
2021-02-16 10:00:06mark.dickinsonsetnosy: + mark.dickinson
messages: + msg387100
2021-02-16 04:52:32Woodzcreate