Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
Force a period when local times are ambiguous
Browse files Browse the repository at this point in the history
This commit includes the essence of
rails#17409.
  • Loading branch information
jbarnette authored and Charlie Somerville committed Nov 10, 2014
1 parent 726e996 commit 7a559d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -338,7 +338,7 @@ def get_period_and_ensure_valid_local_time
# so transfer time values to a utc constructor if necessary
@time = transfer_time_values_to_utc_constructor(@time) unless @time.utc?
begin
@time_zone.period_for_local(@time)
@time_zone.period_for_local(@time) { |periods| periods.first }
rescue ::TZInfo::PeriodNotFound
# time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again
@time += 1.hour
Expand Down
8 changes: 4 additions & 4 deletions activesupport/lib/active_support/values/time_zone.rb
Expand Up @@ -309,8 +309,8 @@ def utc_to_local(time)
end

# Adjust the given time to the simultaneous time in UTC. Returns a Time.utc() instance.
def local_to_utc(time, dst=true)
tzinfo.local_to_utc(time, dst)
def local_to_utc(time, dst=true, &block)
tzinfo.local_to_utc(time, dst, &block)
end

# Available so that TimeZone instances respond like TZInfo::Timezone instances
Expand All @@ -319,8 +319,8 @@ def period_for_utc(time)
end

# Available so that TimeZone instances respond like TZInfo::Timezone instances
def period_for_local(time, dst=true)
tzinfo.period_for_local(time, dst)
def period_for_local(time, dst=true, &block)
tzinfo.period_for_local(time, dst, &block)
end

def self.find_tzinfo(name)
Expand Down
11 changes: 11 additions & 0 deletions activesupport/test/time_zone_test.rb
Expand Up @@ -136,6 +136,17 @@ def test_local_enforces_fall_dst_rules
assert_equal 'EDT', twz.zone
end

def test_local_handles_non_dst_clock_set_back
# If the clocks are set back during a transition from one non-DST observance
# to another, the time of the transition will be ambiguous. The earlier
# observance will be selected to mirror behaviour of DST to non-DST transitions.
# Such a transition occurred at 02:00 local time in Moscow on 26 October 2014.
zone = ActiveSupport::TimeZone['Moscow']
twz = zone.local(2014,10,26,1)
assert_equal Time.utc(2014,10,26,1), twz.time
assert_equal Time.utc(2014,10,25,21), twz.utc
end

def test_at
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
secs = 946684800.0
Expand Down

0 comments on commit 7a559d3

Please sign in to comment.