Skip to content

Commit e98f957

Browse files
committed
Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float [#3476 status:resolved]
1 parent 016f15d commit e98f957

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

activesupport/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*Rails 3.0 (pending)*
22

3+
* Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float #3476 [Geoff Buesing]
4+
35
* YAML serialization for OrderedHash. #3608 [Gregor Schmidt]
46

57
* Update bundled TZInfo to v0.3.16 [Geoff Buesing]

activesupport/lib/active_support/core_ext/time/calculations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def minus_with_duration(other) #:nodoc:
259259
# are coerced into values that Time#- will recognize
260260
def minus_with_coercion(other)
261261
other = other.comparable_time if other.respond_to?(:comparable_time)
262-
minus_without_coercion(other)
262+
other.is_a?(DateTime) ? to_f - other.to_f : minus_without_coercion(other)
263263
end
264264
alias_method :minus_without_coercion, :-
265265
alias_method :-, :minus_with_coercion

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,10 @@ def test_compare_with_time_with_zone
722722
def test_minus_with_time_with_zone
723723
assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
724724
end
725+
726+
def test_minus_with_datetime
727+
assert_equal 86_400.0, Time.utc(2000, 1, 2) - DateTime.civil(2000, 1, 1)
728+
end
725729

726730
def test_time_created_with_local_constructor_cannot_represent_times_during_hour_skipped_by_dst
727731
with_env_tz 'US/Eastern' do

0 commit comments

Comments
 (0)