1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix ActiveSupport::TimeWithZone#localtime

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
This commit is contained in:
Thomas Balthazar 2016-10-01 13:26:15 +02:00
parent 268a5bb010
commit 607a6c7a9a
3 changed files with 30 additions and 1 deletions

View file

@ -1,3 +1,25 @@
* 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.
*Thomas Balthazar*
* Fix `ActiveSupport::TimeWithZone#in` across DST boundaries.
Previously calls to `in` were being sent to the non-DST aware

View file

@ -80,7 +80,8 @@ module ActiveSupport
# Returns a <tt>Time</tt> instance of the simultaneous time in the system timezone.
def localtime(utc_offset = nil)
@localtime ||= utc.getlocal(utc_offset)
@localtime ||= {}
@localtime[utc_offset] ||= utc.getlocal(utc_offset)
end
alias_method :getlocal, :localtime

View file

@ -54,6 +54,12 @@ class TimeWithZoneTest < ActiveSupport::TestCase
assert_instance_of Time, @dt_twz.localtime
end
def test_localtime_with_offset
assert_equal 0, @twz.localtime.gmt_offset
assert_equal (-3600), @twz.localtime(-3600).gmt_offset
assert_equal (-7200), @twz.localtime(-7200).gmt_offset
end
def test_utc?
assert_equal false, @twz.utc?