aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb26
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb5
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb43
4 files changed, 59 insertions, 17 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 095a5c00bd..ebb25b54e6 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*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]
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index c6fa118b55..0f5d0a2ee3 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -27,7 +27,7 @@ module ActiveSupport
# 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 @@ module ActiveSupport
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 @@ module ActiveSupport
# 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
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index 4f2fc5e44b..ced823f052 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -212,6 +212,11 @@ class TimeZone
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)
end
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index c7b71f4ffe..74fbc56595 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -165,6 +165,38 @@ uses_tzinfo 'TimeWithZoneTest' do
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 @@ uses_tzinfo 'TimeWithZoneTest' do
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