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
Comparing TimeWithZones to Dates returns incorrect results depending on your time zone #30870
Comments
Hi @davidbalbert , you are right about the actual behaviour, but it is expected behaviour imo. And you are right that datetime.utc
# => 2017-10-19 00:00:00 UTC
date.in_time_zone.utc
# => 2017-10-19 04:00:00 UTC If you go level deeper, you are actually comparing following integers: datetime.to_i
# => 1508371200
time_with_zone.to_i
# => 1508385600 I may be wrong, but Hope that helps. |
One more thing: you should be always using zone methods in Rails unless you really want to use system time or UTC. And actually yes, it might be confusing, lots of people confuse |
I think you have misread my bug report. This issue is not with how Specifically: when comparing a Date and a TimeWithZone, the comparison returns different results depending on the time zone of the TimeWithZone. This is because the Date is automatically coerced into a timestamp at midnight UTC. I think the comparison behavior would be more intuitive if the Date were coerced into a timestamp with the same time zone as the TimeWithZone. As a possibly clearer example, consider the following code: ActiveSupport::TimeZone["America/New_York"].parse("12:00am 2017-10-19") <= Date.parse("2017-10-19") # => false
ActiveSupport::TimeZone["America/New_York"].parse("11:59pm 2017-10-18") <= Date.parse("2017-10-19") # => false You would expect both lines to return true because you'd expect the comparison to take place in the time zone of the TimeWithZone. Because the code to compare dates and time with zones is provided by ActiveSupport, I would expect it to be time zone aware. |
@davidbalbert actually, what's happening in your example that the TWZ instance is being converted to UTC Time instance and then the Active Support core extension
The reason that the coercion was added was for compatibility between
I'm more inclined to not coerce for comparing dates with times because behaviour will likely be dependent on which side of the operators the respective types fall. Also since
The latter
In your example the best comparison would be to use ActiveSupport::TimeZone["America/New_York"].parse("12:00am 2017-10-19") <= "2017-10-19".in_time_zone("America/New_York") # => true Given that the application context is likely to change the behaviour desired I'm loathe to introduce any kind of breaking change unless we can clearly define what the correct behaviour should be. |
Sounds like there's not a ton of interest in fixing this, so I will close the issue. Thanks for looking into it. |
Depending on the value of
Time.zone
, comparing aTimeWithZone
to aDate
might return incorrect (or at least inconsistent) results. These results are different than the result of comparing aDateTime
to aDate
.Steps to reproduce
Consider the following code (automated test case available):
Expected behavior
All comparisons return true in all time zones.
Actual behavior
In time zones that are less than UTC (e.g. "America/New_York"), the
DateTime
comparisons return true, but theTimeWithZone
comparisons return false.If you switched the direction of the comparisons (i.e. switch
<=
to>=
and vice versa), I believe time zones greater than UTC would exhibit the error, though I haven't tested.I believe what is happening is that at some point the
Date
gets converted to midnight UTC regardless of the value ofTime.zone
. Midnight in "America/New_York" (UTC-4) is greater than midnight UTC, and the comparison fails.It's possible this behavior is by design. The
Date
to "time" coercion could be defined to mean midnight UTC on the given date regardless of the time zone of the time you're comparing it to, but I think that leads to pretty confusing behavior.System configuration
Rails version: I tested on 5.1.4 and on master
Ruby version: 2.4.2
The text was updated successfully, but these errors were encountered: