diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/zones.rb | 35 | ||||
-rw-r--r-- | activesupport/lib/active_support/time_with_zone.rb | 9 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 50 |
4 files changed, 45 insertions, 51 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 8d3a3c2bd0..a569a57f62 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Time, DateTime and TimeWithZone #in_time_zone defaults to Time.zone. Removing now unneeded #in_current_time_zone [Geoff Buesing] + * TZInfo caches Timezone instances in its own internal hash cache, so TimeZone::MAPPING doesn't need to cache them as well [Geoff Buesing] * Adding TimeZone#parse [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index 5d8e085ef6..ee1d33fbc8 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -19,11 +19,11 @@ module ActiveSupport #:nodoc: # Accepts either a Rails TimeZone object, a string that identifies a # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object # - # Any Time or DateTime object can use this default time zone, via #in_current_time_zone. + # Any Time or DateTime object can use this default time zone, via #in_time_zone. # Example: # - # Time.zone = 'Hawaii' # => 'Hawaii' - # Time.utc(2000).in_current_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + # Time.zone = 'Hawaii' # => 'Hawaii' + # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00 def zone=(time_zone) Thread.current[:time_zone] = get_zone(time_zone) end @@ -43,24 +43,21 @@ module ActiveSupport #:nodoc: end end - # Returns the simultaneous time in the supplied zone. Examples: + # Returns the simultaneous time in Time.zone. Example: # - # t = Time.utc(2000) # => Sat Jan 01 00:00:00 UTC 2000 - # t.in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 - # t.in_time_zone('Hawaii') # => Fri, 31 Dec 1999 14:00:00 HST -10:00 - def in_time_zone(zone) - ActiveSupport::TimeWithZone.new(utc? ? self : getutc, get_zone(zone)) - end - - # Returns the simultaneous time in Time.zone - def in_current_time_zone - ::Time.zone ? in_time_zone(::Time.zone) : self + # Time.zone = 'Hawaii' # => 'Hawaii' + # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + # + # This method is similar to Time#localtime, except that it uses Time.zone as the local zone + # instead of the operating system's time zone. + # + # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument, + # and the conversion will be based on that zone instead of Time.zone. Example: + # + # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 + def in_time_zone(zone = ::Time.zone) + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.send!(:get_zone, zone)) end - - private - def get_zone(time_zone) - ::Time.send!(:get_zone, time_zone) - end end end end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 984b1e43cb..82af1653ef 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -29,17 +29,12 @@ module ActiveSupport @period ||= time_zone.period_for_utc(@utc) end - # Returns the simultaneous time in the specified zone - def in_time_zone(new_zone) + # Returns the simultaneous time in Time.zone, or the specified zone + def in_time_zone(new_zone = ::Time.zone) return self if time_zone == new_zone utc.in_time_zone(new_zone) end - # Returns the simultaneous time in Time.zone - def in_current_time_zone - utc.in_current_time_zone - end - # Returns a Time.local() instance of the simultaneous time in your system's ENV['TZ'] zone def localtime utc.getlocal diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index e828e62f9b..8a868b9c62 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -21,8 +21,14 @@ uses_tzinfo 'TimeWithZoneTest' do def test_time_zone assert_equal @time_zone, @twz.time_zone end - + def test_in_time_zone + Time.use_zone 'Alaska' do + assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone + end + end + + def test_in_time_zone_with_argument assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone('Alaska') end @@ -30,12 +36,6 @@ uses_tzinfo 'TimeWithZoneTest' do assert_equal @twz.object_id, @twz.in_time_zone(TimeZone['Eastern Time (US & Canada)']).object_id end - def test_in_current_time_zone - Time.use_zone 'Alaska' do - assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_current_time_zone - end - end - def test_utc? assert_equal false, @twz.utc? assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']).utc? @@ -298,9 +298,26 @@ uses_tzinfo 'TimeWithZoneTest' do def teardown Time.zone = nil end - + def test_in_time_zone silence_warnings do # silence warnings raised by tzinfo gem + Time.use_zone 'Alaska' do + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect + end + Time.use_zone 'Hawaii' do + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect + end + Time.use_zone nil do + assert_equal @t, @t.in_time_zone + assert_equal @dt, @dt.in_time_zone + end + end + end + + def test_in_time_zone_with_argument + silence_warnings do # silence warnings raised by tzinfo gem Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect @@ -321,23 +338,6 @@ uses_tzinfo 'TimeWithZoneTest' do end end end - - def test_in_current_time_zone - silence_warnings do # silence warnings raised by tzinfo gem - Time.use_zone 'Alaska' do - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_current_time_zone.inspect - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_current_time_zone.inspect - end - Time.use_zone 'Hawaii' do - assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_current_time_zone.inspect - assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_current_time_zone.inspect - end - Time.use_zone nil do - assert_equal @t, @t.in_current_time_zone - assert_equal @dt, @dt.in_current_time_zone - end - end - end def test_use_zone Time.zone = 'Alaska' |