diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 81c5574dbff11610142025e53328a67e5baaf68a..b10c9614d8ba63a9bed33d32b993a429d1851a9d 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Time#in_time_zone handles Time.local instances correctly [Geoff Buesing] + * Pruning unneeded Time#change_time_zone_to_current. Enhanced docs to #change_time_zone to explain the difference between this method and #in_time_zone [Geoff Buesing] * TimeZone#new method renamed #local; when used with Time.zone, constructor now reads: Time.zone.local() [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 4705e93f27033bfc2aeab9135c317c14eb55ea6f..18af28edc87d59c5b54b4fd67ed7f5509367e066 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -42,15 +42,13 @@ def get_zone(time_zone) end end - # Returns the simultaneous time in the supplied zone. self is assumed to be in UTC regardless of constructor. - # - # Examples: + # Returns the simultaneous time in the supplied zone. Examples: # # 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(self, get_zone(zone)) + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, get_zone(zone)) end # Returns the simultaneous time in Time.zone diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 3f0ec0720bec1f70085795c8afdff9e4d4eb4bbd..14a96169ddb7ecc81dce1837934db539027699e0 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -174,6 +174,15 @@ def test_in_time_zone end end end + + def test_in_time_zone_with_time_local_instance + silence_warnings do # silence warnings raised by tzinfo gem + with_env_tz 'US/Eastern' do + time = Time.local(1999, 12, 31, 19) # == Time.utc(2000) + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect + end + end + end def test_in_current_time_zone Time.use_zone 'Alaska' do @@ -228,5 +237,13 @@ def test_time_zone_setter_is_thread_safe assert_equal TimeZone['Hawaii'], t2[:time_zone] end end + + protected + def with_env_tz(new_tz = 'US/Eastern') + old_tz, ENV['TZ'] = ENV['TZ'], new_tz + yield + ensure + old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') + end end end