From 54ccdd334385fea706eb8d5b3ed95d7102a9d0d4 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Mon, 17 Mar 2008 05:50:13 +0000 Subject: Time, DateTime and TimeWithZone #in_time_zone defaults to Time.zone. Removing now unneeded #in_current_time_zone. ActiveRecord time zone aware attributes updated to use #in_time_zone git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9047 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 + .../lib/active_record/attribute_methods.rb | 4 +- activerecord/test/cases/attribute_methods_test.rb | 2 +- activesupport/CHANGELOG | 2 + .../lib/active_support/core_ext/time/zones.rb | 35 +++++++-------- activesupport/lib/active_support/time_with_zone.rb | 9 +--- activesupport/test/core_ext/time_with_zone_test.rb | 50 +++++++++++----------- 7 files changed, 50 insertions(+), 54 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 645ecdcda6..a1c9a0f4d7 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Time zone aware attributes use Time#in_time_zone [Geoff Buesing] + * Fixed that scoped joins would not always be respected #6821 [Theory/Danger] * Ensure that ActiveRecord::Calculations disambiguates field names with the table name. #11027 [cavalle] diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index f976e70a06..46ecfc1969 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -161,7 +161,7 @@ module ActiveRecord cached = @attributes_cache['#{attr_name}'] return cached if cached && !reload time = read_attribute('#{attr_name}') - @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_current_time_zone : time + @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time end EOV evaluate_attribute_method attr_name, method_body @@ -181,7 +181,7 @@ module ActiveRecord def #{attr_name}=(time) if time time = time.to_time rescue time unless time.acts_like?(:time) - time = time.in_current_time_zone if time.acts_like?(:time) + time = time.in_time_zone if time.acts_like?(:time) end write_attribute(:#{attr_name}, time) end diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index b42d419216..376a70d6fe 100755 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -178,7 +178,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase utc_time = Time.utc(2008, 1, 1) in_time_zone "Pacific Time (US & Canada)" do record = @target.new - record.written_on = utc_time.in_current_time_zone + record.written_on = utc_time.in_time_zone assert_equal utc_time, record.written_on assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time 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,8 +298,25 @@ 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 @@ -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' -- cgit v1.2.3