aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/values
diff options
context:
space:
mode:
authorGeoff Buesing <gbuesing@gmail.com>2008-03-11 05:39:13 +0000
committerGeoff Buesing <gbuesing@gmail.com>2008-03-11 05:39:13 +0000
commitba91fddc442ecfbeffb3c7b05fa5851fabc32059 (patch)
tree3dddf86bf8d3900a0006f12752dcbcc99ee2f33a /activesupport/lib/active_support/values
parent52b57c662c129d72d44fa1ab9920a752c1973482 (diff)
downloadrails-ba91fddc442ecfbeffb3c7b05fa5851fabc32059.tar.gz
rails-ba91fddc442ecfbeffb3c7b05fa5851fabc32059.tar.bz2
rails-ba91fddc442ecfbeffb3c7b05fa5851fabc32059.zip
TimeZone #local and #now correctly enforce DST rules
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9007 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/values')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb27
1 files changed, 17 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index ced823f052..a52eee3e7c 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -174,25 +174,32 @@ 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.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
- def local(*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)
+ # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+. Example:
+ #
+ # Time.zone = "Hawaii" # => "Hawaii"
+ # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
+ def local(*args)
+ t = Time.utc_time(*args)
+ begin
+ result = local_to_utc(t)
+ rescue TZInfo::PeriodNotFound
+ t += 1.hour
+ retry
+ end
+ result.in_time_zone(self)
+ end
+
# Returns an ActiveSupport::TimeWithZone instance representing the current time
# in the time zone represented by +self+. Example:
#
# Time.zone = 'Hawaii' # => "Hawaii"
# Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
def now
- tzinfo.now.change_time_zone(self)
+ Time.now.utc.in_time_zone(self)
end
# Return the current date in this time zone.
@@ -233,7 +240,7 @@ class TimeZone
rescue LoadError # Tzinfo gem is not available
# re-raise LoadError only when a tzinfo-dependent method is called:
- %w(now today utc_to_local local_to_utc period_for_local tzinfo).each do |method|
+ %w(local now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method|
define_method(method) {|*args| raise LoadError, "TZInfo gem is required for TimeZone##{method}. `gem install tzinfo` and try again."}
end
end