You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I had somewhere in the code DateTime.now. When running the linter (rubocop command) I was getting this error:
Rails/TimeZone: Do not use DateTime.now without zone. Use one of Time.zone.now, DateTime.current, DateTime.now.in_time_zone, DateTime.now.utc, DateTime.now.getlocal, DateTime.now.xmlschema, DateTime.now.iso8601, DateTime.now.jisx0301, DateTime.now.rfc3339, DateTime.now.httpdate, DateTime.now.to_i, DateTime.now.to_f instead.
which is correct.
But, when I run rubocop --safe-auto-correct that string get changed to DateTime.zone.now which is incorrect – DateTime does not implement zone method:
$ DateTime.zone
NoMethodError: undefined method `zone' for DateTime:Class
Expected behavior
After running autofix, I expect DateTime.now changes to Time.zone.now or something similar.
Fixesrubocop#67.
Closesrubocop#71.
This PR fixes an incorrect auto-correct for `Rails/TimeZone`
when using `DateTime`.
```diff
- DateTime.new
+ DateTime.zone.new
```
```ruby
DateTime.zone.new # NoMethodError: undefined method `zone' for
DateTime:Class
```
This PR changes to ignore `DateTime` with the following as
background.
`DateTime` is not mentioned in The Rails Style Guide and
`Rails/TimeZone` cop's examples.
- https://rails.rubystyle.guide/#time-now
- https://docs.rubocop.org/projects/rails/en/stable/cops_rails/#railstimezone
Recently, rubocop#81 and rubocop#82 have been feedback.
I think breaking by auto-correction should be fixed with
the highest priority in this case.
Also, `DateTime` and `Time` are not replaced from `DateTime` to `Time`
by auto-correct because they are different objects.
So I had somewhere in the code
DateTime.now
. When running the linter (rubocop
command) I was getting this error:which is correct.
But, when I run
rubocop --safe-auto-correct
that string get changed toDateTime.zone.now
which is incorrect –DateTime
does not implementzone
method:Expected behavior
After running autofix, I expect
DateTime.now
changes toTime.zone.now
or something similar.RuboCop version
The text was updated successfully, but these errors were encountered: