Changeset 8718
- Timestamp:
- 01/25/08 15:52:23 (8 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/time/zones.rb (modified) (2 diffs)
- trunk/activesupport/test/core_ext/time_with_zone_test.rb (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r8716 r8718 1 1 *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] 2 4 3 5 * 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 10 10 11 11 module ClassMethods 12 attr_reader :zone 12 def zone 13 Thread.current[:time_zone] 14 end 13 15 14 16 # Sets a global default time zone, separate from the system time zone in ENV['TZ']. … … 21 23 # Time.zone = 'Hawaii' # => 'Hawaii' 22 24 # 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) 25 27 end 26 28 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 29 35 end 30 36 31 def get_zone( zone)32 ::String === zone || ::Numeric === zone ? TimeZone[zone] :zone37 def get_zone(time_zone) 38 ::String === time_zone || ::Numeric === time_zone ? TimeZone[time_zone] : time_zone 33 39 end 34 40 end trunk/activesupport/test/core_ext/time_with_zone_test.rb
r8711 r8718 28 28 29 29 def test_in_current_time_zone 30 with_time_zone 'Alaska' do30 Time.use_zone 'Alaska' do 31 31 assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_current_time_zone 32 32 end … … 40 40 41 41 def test_change_time_zone_to_current 42 with_time_zone 'Alaska' do42 Time.use_zone 'Alaska' do 43 43 assert_equal ActiveSupport::TimeWithZone.new(nil, TimeZone['Alaska'], Time.utc(1999, 12, 31, 19)), @twz.change_time_zone_to_current 44 44 end … … 147 147 assert_equal 31, @twz.day 148 148 end 149 150 protected151 def with_time_zone(zone)152 old_zone, Time.zone = Time.zone, Time.get_zone(zone)153 yield154 ensure155 Time.zone = old_zone156 end157 149 end 158 150 … … 161 153 @t, @dt = Time.utc(2000), DateTime.civil(2000) 162 154 end 155 156 def teardown 157 Time.zone = nil 158 end 163 159 164 160 def test_in_time_zone 165 161 silence_warnings do # silence warnings raised by tzinfo gem 166 with_time_zone 'Eastern Time (US & Canada)' do162 Time.use_zone 'Eastern Time (US & Canada)' do 167 163 assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect 168 164 assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect … … 174 170 175 171 def test_in_current_time_zone 176 with_time_zone 'Alaska' do172 Time.use_zone 'Alaska' do 177 173 assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_current_time_zone.inspect 178 174 assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_current_time_zone.inspect 179 175 end 180 with_time_zone 'Hawaii' do176 Time.use_zone 'Hawaii' do 181 177 assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_current_time_zone.inspect 182 178 assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_current_time_zone.inspect 183 179 end 184 with_time_zone nil do180 Time.use_zone nil do 185 181 assert_equal @t, @t.in_current_time_zone 186 182 assert_equal @dt, @dt.in_current_time_zone … … 190 186 def test_change_time_zone 191 187 silence_warnings do # silence warnings raised by tzinfo gem 192 with_time_zone 'Eastern Time (US & Canada)' do188 Time.use_zone 'Eastern Time (US & Canada)' do 193 189 assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone('Alaska').inspect 194 190 assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone('Alaska').inspect … … 200 196 201 197 def test_change_time_zone_to_current 202 with_time_zone 'Alaska' do198 Time.use_zone 'Alaska' do 203 199 assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone_to_current.inspect 204 200 assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone_to_current.inspect 205 201 end 206 with_time_zone 'Hawaii' do202 Time.use_zone 'Hawaii' do 207 203 assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @t.change_time_zone_to_current.inspect 208 204 assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @dt.change_time_zone_to_current.inspect 209 205 end 210 with_time_zone nil do206 Time.use_zone nil do 211 207 assert_equal @t, @t.change_time_zone_to_current 212 208 assert_equal @dt, @dt.change_time_zone_to_current … … 214 210 end 215 211 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 223 237 end 224 238 end