Skip to content
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

Fix ActiveSupport::TimeWithZone#localtime #26677

Merged
merged 1 commit into from
Oct 1, 2016
Merged

Fix ActiveSupport::TimeWithZone#localtime #26677

merged 1 commit into from
Oct 1, 2016

Conversation

tbalthazar
Copy link
Contributor

Summary

Fix ActiveSupport::TimeWithZone#localtime when called with different utc_offset values.

Previously memoization in localtime wasn't taking the utc_offset parameter into account when returning a cached value. It now caches the computed value depending on the utc_offset parameter, e.g:

Time.zone = "US/Eastern"

t = Time.zone.local(2016,5,2,11)
# => Mon, 02 May 2016 11:00:00 EDT -04:00

t.localtime(-7200)
# => 2016-05-02 13:00:00 -0200

t.localtime(-3600)
# => 2016-05-02 14:00:00 -0100

Fixes #26644.

Previously memoization in `localtime` wasn't taking the `utc_offset`
parameter into account when returning a cached value. It now caches the
computed value depending on the `utc_offset` parameter, e.g:

    Time.zone = "US/Eastern"

    t = Time.zone.local(2016,5,2,11)
    # => Mon, 02 May 2016 11:00:00 EDT -04:00

    t.localtime(-7200)
    # => 2016-05-02 13:00:00 -0200

    t.localtime(-3600)
    # => 2016-05-02 14:00:00 -0100
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@pixeltrix pixeltrix merged commit 9ce2d1b into rails:master Oct 1, 2016
@pixeltrix
Copy link
Contributor

@tbalthazar thanks! 👍

@pixeltrix
Copy link
Contributor

Backported to 5-0-stable in f7e9f1a - the fix I made for 4-2-stable to address the original performance issue isn't affected because it just caches to_time which doesn't take a utc_offset.

@@ -54,6 +54,12 @@ def test_localtime
assert_instance_of Time, @dt_twz.localtime
end

def test_localtime_with_offset
assert_equal 0, @twz.localtime.gmt_offset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pixeltrix this isn't passing for me

  1) Failure:
TimeWithZoneTest#test_localtime_with_offset [test/core_ext/time_with_zone_test.rb:58]:
Expected: 0
  Actual: 37800

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build was green - hmm. What timezone are you in?

pixeltrix added a commit that referenced this pull request Oct 2, 2016
Turns out trying to cache on localtime with arguments is too hard
so we'll do it on DateAndTime::Compatibility#to_time instead.

This reverts commit 9ce2d1b, reversing
changes made to 53ede1a.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants