Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9047

Show
Ignore:
Timestamp:
03/17/08 05:50:13 (4 months ago)
Author:
gbuesing
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9022 r9047  
    11*SVN* 
     2 
     3* Time zone aware attributes use Time#in_time_zone [Geoff Buesing] 
    24 
    35* Fixed that scoped joins would not always be respected #6821 [Theory/Danger] 
  • trunk/activerecord/lib/active_record/attribute_methods.rb

    r8806 r9047  
    162162              return cached if cached && !reload 
    163163              time = read_attribute('#{attr_name}') 
    164               @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_current_time_zone : time 
     164              @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time 
    165165            end 
    166166          EOV 
     
    182182              if time 
    183183                time = time.to_time rescue time unless time.acts_like?(:time) 
    184                 time = time.in_current_time_zone if time.acts_like?(:time) 
     184                time = time.in_time_zone if time.acts_like?(:time) 
    185185              end 
    186186              write_attribute(:#{attr_name}, time) 
  • trunk/activerecord/test/cases/attribute_methods_test.rb

    r8808 r9047  
    179179      in_time_zone "Pacific Time (US & Canada)" do 
    180180        record   = @target.new 
    181         record.written_on = utc_time.in_current_time_zone 
     181        record.written_on = utc_time.in_time_zone 
    182182        assert_equal utc_time, record.written_on 
    183183        assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone 
  • trunk/activesupport/CHANGELOG

    r9046 r9047  
    11*SVN* 
     2 
     3* Time, DateTime and TimeWithZone #in_time_zone defaults to Time.zone. Removing now unneeded #in_current_time_zone [Geoff Buesing] 
    24 
    35* TZInfo caches Timezone instances in its own internal hash cache, so TimeZone::MAPPING doesn't need to cache them as well [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/time/zones.rb

    r9008 r9047  
    2020          # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object 
    2121          # 
    22           # Any Time or DateTime object can use this default time zone, via #in_current_time_zone. 
     22          # Any Time or DateTime object can use this default time zone, via #in_time_zone. 
    2323          # Example: 
    2424          # 
    25           #   Time.zone = 'Hawaii'                  # => 'Hawaii' 
    26           #   Time.utc(2000).in_current_time_zone   # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
     25          #   Time.zone = 'Hawaii'          # => 'Hawaii' 
     26          #   Time.utc(2000).in_time_zone   # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
    2727          def zone=(time_zone) 
    2828            Thread.current[:time_zone] = get_zone(time_zone) 
     
    4444        end 
    4545         
    46         # Returns the simultaneous time in the supplied zone. Examples
     46        # Returns the simultaneous time in Time.zone. Example
    4747        # 
    48         #   t = Time.utc(2000)        # => Sat Jan 01 00:00:00 UTC 2000 
    49         #   t.in_time_zone('Alaska')  # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 
    50         #   t.in_time_zone('Hawaii')  # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
    51         def in_time_zone(zone) 
    52           ActiveSupport::TimeWithZone.new(utc? ? self : getutc, get_zone(zone)) 
     48        #   Time.zone = 'Hawaii'                    # => 'Hawaii' 
     49        #   Time.utc(2000).in_time_zone             # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
     50        # 
     51        # This method is similar to Time#localtime, except that it uses Time.zone as the local zone 
     52        # instead of the operating system's time zone. 
     53        # 
     54        # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,  
     55        # and the conversion will be based on that zone instead of Time.zone. Example: 
     56        # 
     57        #   Time.utc(2000).in_time_zone('Alaska')   # => Fri, 31 Dec 1999 15:00:00 AKST -09:00 
     58        def in_time_zone(zone = ::Time.zone) 
     59          ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.send!(:get_zone, zone)) 
    5360        end 
    54  
    55         # Returns the simultaneous time in Time.zone 
    56         def in_current_time_zone 
    57           ::Time.zone ? in_time_zone(::Time.zone) : self 
    58         end 
    59          
    60         private 
    61           def get_zone(time_zone) 
    62             ::Time.send!(:get_zone, time_zone) 
    63           end 
    6461      end 
    6562    end 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r9045 r9047  
    3030    end 
    3131 
    32     # Returns the simultaneous time in the specified zone 
    33     def in_time_zone(new_zone
     32    # Returns the simultaneous time in Time.zone, or the specified zone 
     33    def in_time_zone(new_zone = ::Time.zone
    3434      return self if time_zone == new_zone 
    3535      utc.in_time_zone(new_zone) 
    36     end 
    37    
    38     # Returns the simultaneous time in Time.zone 
    39     def in_current_time_zone 
    40       utc.in_current_time_zone 
    4136    end 
    4237   
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r9041 r9047  
    2222      assert_equal @time_zone, @twz.time_zone 
    2323    end 
    24    
     24     
    2525    def test_in_time_zone 
     26      Time.use_zone 'Alaska' do 
     27        assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone 
     28      end 
     29    end 
     30   
     31    def test_in_time_zone_with_argument 
    2632      assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone('Alaska') 
    2733    end 
     
    2935    def test_in_time_zone_with_new_zone_equal_to_old_zone_does_not_create_new_object 
    3036      assert_equal @twz.object_id, @twz.in_time_zone(TimeZone['Eastern Time (US & Canada)']).object_id 
    31     end 
    32    
    33     def test_in_current_time_zone 
    34       Time.use_zone 'Alaska' do 
    35         assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_current_time_zone 
    36       end 
    3737    end 
    3838   
     
    299299      Time.zone = nil 
    300300    end 
    301  
     301     
    302302    def test_in_time_zone 
     303      silence_warnings do # silence warnings raised by tzinfo gem 
     304        Time.use_zone 'Alaska' do 
     305          assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect 
     306          assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect 
     307        end 
     308        Time.use_zone 'Hawaii' do 
     309          assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect 
     310          assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect 
     311        end 
     312        Time.use_zone nil do 
     313          assert_equal @t, @t.in_time_zone 
     314          assert_equal @dt, @dt.in_time_zone 
     315        end 
     316      end 
     317    end 
     318 
     319    def test_in_time_zone_with_argument 
    303320      silence_warnings do # silence warnings raised by tzinfo gem 
    304321        Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) 
     
    322339      end 
    323340    end 
    324  
    325     def test_in_current_time_zone 
    326       silence_warnings do # silence warnings raised by tzinfo gem 
    327         Time.use_zone 'Alaska' do 
    328           assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_current_time_zone.inspect 
    329           assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_current_time_zone.inspect 
    330         end 
    331         Time.use_zone 'Hawaii' do 
    332           assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_current_time_zone.inspect 
    333           assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_current_time_zone.inspect 
    334         end 
    335         Time.use_zone nil do 
    336           assert_equal @t, @t.in_current_time_zone 
    337           assert_equal @dt, @dt.in_current_time_zone 
    338         end 
    339       end 
    340     end 
    341341     
    342342    def test_use_zone