diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 15:52:23 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 15:52:23 +0000 |
commit | c911e1ac08d119e94fcec6973a012f4cd198ecee (patch) | |
tree | 5f46b543d17fe4d502e74e76d7c8773eb491534c /activesupport/lib | |
parent | 9f1fdcc27d689fc5feb07a6f197856a710383319 (diff) | |
download | rails-c911e1ac08d119e94fcec6973a012f4cd198ecee.tar.gz rails-c911e1ac08d119e94fcec6973a012f4cd198ecee.tar.bz2 rails-c911e1ac08d119e94fcec6973a012f4cd198ecee.zip |
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
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/zones.rb | 20 |
1 files changed, 13 insertions, 7 deletions
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 |