From dad3e835f77d402aad3b0802ee14bf7a20fd9365 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Fri, 25 Jan 2008 23:55:07 +0000 Subject: 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 --- .../lib/active_support/core_ext/time/calculations.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activesupport/lib/active_support') 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) -- cgit v1.2.3