-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raise ArgumentError
when an invalid form is passed to Date#to_time
#24552
Conversation
r? @pixeltrix (@rails-bot has picked a reviewer for you, use r? to override) |
@@ -80,6 +80,7 @@ def readable_inspect | |||
# | |||
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC | |||
def to_time(form = :local) | |||
raise ArgumentError, "Expected :local or :uts, got #{form}." unless [:local, :utc].include?(form) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:uts
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
form.inspect
so we can see whether it's a string or symbol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we speed this up (use a case; when) or defer it (rescue NoMethodError and re-raise ArgumentError)?
Before this commit `NoMethodError: undefined method `form_name' for Time:Class` is raised when an invalid argument is passed. It is better to raise `ArgumentError` and show list of valid arguments to developers.
be2dea1
to
29e876f
Compare
@jeremy Thanks for your review. I updated 😍 |
I took benchmark. def to_time_with_case(form = :local)
case
when ![:local, :utc].include?(form)
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}."
else
::Time.send(form, year, month, day)
end
end
def to_time_with_rescue(form = :local)
::Time.send(form, year, month, day)
rescue NoMethodError
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}."
end and create require 'abstract_unit'
require 'active_support/time'
require 'core_ext/date_and_time_behavior'
require 'time_zone_test_helpers'
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('to_time') { Date.new(2005, 2, 21).to_time(:utc) }
x.report('to_time_with_case') { Date.new(2005, 2, 21).to_time_with_case(:utc) }
x.report('to_time_with_rescue') { Date.new(2005, 2, 21).to_time_with_rescue(:utc) }
end
Although |
Raise `ArgumentError` when an invalid form is passed to `Date#to_time`
Before this commit
NoMethodError: undefined method
form_name' for Time:Classis raised when an invalid argument is passed. It is better to raise
ArgumentError` and show list of valid argumentsto developers.