Skip to content

Commit 98df193

Browse files
authored
Merge pull request rails#50268 from Shopify/opt-time-at
Optimize `Time.at_with_coercion`
2 parents 1c8be9c + 7cfc4ee commit 98df193

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ def current
4242

4343
# Layers additional behavior on Time.at so that ActiveSupport::TimeWithZone and DateTime
4444
# instances can be used when called with a single argument
45-
def at_with_coercion(*args, **kwargs)
46-
return at_without_coercion(*args, **kwargs) if args.size != 1 || !kwargs.empty?
47-
48-
# Time.at can be called with a time or numerical value
49-
time_or_number = args.first
50-
51-
if time_or_number.is_a?(ActiveSupport::TimeWithZone)
52-
at_without_coercion(time_or_number.to_r).getlocal
53-
elsif time_or_number.is_a?(DateTime)
54-
at_without_coercion(time_or_number.to_f).getlocal
45+
def at_with_coercion(time_or_number, *args)
46+
if args.empty?
47+
if time_or_number.is_a?(ActiveSupport::TimeWithZone)
48+
at_without_coercion(time_or_number.to_r).getlocal
49+
elsif time_or_number.is_a?(DateTime)
50+
at_without_coercion(time_or_number.to_f).getlocal
51+
else
52+
at_without_coercion(time_or_number)
53+
end
5554
else
56-
at_without_coercion(time_or_number)
55+
at_without_coercion(time_or_number, *args)
5756
end
5857
end
58+
ruby2_keywords :at_with_coercion
5959
alias_method :at_without_coercion, :at
6060
alias_method :at, :at_with_coercion
6161

0 commit comments

Comments
 (0)