aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeoff Buesing <gbuesing@gmail.com>2008-03-17 02:40:28 +0000
committerGeoff Buesing <gbuesing@gmail.com>2008-03-17 02:40:28 +0000
commit77ee522bf6c97352485fad3b72866f7ab424ecaf (patch)
treeae6684b38fce4c18cc46e4103f32cee92f8fc1da
parent1b41178a836ae84cf62a11ebd6bd640fd0d09566 (diff)
downloadrails-77ee522bf6c97352485fad3b72866f7ab424ecaf.tar.gz
rails-77ee522bf6c97352485fad3b72866f7ab424ecaf.tar.bz2
rails-77ee522bf6c97352485fad3b72866f7ab424ecaf.zip
TimeWithZone caches TZInfo::TimezonePeriod used for time conversion so that it can be reused, and enforces DST rules correctly when instance is created from a local time
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9040 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb34
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb10
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb25
4 files changed, 55 insertions, 16 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index cd0106c0b5..d72fdadf3f 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* TimeWithZone caches TZInfo::TimezonePeriod used for time conversion so that it can be reused, and enforces DST rules correctly when instance is created from a local time [Geoff Buesing]
+
* Fixed that BufferedLogger should create its own directory if one doesn't already exist #11285 [lotswholetime]
* Fix Numeric time tests broken by DST change by anchoring them to fixed times instead of Time.now. Anchor TimeZone#now DST test to time specified with Time.at instead of Time.local to work around platform differences with Time.local and DST representation [Geoff Buesing]
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 008c7f151b..68585e8273 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -5,29 +5,28 @@ module ActiveSupport
include Comparable
attr_reader :time_zone
- def initialize(utc_time, time_zone, local_time = nil)
- @utc = utc_time
- @time = local_time
- @time_zone = time_zone
+ def initialize(utc_time, time_zone, local_time = nil, period = nil)
+ @utc, @time_zone, @time = utc_time, time_zone, local_time
+ @period = @utc ? period : get_period_and_ensure_valid_local_time
end
# Returns a Time instance that represents the time in time_zone
def time
- @time ||= time_zone.utc_to_local(@utc)
+ @time ||= utc_to_local
end
# Returns a Time instance that represents the time in UTC
def utc
- @utc ||= time_zone.local_to_utc(@time)
+ @utc ||= local_to_utc
end
alias_method :comparable_time, :utc
alias_method :getgm, :utc
alias_method :getutc, :utc
alias_method :gmtime, :utc
- # Returns the underlying TZInfo::TimezonePeriod for the local time
+ # Returns the underlying TZInfo::TimezonePeriod
def period
- @period ||= time_zone.period_for_utc(utc)
+ @period ||= time_zone.period_for_utc(@utc)
end
# Returns the simultaneous time in the specified zone
@@ -214,5 +213,24 @@ module ActiveSupport
result = result.in_time_zone(time_zone) if result.acts_like?(:time)
result
end
+
+ private
+ def get_period_and_ensure_valid_local_time
+ @time_zone.period_for_local(@time)
+ 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
+ retry
+ end
+
+ # Replicating logic from TZInfo::Timezone#utc_to_local because we want to cache the period in an instance variable for reuse
+ def utc_to_local
+ ::TZInfo::TimeOrDateTime.wrap(utc) {|utc| period.to_local(utc)}
+ end
+
+ # Replicating logic from TZInfo::Timezone#local_to_utc because we want to cache the period in an instance variable for reuse
+ def local_to_utc
+ ::TZInfo::TimeOrDateTime.wrap(time) {|time| period.to_utc(time)}
+ end
end
end
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index a52eee3e7c..401b669a0a 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -183,14 +183,8 @@ class TimeZone
# Time.zone = "Hawaii" # => "Hawaii"
# Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
def local(*args)
- t = Time.utc_time(*args)
- begin
- result = local_to_utc(t)
- rescue TZInfo::PeriodNotFound
- t += 1.hour
- retry
- end
- result.in_time_zone(self)
+ time = Time.utc_time(*args)
+ ActiveSupport::TimeWithZone.new(nil, self, time)
end
# Returns an ActiveSupport::TimeWithZone instance representing the current time
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index a3566796a7..5f1a002071 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -256,6 +256,31 @@ uses_tzinfo 'TimeWithZoneTest' do
assert_equal 17, twz.sec
assert_equal 500, twz.usec
end
+
+ def test_utc_to_local_conversion_saves_period_in_instance_variable
+ assert_nil @twz.instance_variable_get('@period')
+ @twz.time
+ assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period')
+ end
+
+ def test_instance_created_with_local_time_returns_correct_utc_time
+ twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19))
+ assert_equal Time.utc(2000), twz.utc
+ end
+
+ def test_instance_created_with_local_time_enforces_spring_dst_rules
+ twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST
+ assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
+ end
+
+ def test_instance_created_with_local_time_enforces_fall_dst_rules
+ twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST
+ assert_equal Time.utc(2006,10,29,1), twz.time
+ assert_equal true, twz.dst?
+ assert_equal 'EDT', twz.zone
+ end
end
class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase