diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 23:55:07 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-01-25 23:55:07 +0000 |
commit | dad3e835f77d402aad3b0802ee14bf7a20fd9365 (patch) | |
tree | 8242ac550be31bf80cb5ff616cafc563e942d8b2 /activesupport/lib | |
parent | 4b2ac708b7666510d522fc706936d3c2ea5cbb80 (diff) | |
download | rails-dad3e835f77d402aad3b0802ee14bf7a20fd9365.tar.gz rails-dad3e835f77d402aad3b0802ee14bf7a20fd9365.tar.bz2 rails-dad3e835f77d402aad3b0802ee14bf7a20fd9365.zip |
Time#- coerces TimeWithZone argument to a Time instance so that difference in seconds can be calculated. Closes #10914 [Geoff Buesing, yyyc514]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8730 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index eb342084a8..7fbfca5b2a 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -13,6 +13,9 @@ module ActiveSupport #:nodoc: alias_method :minus_without_duration, :- alias_method :-, :minus_with_duration + alias_method :minus_without_coercion, :- + alias_method :-, :minus_with_coercion + alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion end @@ -218,6 +221,14 @@ module ActiveSupport #:nodoc: end end + # Time#- can also be used to determine the number of seconds between two Time instances. + # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances + # are coerced into values that Time#- will recognize + def minus_with_coercion(other) + other = other.comparable_time if other.respond_to?(:comparable_time) + minus_without_coercion(other) + end + # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances # can be chronologically compared with a Time def compare_with_coercion(other) |