Skip to content

Commit

Permalink
Deprecate passing a Time object to Time#since
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Schwad authored and jhawthorn committed Jul 19, 2024
1 parent 1f92ae6 commit efd0e6c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ def ago(seconds)
# Returns a new Time representing the time a number of seconds since the instance time
def since(seconds)
self + seconds
rescue
rescue TypeError
ActiveSupport.deprecator.warn(
"Passing an instance of #{seconds.class} to #{self.class}#since is deprecated. This behavior will raise " \
"a `TypeError` in Rails 8.1."
)
to_datetime.since(seconds)
end
alias :in :since
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ def test_daylight_savings_time_crossings_backward_end_1day
end
end

def test_since_with_instance_of_time_deprecated
assert_deprecated(ActiveSupport.deprecator) do
Time.now.since(Time.now)
end
end

def test_since
assert_equal Time.local(2005, 2, 22, 10, 10, 11), Time.local(2005, 2, 22, 10, 10, 10).since(1)
assert_equal Time.local(2005, 2, 22, 11, 10, 10), Time.local(2005, 2, 22, 10, 10, 10).since(3600)
Expand Down

0 comments on commit efd0e6c

Please sign in to comment.