Merge pull request #36 from github/3-2-github+ambiguous-times

Force a period when local times are ambiguous
This commit is contained in:
Charlie Somerville
2014-11-10 15:26:47 +11:00
3 changed files with 16 additions and 5 deletions

View File

@@ -338,7 +338,7 @@ module ActiveSupport
# 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

View File

@@ -309,8 +309,8 @@ module ActiveSupport
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
@@ -319,8 +319,8 @@ module ActiveSupport
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)

View File

@@ -136,6 +136,17 @@ class TimeZoneTest < Test::Unit::TestCase
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