Changeset 9047
- Timestamp:
- 03/17/08 05:50:13 (4 months ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/attribute_methods.rb (modified) (2 diffs)
- trunk/activerecord/test/cases/attribute_methods_test.rb (modified) (1 diff)
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/time/zones.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/time_with_zone.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/time_with_zone_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r9022 r9047 1 1 *SVN* 2 3 * Time zone aware attributes use Time#in_time_zone [Geoff Buesing] 2 4 3 5 * Fixed that scoped joins would not always be respected #6821 [Theory/Danger] trunk/activerecord/lib/active_record/attribute_methods.rb
r8806 r9047 162 162 return cached if cached && !reload 163 163 time = read_attribute('#{attr_name}') 164 @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_ current_time_zone : time164 @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time 165 165 end 166 166 EOV … … 182 182 if time 183 183 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) 185 185 end 186 186 write_attribute(:#{attr_name}, time) trunk/activerecord/test/cases/attribute_methods_test.rb
r8808 r9047 179 179 in_time_zone "Pacific Time (US & Canada)" do 180 180 record = @target.new 181 record.written_on = utc_time.in_ current_time_zone181 record.written_on = utc_time.in_time_zone 182 182 assert_equal utc_time, record.written_on 183 183 assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone trunk/activesupport/CHANGELOG
r9046 r9047 1 1 *SVN* 2 3 * Time, DateTime and TimeWithZone #in_time_zone defaults to Time.zone. Removing now unneeded #in_current_time_zone [Geoff Buesing] 2 4 3 5 * 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 20 20 # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object 21 21 # 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. 23 23 # Example: 24 24 # 25 # Time.zone = 'Hawaii' # => 'Hawaii'26 # Time.utc(2000).in_ current_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:0025 # Time.zone = 'Hawaii' # => 'Hawaii' 26 # Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00 27 27 def zone=(time_zone) 28 28 Thread.current[:time_zone] = get_zone(time_zone) … … 44 44 end 45 45 46 # Returns the simultaneous time in the supplied zone. Examples:46 # Returns the simultaneous time in Time.zone. Example: 47 47 # 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)) 53 60 end 54 55 # Returns the simultaneous time in Time.zone56 def in_current_time_zone57 ::Time.zone ? in_time_zone(::Time.zone) : self58 end59 60 private61 def get_zone(time_zone)62 ::Time.send!(:get_zone, time_zone)63 end64 61 end 65 62 end trunk/activesupport/lib/active_support/time_with_zone.rb
r9045 r9047 30 30 end 31 31 32 # Returns the simultaneous time in the specified zone33 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) 34 34 return self if time_zone == new_zone 35 35 utc.in_time_zone(new_zone) 36 end37 38 # Returns the simultaneous time in Time.zone39 def in_current_time_zone40 utc.in_current_time_zone41 36 end 42 37 trunk/activesupport/test/core_ext/time_with_zone_test.rb
r9041 r9047 22 22 assert_equal @time_zone, @twz.time_zone 23 23 end 24 24 25 25 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 26 32 assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone('Alaska') 27 33 end … … 29 35 def test_in_time_zone_with_new_zone_equal_to_old_zone_does_not_create_new_object 30 36 assert_equal @twz.object_id, @twz.in_time_zone(TimeZone['Eastern Time (US & Canada)']).object_id 31 end32 33 def test_in_current_time_zone34 Time.use_zone 'Alaska' do35 assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_current_time_zone36 end37 37 end 38 38 … … 299 299 Time.zone = nil 300 300 end 301 301 302 302 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 303 320 silence_warnings do # silence warnings raised by tzinfo gem 304 321 Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) … … 322 339 end 323 340 end 324 325 def test_in_current_time_zone326 silence_warnings do # silence warnings raised by tzinfo gem327 Time.use_zone 'Alaska' do328 assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_current_time_zone.inspect329 assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_current_time_zone.inspect330 end331 Time.use_zone 'Hawaii' do332 assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_current_time_zone.inspect333 assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_current_time_zone.inspect334 end335 Time.use_zone nil do336 assert_equal @t, @t.in_current_time_zone337 assert_equal @dt, @dt.in_current_time_zone338 end339 end340 end341 341 342 342 def test_use_zone