Skip to content

Commit efd0e6c

Browse files
Schwadjhawthorn
authored andcommitted
Deprecate passing a Time object to Time#since
Ref: rails#52343 `Time.now.since(Time.now)` does not fail, but it returns a random future date (currently in 2079). This commit deprecates passing a Time object to Time#since until Rails 8.0, where it will raise a TypeError.
1 parent 1f92ae6 commit efd0e6c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ def ago(seconds)
217217
# Returns a new Time representing the time a number of seconds since the instance time
218218
def since(seconds)
219219
self + seconds
220-
rescue
220+
rescue TypeError
221+
ActiveSupport.deprecator.warn(
222+
"Passing an instance of #{seconds.class} to #{self.class}#since is deprecated. This behavior will raise " \
223+
"a `TypeError` in Rails 8.1."
224+
)
221225
to_datetime.since(seconds)
222226
end
223227
alias :in :since

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ def test_daylight_savings_time_crossings_backward_end_1day
282282
end
283283
end
284284

285+
def test_since_with_instance_of_time_deprecated
286+
assert_deprecated(ActiveSupport.deprecator) do
287+
Time.now.since(Time.now)
288+
end
289+
end
290+
285291
def test_since
286292
assert_equal Time.local(2005, 2, 22, 10, 10, 11), Time.local(2005, 2, 22, 10, 10, 10).since(1)
287293
assert_equal Time.local(2005, 2, 22, 11, 10, 10), Time.local(2005, 2, 22, 10, 10, 10).since(3600)

0 commit comments

Comments
 (0)