From ba91fddc442ecfbeffb3c7b05fa5851fabc32059 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Tue, 11 Mar 2008 05:39:13 +0000 Subject: TimeZone #local and #now correctly enforce DST rules git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9007 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/values/time_zone.rb | 27 ++++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'activesupport/lib/active_support/values/time_zone.rb') 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 -- cgit v1.2.3