From c911e1ac08d119e94fcec6973a012f4cd198ecee Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Fri, 25 Jan 2008 15:52:23 +0000 Subject: 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! git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8718 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/time/zones.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index 390d4128a1..a600e4099d 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -9,7 +9,9 @@ module ActiveSupport #:nodoc: end module ClassMethods - attr_reader :zone + def zone + Thread.current[:time_zone] + end # Sets a global default time zone, separate from the system time zone in ENV['TZ']. # Accepts either a Rails TimeZone object, a string that identifies a @@ -20,16 +22,20 @@ module ActiveSupport #:nodoc: # # Time.zone = 'Hawaii' # => 'Hawaii' # Time.utc(2000).in_current_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00 - def zone=(zone) - @zone = get_zone(zone) + def zone=(time_zone) + Thread.current[:time_zone] = get_zone(time_zone) end - def zone_reset! - @zone = nil + # Allows override of Time.zone locally inside supplied block; resets Time.zone to existing value when done + def use_zone(time_zone) + old_zone, ::Time.zone = ::Time.zone, ::Time.get_zone(time_zone) + yield + ensure + ::Time.zone = old_zone end - def get_zone(zone) - ::String === zone || ::Numeric === zone ? TimeZone[zone] : zone + def get_zone(time_zone) + ::String === time_zone || ::Numeric === time_zone ? TimeZone[time_zone] : time_zone end end -- cgit v1.2.3