From 2366fdbdb1e8a5ba4ee07d94e79c2011f5821820 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Mon, 17 Mar 2008 05:07:50 +0000 Subject: Adding TimeZone#parse git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9045 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/time_with_zone.rb | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support/time_with_zone.rb') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index ca82c2e004..984b1e43cb 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -10,12 +10,12 @@ module ActiveSupport @period = @utc ? period : get_period_and_ensure_valid_local_time end - # Returns a Time instance that represents the time in time_zone + # Returns a Time or DateTime instance that represents the time in time_zone def time @time ||= utc_to_local end - # Returns a Time instance that represents the time in UTC + # Returns a Time or DateTime instance that represents the time in UTC def utc @utc ||= local_to_utc end @@ -222,11 +222,20 @@ module ActiveSupport private def get_period_and_ensure_valid_local_time - @time_zone.period_for_local(@time) - rescue ::TZInfo::PeriodNotFound - # time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again - @time += 1.hour - retry + # we don't want a Time.local instance enforcing its own DST rules as well, + # so transfer time values to a utc constructor if necessary + @time = transfer_time_values_to_utc_constructor(@time) unless @time.utc? + begin + @time_zone.period_for_local(@time) + rescue ::TZInfo::PeriodNotFound + # time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again + @time += 1.hour + retry + end + end + + def transfer_time_values_to_utc_constructor(time) + ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0) end # Replicating logic from TZInfo::Timezone#utc_to_local because we want to cache the period in an instance variable for reuse -- cgit v1.2.3