提交 52b57c66 编写于 作者: G Geoff Buesing

TimeWithZone instances correctly enforce DST rules. Adding TimeZone#period_for_utc

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9006 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 3d2177df
*SVN*
* TimeWithZone instances correctly enforce DST rules. Adding TimeZone#period_for_utc [Geoff Buesing]
* test_time_with_datetime_fallback expects DateTime.local_offset instead of DateTime.now.offset [Geoff Buesing]
* Adding TimeWithZone #marshal_dump and #marshal_load [Geoff Buesing]
......
......@@ -27,7 +27,7 @@ def utc
# Returns the underlying TZInfo::TimezonePeriod for the local time
def period
@period ||= get_period_for_local
@period ||= time_zone.period_for_utc(utc)
end
# Returns the simultaneous time in the specified zone
......@@ -150,6 +150,15 @@ def -(other)
end
end
%w(asctime day hour min mon sec usec wday yday year).each do |name|
define_method(name) do
time.__send__(name)
end
end
alias_method :ctime, :asctime
alias_method :mday, :day
alias_method :month, :mon
def to_a
time.to_a[0, 8].push(dst?, zone)
end
......@@ -206,20 +215,9 @@ def respond_to?(sym)
# Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone
def method_missing(sym, *args, &block)
result = time.__send__(sym, *args, &block)
result = result.change_time_zone(time_zone) if result.acts_like?(:time)
result = utc.__send__(sym, *args, &block)
result = result.in_time_zone(time_zone) if result.acts_like?(:time)
result
end
private
def get_period_for_local
t = time
begin
time_zone.period_for_local(t, true)
rescue ::TZInfo::PeriodNotFound # failover logic from TzTime
t -= 1.hour
retry
end
end
end
end
......@@ -211,6 +211,11 @@ def local_to_utc(time, dst=true)
tzinfo.local_to_utc(time, dst)
end
# Available so that TimeZone instances respond like TZInfo::Timezone instances
def period_for_utc(time)
tzinfo.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)
......
......@@ -165,6 +165,38 @@ def test_minus_with_time_with_zone
assert_equal 86_400.0, twz2 - twz1
end
def test_plus_and_minus_enforce_spring_dst_rules
utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start
twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
assert_equal Time.utc(2006,4,2,1,59,59), twz.time
assert_equal false, twz.dst?
assert_equal 'EST', twz.zone
twz = twz + 1
assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT
assert_equal true, twz.dst?
assert_equal 'EDT', twz.zone
twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST
assert_equal Time.utc(2006,4,2,1,59,59), twz.time
assert_equal false, twz.dst?
assert_equal 'EST', twz.zone
end
def test_plus_and_minus_enforce_fall_dst_rules
utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end
twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
assert_equal Time.utc(2006,10,29,1,59,59), twz.time
assert_equal true, twz.dst?
assert_equal 'EDT', twz.zone
twz = twz + 1
assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST
assert_equal false, twz.dst?
assert_equal 'EST', twz.zone
twz = twz - 1
assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT
assert_equal true, twz.dst?
assert_equal 'EDT', twz.zone
end
def test_to_a
assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a
end
......@@ -219,9 +251,14 @@ def test_marshal_dump_and_load
end
def test_method_missing_with_non_time_return_value
assert_equal 1999, @twz.year
assert_equal 12, @twz.month
assert_equal 31, @twz.day
twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
assert_equal 1999, twz.year
assert_equal 12, twz.month
assert_equal 31, twz.day
assert_equal 14, twz.hour
assert_equal 18, twz.min
assert_equal 17, twz.sec
assert_equal 500, twz.usec
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册