From 3e47c9fb120a70cf3bce57516d31c7c492b0e8f8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 17 Jun 2010 14:10:51 -0700 Subject: [PATCH] when the timezone is nil, a TimeWithZone object should not be constructed. [#4881 state:resolved] --- .../lib/active_support/core_ext/date_time/zones.rb | 2 ++ .../lib/active_support/core_ext/time/zones.rb | 2 ++ activesupport/test/core_ext/time_with_zone_test.rb | 7 +++++++ 3 files changed, 11 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date_time/zones.rb b/activesupport/lib/active_support/core_ext/date_time/zones.rb index 98565e6..6002d4a 100644 --- a/activesupport/lib/active_support/core_ext/date_time/zones.rb +++ b/activesupport/lib/active_support/core_ext/date_time/zones.rb @@ -12,6 +12,8 @@ class DateTime # # DateTime.new(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 def in_time_zone(zone = ::Time.zone) + return self unless zone + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.__send__(:get_zone, zone)) end end diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index adc9fe3..a02402a 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -73,6 +73,8 @@ class Time # # Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 def in_time_zone(zone = ::Time.zone) + return self unless zone + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.__send__(:get_zone, zone)) end end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 2cf5bd6..cf11f4d 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -737,6 +737,13 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end end + def test_nil_time_zone + Time.use_zone nil do + assert !@t.in_time_zone.respond_to?(:period), 'no period method' + assert !@dt.in_time_zone.respond_to?(:period), 'no period method' + end + end + def test_in_time_zone_with_argument 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 -- 1.7.0.6