Add Time Zone support to ActiveRecord, and config.time_zone property for specifying a default Time Zone. Closes #10982 [Geoff Buesing, rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8806 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson
2008-02-06 06:43:02 +00:00
parent 692dbbf793
commit 72385a7be6
12 changed files with 209 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Add Time.zone_default accessor for setting the default time zone. Rails::Configuration.time_zone sets this. #10982 [Geoff Buesing]
* cache.fetch(key, :force => true) to force a cache miss. [Jeremy Kemper]
* Support retrieving TimeZones with a Duration. TimeZone[-28800] == TimeZone[-480.minutes]. [rick]

View File

@@ -9,8 +9,10 @@ module ActiveSupport #:nodoc:
end
module ClassMethods
attr_accessor :zone_default
def zone
Thread.current[:time_zone]
Thread.current[:time_zone] || zone_default
end
# Sets a global default time zone, separate from the system time zone in ENV['TZ'].

View File

@@ -174,6 +174,14 @@ class TimeZone
def to_s
"(UTC#{formatted_offset}) #{name}"
end
# Method for creating new ActiveSupport::TimeWithZone instance in time zone of self. Example:
#
# Time.zone = "Hawaii" # => "Hawaii"
# Time.zone.new(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
def new(*args)
Time.utc_time(*args).change_time_zone(self)
end
begin # the following methods depend on the tzinfo gem
require_library_or_gem "tzinfo" unless Object.const_defined?(:TZInfo)

View File

@@ -459,6 +459,15 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] )
end
def test_time_created_with_local_constructor_cannot_represent_times_during_hour_skipped_by_dst
with_env_tz 'US/Eastern' do
# On Apr 2 2006 at 2:00AM in US, clocks were moved forward to 3:00AM.
# Therefore, 2AM EST doesn't exist for this date; Time.local fails over to 3:00AM EDT
assert_equal Time.local(2006, 4, 2, 3), Time.local(2006, 4, 2, 2)
assert Time.local(2006, 4, 2, 2).dst?
end
end
protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz

View File

@@ -128,5 +128,11 @@ class TimeZoneTest < Test::Unit::TestCase
assert TimeZone.us_zones.include?(TimeZone["Hawaii"])
assert !TimeZone.us_zones.include?(TimeZone["Kuala Lumpur"])
end
def test_new
time = TimeZone["Hawaii"].new(2007, 2, 5, 15, 30, 45)
assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time
assert_equal TimeZone["Hawaii"], time.time_zone
end
end