aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/time_with_zone.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/time_with_zone.rb')
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb23
1 files changed, 16 insertions, 7 deletions
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