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

Changeset 8718

Show
Ignore:
Timestamp:
01/25/08 15:52:23 (8 months ago)
Author:
gbuesing
Message:

Time.zone uses thread-local variable for thread safety. Adding Time.use_zone, for
overriding Time.zone locally inside a block. Removing unneeded Time.zone_reset!

Files:

Legend:

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

    r8716 r8718  
    11*SVN* 
     2 
     3* Time.zone uses thread-local variable for thread safety. Adding Time.use_zone, for overriding Time.zone locally inside a block. Removing unneeded Time.zone_reset! [Geoff Buesing] 
    24 
    35* TimeZone#to_s uses UTC rather than GMT; reapplying change that was undone in [8679]. #1689 [Cheah Chu Yeow] 
  • trunk/activesupport/lib/active_support/core_ext/time/zones.rb

    r8708 r8718  
    1010         
    1111        module ClassMethods 
    12           attr_reader :zone 
     12          def zone 
     13            Thread.current[:time_zone] 
     14          end 
    1315 
    1416          # Sets a global default time zone, separate from the system time zone in ENV['TZ'].  
     
    2123          #   Time.zone = 'Hawaii'                  # => 'Hawaii' 
    2224          #   Time.utc(2000).in_current_time_zone   # => Fri, 31 Dec 1999 14:00:00 HST -10:00 
    23           def zone=(zone) 
    24             @zone = get_zone(zone) 
     25          def zone=(time_zone) 
     26            Thread.current[:time_zone] = get_zone(time_zone) 
    2527          end 
    2628           
    27           def zone_reset! 
    28             @zone = nil 
     29          # Allows override of Time.zone locally inside supplied block; resets Time.zone to existing value when done 
     30          def use_zone(time_zone) 
     31            old_zone, ::Time.zone = ::Time.zone, ::Time.get_zone(time_zone) 
     32            yield 
     33          ensure 
     34            ::Time.zone = old_zone 
    2935          end 
    3036           
    31           def get_zone(zone) 
    32             ::String === zone || ::Numeric === zone ? TimeZone[zone] : zone 
     37          def get_zone(time_zone) 
     38            ::String === time_zone || ::Numeric === time_zone ? TimeZone[time_zone] : time_zone 
    3339          end 
    3440        end 
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r8711 r8718  
    2828   
    2929    def test_in_current_time_zone 
    30       with_time_zone 'Alaska' do 
     30      Time.use_zone 'Alaska' do 
    3131        assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_current_time_zone 
    3232      end 
     
    4040   
    4141    def test_change_time_zone_to_current 
    42       with_time_zone 'Alaska' do 
     42      Time.use_zone 'Alaska' do 
    4343        assert_equal ActiveSupport::TimeWithZone.new(nil, TimeZone['Alaska'], Time.utc(1999, 12, 31, 19)), @twz.change_time_zone_to_current 
    4444      end 
     
    147147      assert_equal 31, @twz.day 
    148148    end 
    149    
    150     protected 
    151       def with_time_zone(zone) 
    152         old_zone, Time.zone = Time.zone, Time.get_zone(zone) 
    153         yield 
    154       ensure 
    155         Time.zone = old_zone 
    156       end 
    157149  end 
    158150   
     
    161153      @t, @dt = Time.utc(2000), DateTime.civil(2000) 
    162154    end 
     155     
     156    def teardown 
     157      Time.zone = nil 
     158    end 
    163159 
    164160    def test_in_time_zone 
    165161      silence_warnings do # silence warnings raised by tzinfo gem 
    166         with_time_zone 'Eastern Time (US & Canada)' do 
     162        Time.use_zone 'Eastern Time (US & Canada)' do 
    167163          assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect 
    168164          assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect 
     
    174170 
    175171    def test_in_current_time_zone 
    176       with_time_zone 'Alaska' do 
     172      Time.use_zone 'Alaska' do 
    177173        assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_current_time_zone.inspect 
    178174        assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_current_time_zone.inspect 
    179175      end 
    180       with_time_zone 'Hawaii' do 
     176      Time.use_zone 'Hawaii' do 
    181177        assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_current_time_zone.inspect 
    182178        assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_current_time_zone.inspect 
    183179      end 
    184       with_time_zone nil do 
     180      Time.use_zone nil do 
    185181        assert_equal @t, @t.in_current_time_zone 
    186182        assert_equal @dt, @dt.in_current_time_zone 
     
    190186    def test_change_time_zone 
    191187      silence_warnings do # silence warnings raised by tzinfo gem 
    192         with_time_zone 'Eastern Time (US & Canada)' do 
     188        Time.use_zone 'Eastern Time (US & Canada)' do 
    193189          assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone('Alaska').inspect 
    194190          assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone('Alaska').inspect 
     
    200196     
    201197    def test_change_time_zone_to_current 
    202       with_time_zone 'Alaska' do 
     198      Time.use_zone 'Alaska' do 
    203199        assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone_to_current.inspect 
    204200        assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone_to_current.inspect 
    205201      end 
    206       with_time_zone 'Hawaii' do 
     202      Time.use_zone 'Hawaii' do 
    207203        assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @t.change_time_zone_to_current.inspect 
    208204        assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @dt.change_time_zone_to_current.inspect 
    209205      end 
    210       with_time_zone nil do 
     206      Time.use_zone nil do 
    211207        assert_equal @t, @t.change_time_zone_to_current 
    212208        assert_equal @dt, @dt.change_time_zone_to_current 
     
    214210    end 
    215211     
    216     protected 
    217       def with_time_zone(zone) 
    218         old_zone, Time.zone = Time.zone, Time.get_zone(zone) 
    219         yield 
    220       ensure 
    221         Time.zone = old_zone 
    222       end 
     212    def test_use_zone 
     213      Time.zone = 'Alaska' 
     214      Time.use_zone 'Hawaii' do 
     215        assert_equal TimeZone['Hawaii'], Time.zone 
     216      end 
     217      assert_equal TimeZone['Alaska'], Time.zone 
     218    end 
     219     
     220    def test_use_zone_with_exception_raised 
     221      Time.zone = 'Alaska' 
     222      assert_raises RuntimeError do 
     223        Time.use_zone('Hawaii') { raise RuntimeError } 
     224      end 
     225      assert_equal TimeZone['Alaska'], Time.zone 
     226    end 
     227     
     228    def test_time_zone_setter_is_thread_safe 
     229      Time.use_zone 'Paris' do 
     230        t1 = Thread.new { Time.zone = 'Alaska' } 
     231        t2 = Thread.new { Time.zone = 'Hawaii' } 
     232        assert_equal TimeZone['Paris'], Time.zone 
     233        assert_equal TimeZone['Alaska'], t1[:time_zone] 
     234        assert_equal TimeZone['Hawaii'], t2[:time_zone] 
     235      end 
     236    end 
    223237  end 
    224238end