Skip to content

Commit 2627c95

Browse files
jhawthornrafaelfranca
authored andcommitted
Avoid using to_time in TimeWithZone#-
We're more likely to already have utc and can avoid constructing the extra time object, and this will avoid deprecations on unconfigured to_time_preserves_timezone.
1 parent d47c371 commit 2627c95

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

activesupport/lib/active_support/time_with_zone.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def +(other)
332332
#
333333
def -(other)
334334
if other.acts_like?(:time)
335-
to_time - other.to_time
335+
getutc - other.getutc
336336
elsif duration_of_variable_length?(other)
337337
method_missing(:-, other)
338338
else

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ def test_minus_with_time_with_zone
433433
assert_equal 86_400.0, twz2 - twz1
434434
end
435435

436+
def test_minus_with_time_with_zone_without_preserve_configured
437+
with_preserve_timezone(nil) do
438+
twz1 = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone["UTC"])
439+
twz2 = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 2), ActiveSupport::TimeZone["UTC"])
440+
441+
difference = assert_not_deprecated(ActiveSupport.deprecator) { twz2 - twz1 }
442+
assert_equal 86_400.0, difference
443+
end
444+
end
445+
436446
def test_minus_with_time_with_zone_precision
437447
twz1 = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0, Rational(1, 1000)), ActiveSupport::TimeZone["UTC"])
438448
twz2 = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 23, 59, 59, Rational(999999999, 1000)), ActiveSupport::TimeZone["UTC"])

0 commit comments

Comments
 (0)