From 92fc8ec663f7dbb554c42dd41a86a7efe63dc725 Mon Sep 17 00:00:00 2001 From: Kevin McPhillips Date: Mon, 6 Mar 2017 16:14:05 -0500 Subject: Handle #to_time and memoization taking into account memoization, frozen state, and preserve_timezone flag. --- .../active_support/core_ext/date_and_time/compatibility.rb | 6 +----- .../lib/active_support/core_ext/time/compatibility.rb | 9 ++++++++- activesupport/lib/active_support/time_with_zone.rb | 11 ++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/date_and_time/compatibility.rb b/activesupport/lib/active_support/core_ext/date_and_time/compatibility.rb index db95ae0db5..3f4e236ab7 100644 --- a/activesupport/lib/active_support/core_ext/date_and_time/compatibility.rb +++ b/activesupport/lib/active_support/core_ext/date_and_time/compatibility.rb @@ -12,11 +12,7 @@ module DateAndTime mattr_accessor(:preserve_timezone, instance_writer: false) { false } def to_time - if preserve_timezone - @_to_time_with_instance_offset ||= getlocal(utc_offset) - else - @_to_time_with_system_offset ||= getlocal - end + preserve_timezone ? getlocal(utc_offset) : getlocal end end end diff --git a/activesupport/lib/active_support/core_ext/time/compatibility.rb b/activesupport/lib/active_support/core_ext/time/compatibility.rb index ca4b9574d5..32e5608b32 100644 --- a/activesupport/lib/active_support/core_ext/time/compatibility.rb +++ b/activesupport/lib/active_support/core_ext/time/compatibility.rb @@ -1,5 +1,12 @@ require "active_support/core_ext/date_and_time/compatibility" +require "active_support/core_ext/module/remove_method" class Time - prepend DateAndTime::Compatibility + include DateAndTime::Compatibility + + remove_possible_method :to_time + + def to_time + preserve_timezone ? self : getlocal + end end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 857cc1a664..9ad8453ccf 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -410,6 +410,15 @@ module ActiveSupport @to_datetime ||= utc.to_datetime.new_offset(Rational(utc_offset, 86_400)) end + # Returns an instance of Time + def to_time + if preserve_timezone + @to_time_with_instance_offset ||= getlocal(utc_offset) + else + @to_time_with_system_offset ||= getlocal + end + end + # So that +self+ acts_like?(:time). def acts_like_time? true @@ -428,7 +437,7 @@ module ActiveSupport def freeze # preload instance variables before freezing - period; utc; time; to_datetime + period; utc; time; to_datetime; to_time super end -- cgit v1.2.3