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
WIP fix for beginning_of_minute removes time zone information in some specific scenarios #43583 #43592
Conversation
So I've been taking a look into the code and and found some interesting behaviour. When writing the test I was only able to recreate the issue using Looks like its something to do with this:
Here is the last section of rails/activesupport/lib/active_support/core_ext/time/calculations.rb Lines 158 to 168 in 396b22d
In the In the Removing the |
Hmmm, this is confusing. It looks like If you get rid of this branch what happens elsif zone
::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec) |
Removing that branch makes the new test pass along with all of the other direct change tests. However it does make lots of tests fail that cross a daylight savings time boundary using methods like
|
@@ -454,6 +454,9 @@ def test_offset_change | |||
assert_equal 10, Time.new(2005, 2, 22, 15, 15, 0, "-08:00").change(nsec: 10).nsec | |||
assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(usec: 1000000) } | |||
assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(nsec: 1000000000) } | |||
with_env_tz "GB" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with_env_tz "GB" do | |
with_env_tz "BST" do |
for the record, this makes the test pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried a bunch of other things but couldn't find any other way to get it passing. Two questions
- Why
GB
as the time zone? Where did that come from? - I think in general you should use
Time.zone.parse
for this sort of thing - any reason you're not here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
I believe
GB
is what should be there, based on others not being timezones,US/Eastern
andNZ
etc. It might pass withBST
as its not a valid region, so defaults to another region which does leave daylight savings time at the same time as theGB
region, but thats just me guessing. All thewith_env_tz "GB"
does is setsENV['TZ'] = "GB"
, so I think it has to be one of these codes: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones -
We only noticed the issue during the hour that the clocks went back in the UK, in the production code we were using we had
Time.now.beginning_of_minute
, but givenTime#beginning_of_minute
just callschange(sec: 0)
the problem is not actually withTime#beginning_of_minute
. UsingTime.zone
was the only way to recreate it in a test. I had managed to also recreate it using Timecop but I was not going to introduce that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I'm suggesting maybe it should be "BST"
is because that's what Time.parse(...).zone
is returning. But I'm not really sure.
FWIW in our production apps we don't set ENV["TZ"]
at all, and we use https://www.rubydoc.info/gems/rubocop/0.57.2/RuboCop/Cop/Rails/TimeZone to remind people to use Time.zone.now...
instead of Time.now...
.
All of which is to say, I'm not really sure if this is a bug or not just yet.
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Summary
WIP on a fix for #43583, provides a failing test for the bug as a start for more development.