New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime.__eq__ returns true when timezones don't match #87403
Comments
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 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 |
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
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. |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: