Skip to content

Rails/TimeZone: DateTime.new is wrongly corrected to DateTime.zone.new #67

Closed
@doloopwhile

Description

When EnforcedStyle is flexible(default), Rails/TimeZone cop will correct DateTime.new to DateTime.zone.new. It breaks codes because DateTime.zone is not defined by Rails.

See: https://api.rubyonrails.org/classes/DateTime.html

Expected behavior

  • DateTime.new is left as it is.
  • DateTime.new is corrected to Time.zone.new as EnforcedTyope: strict

Actual behavior

  • DateTime.new is corrected to DateTime.zone.new

Other DateTime's methods (now, local, parse, at, current) are also corrected to DateTime.zone.*.

Steps to reproduce the problem

$ gem install rubocop -v 0.71.0
$ gem install rubocop-rails -v 2.0
$ echo 'require: rubocop-rails' > .rubocop.yml

$ echo 'DateTime.new' > a.rb

$ rubocop --only Rails/TimeZone --auto-correct a.rb
Inspecting 1 file
C

Offenses:

a.rb:1:10: C: [Corrected] Rails/TimeZone: Do not use DateTime.new without zone. Use one of Time.zone.now, DateTime.current, DateTime.new.in_time_zone, DateTime.new.utc, DateTime.new.getlocal, DateTime.new.xmlschema, DateTime.new.iso8601, DateTime.new.jisx0301, DateTime.new.rfc3339, DateTime.new.httpdate, DateTime.new.to_i, DateTime.new.to_f instead. (https://github.com/rubocop-hq/rails-style-guide#time, http://danilenko.org/2012/7/6/rails_timezones)
DateTime.new
         ^^^

1 file inspected, 1 offense detected, 1 offense corrected

$ cat a.rb
DateTime.zone.new

RuboCop version

$ rubocop -V
0.71.0 (using Parser 2.6.3.0, running on ruby 2.6.2 x86_64-darwin17)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions