Skip to content

Commit 3df6768

Browse files
committed
Avoid deprecation warning when raising
When calling + or since between two time objects we should avoid emitting two deprecation messages and avoiding any deprecation messages if the deprecated fallback raises an exception.
1 parent efd0e6c commit 3df6768

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

activesupport/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
* Deprecate addition for `Time` instances with `ActiveSupport::TimeWithZone`.
1+
* Deprecate addition and since between two `Time` and `ActiveSupport::TimeWithZone`.
22

3-
Previously adding time instances together such as `10.days.ago + 10.days.ago` produced a nonsensical future date. This behavior is deprecated and will be removed in Rails 8.0.
3+
Previously adding time instances together such as `10.days.ago + 10.days.ago` or `10.days.ago.since(10.days.ago)` produced a nonsensical future date. This behavior is deprecated and will be removed in Rails 8.1.
44

55
*Nick Schwaderer*
66

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,12 @@ def ago(seconds)
218218
def since(seconds)
219219
self + seconds
220220
rescue TypeError
221+
result = to_datetime.since(seconds)
221222
ActiveSupport.deprecator.warn(
222223
"Passing an instance of #{seconds.class} to #{self.class}#since is deprecated. This behavior will raise " \
223224
"a `TypeError` in Rails 8.1."
224225
)
225-
to_datetime.since(seconds)
226+
result
226227
end
227228
alias :in :since
228229

activesupport/lib/active_support/time_with_zone.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ def +(other)
303303
begin
304304
result = utc + other
305305
rescue TypeError
306+
result = utc.to_datetime.since(other)
306307
ActiveSupport.deprecator.warn(
307308
"Adding an instance of #{other.class} to an instance of #{self.class} is deprecated. This behavior will raise " \
308309
"a `TypeError` in Rails 8.1."
309310
)
310-
result = utc.since(other)
311311
result.in_time_zone(time_zone)
312312
end
313313
result.in_time_zone(time_zone)

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ def test_plus_two_time_instances_raises_deprecation_warning
407407
end
408408
end
409409

410+
def test_plus_with_invalid_argument
411+
twz = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), @time_zone)
412+
assert_not_deprecated(ActiveSupport.deprecator) do
413+
assert_raises TypeError do
414+
twz + Object.new
415+
end
416+
end
417+
end
418+
410419
def test_plus_with_duration
411420
assert_equal Time.utc(2000, 1, 5, 19, 0, 0), (@twz + 5.days).time
412421
end

0 commit comments

Comments
 (0)