-
Notifications
You must be signed in to change notification settings - Fork 549
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
Timezone-aware datetime comparisons ignore the timezone #1211
Comments
went undetected because of chronotope/chrono#1211
Yes, I was somewhat surprised by this also, see #1088 (review). In many cases it makes sense that In any case this is not a bug, it is a design choice. Changing it would break the code of others. We can work on improving the documentation here. |
We should definitely change this in 0.5 IMO, even if it's just removing the |
Oh, I didn't expect this to be intentional ! Are there cases in which it makes sense for two datetimes that have the same hour, but in different timezones to be considered equal? I'm struggling to imagine an application in which this behavior would be desired. I think few people read the docs of the PartialEq implementation, because it's generally obvious. So changing the documentation wouldn't really solve the problem. At least in my case, I spent two hours searching for a bug in my code, because I couldn't imagine two different dates being equal in PartialEq. |
I agree that it's a surprising choice, and not one that I would have made (but it was made before I got involved with this library). But due to compatibility, we can't easily change it now. |
Maybe you already realize this, but just in case: the Values compare equal when they describe the same moment in time. And it doesn't matter for the comparison if a type includes the full timezone name, only an offset, is normalized to UTC, or some other format. I don't have strong opinion on if this is something that should change. |
@lovasoa Do you already have a workaround? What would be the comparison you need? Should besides the timestamp the offset be equal, the type, of both? |
In my case, the problem occurred in the tests, allowing the tests to pass even when the code was returning completely incoherent timezones. Currently my only workaround is to be careful and to keep in mind that the tests won't catch this kind of errors. I think the most basic thing, just testing that everything is exactly equal (and of the same type), would make the most sense. I would not mind the equality code trying to be smart about timezones, though. But just ignoring them completely is a real footgun. |
This occurred in my code where I assumed cross-tz date time |
@msdrigg Maybe you want to double-check? Because chrono does compare the actual instant, all values are converted to UTC internally. |
Ahh... well my bad. I saw
and searched for "datetime comparison issue chrono" and assumed it was a problem as soon as I found this issue. I'll go find the bug in my own code now... |
In the current version of chrono, timezone-aware datetime comparisons ignore the timezone.
chrono/src/datetime/mod.rs
Lines 1167 to 1171 in d754c5c
(the timezone should be compared here too)
This means that
even though these represent completely different times.
It just took me 2 hours to realise that the bug was not in my code but in chrono itself
The text was updated successfully, but these errors were encountered: