-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Dates to Unix Timestamp #2860
Comments
What is your configuration for this cop? From the documentation for this class # This cop checks for the correct use of Date methods,
# such as Date.today, Date.current etc.
#
# Using Date.today is dangerous, because it doesn't know anything about
# Rails time zone. You must use Time.zone.today instead.
#
# The cop also reports warnings when you are using 'to_time' method,
# because it doesn't know about Rails time zone either.
#
# Two styles are supported for this cop. When EnforcedStyle is 'strict'
# then the Date methods (today, current, yesterday, tomorrow)
# are prohibited and the usage of both 'to_time'
# and 'to_time_in_current_zone' is reported as warning.
#
# When EnforcedStyle is 'flexible' then only 'Date.today' is prohibited
# and only 'to_time' is reported as warning.
#
# @example
# # no offense
# Time.zone.today
# Time.zone.today - 1.day
#
# # acceptable
# Date.current
# Date.yesterday
#
# # always reports offense
# Date.today
# date.to_time
#
# # reports offense only when style is 'strict'
# date.to_time_in_current_zone It looks like the cop is wanting you to use |
So the code it doesn't like is in a conditional: val = send(sattr)
if val.is_a?(Date)
val = val.to_time.to_i # This is the line it hates.
elsif val.is_a?(Time) || val.is_a?(DateTime)
val = val.to_i
end |
I need someone else to weigh in on this because I am unsure what the accepted way of converting @palkan appears to have authored this cop, maybe he can shed some light on the issue. |
The issue exists. PR attached. |
Thanks for the PR |
I can't find a way to satisfy Rubocop and convert dates to unix timestamps. For time, you can do something like
Time.current.to_i
. For dates, I haveDate.today.to_time.to_i
. Rubocop complains about theto_time
aspect asDate
does not know anything about TimeZones.Is there a way around this without disabling date checks?
The text was updated successfully, but these errors were encountered: