diff options
author | Andrew White <pixeltrix@users.noreply.github.com> | 2017-03-16 08:53:12 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-16 08:53:12 +0000 |
commit | ee33b9e93a5a06aa6c312466576b91ed210cf7c0 (patch) | |
tree | 85548d355856304027b972641775548e15e8abce /activesupport/lib | |
parent | f38de5a7b4ea56c8f9f9138248128339a53a1b1f (diff) | |
parent | 92fc8ec663f7dbb554c42dd41a86a7efe63dc725 (diff) | |
download | rails-ee33b9e93a5a06aa6c312466576b91ed210cf7c0.tar.gz rails-ee33b9e93a5a06aa6c312466576b91ed210cf7c0.tar.bz2 rails-ee33b9e93a5a06aa6c312466576b91ed210cf7c0.zip |
Merge pull request #28147 from kmcphillips/master-time-freeze
Allow Time#to_time on frozen objects. Return frozen time rather than "RuntimeError: can't modify frozen Time"
Diffstat (limited to 'activesupport/lib')
3 files changed, 19 insertions, 7 deletions
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 e31983cf26..7603d7069d 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -411,6 +411,15 @@ module ActiveSupport @to_datetime ||= utc.to_datetime.new_offset(Rational(utc_offset, 86_400)) end + # Returns an instance of <tt>Time</tt> + 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+ <tt>acts_like?(:time)</tt>. def acts_like_time? true @@ -429,7 +438,7 @@ module ActiveSupport def freeze # preload instance variables before freezing - period; utc; time; to_datetime + period; utc; time; to_datetime; to_time super end |