Skip to content

Commit

Permalink
[Fix rubocop#440] Make Rails/TimeZone aware of timezone specifier
Browse files Browse the repository at this point in the history
Fixes rubocop#440.

This PR makes `Rails/TimeZone` aware of timezone specifier.
  • Loading branch information
koic committed Feb 20, 2021
1 parent 06a57ec commit a7dfbdf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [#409](https://github.com/rubocop-hq/rubocop-rails/pull/409): Deconstruct "table.column" in `Rails/WhereNot`. ([@mobilutz][])
* [#416](https://github.com/rubocop-hq/rubocop-rails/pull/416): Make `Rails/HasManyOrHasOneDependent` accept combination of association extension and `with_options`. ([@ohbarye][])
* [#432](https://github.com/rubocop-hq/rubocop-rails/issues/432): Exclude gemspec file by default for `Rails/TimeZone` cop. ([@koic][])
* [#440](https://github.com/rubocop-hq/rubocop-rails/issues/440): This PR makes `Rails/TimeZone` aware of timezone specifier. ([@koic][])

## 2.9.1 (2020-12-16)

Expand Down
9 changes: 5 additions & 4 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4157,15 +4157,16 @@ to use Time.in_time_zone.
# bad
Time.now
Time.parse('2015-03-02 19:05:37')
Time.parse('2015-03-02T19:05:37')
# bad
Time.current
Time.at(timestamp).in_time_zone
# good
Time.zone.now
Time.zone.parse('2015-03-02 19:05:37')
Time.zone.parse('2015-03-02T19:05:37')
Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
----

==== EnforcedStyle: flexible (default)
Expand All @@ -4176,11 +4177,11 @@ Time.zone.parse('2015-03-02 19:05:37')
# bad
Time.now
Time.parse('2015-03-02 19:05:37')
Time.parse('2015-03-02T19:05:37')
# good
Time.zone.now
Time.zone.parse('2015-03-02 19:05:37')
Time.zone.parse('2015-03-02T19:05:37')
# good
Time.current
Expand Down
18 changes: 13 additions & 5 deletions lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@ module Rails
#
# # bad
# Time.now
# Time.parse('2015-03-02 19:05:37')
# Time.parse('2015-03-02T19:05:37')
#
# # bad
# Time.current
# Time.at(timestamp).in_time_zone
#
# # good
# Time.zone.now
# Time.zone.parse('2015-03-02 19:05:37')
# Time.zone.parse('2015-03-02T19:05:37')
# Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
#
# @example EnforcedStyle: flexible (default)
# # `flexible` allows usage of `in_time_zone` instead of `zone`.
#
# # bad
# Time.now
# Time.parse('2015-03-02 19:05:37')
# Time.parse('2015-03-02T19:05:37')
#
# # good
# Time.zone.now
# Time.zone.parse('2015-03-02 19:05:37')
# Time.zone.parse('2015-03-02T19:05:37')
#
# # good
# Time.current
Expand All @@ -63,6 +64,8 @@ class TimeZone < Base
ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601
jisx0301 rfc3339 httpdate to_i to_f].freeze

TIMEZONE_SPECIFIER = /[A-z]/.freeze

def on_const(node)
mod, klass = *node
# we should only check core classes
Expand Down Expand Up @@ -116,9 +119,10 @@ def remove_redundant_in_time_zone(corrector, node)
end

def check_time_node(klass, node)
return if attach_timezone_specifier?(node.first_argument)

chain = extract_method_chain(node)
return if not_danger_chain?(chain)

return check_localtime(node) if need_check_localtime?(chain)

method_name = (chain & DANGEROUS_METHODS).join('.')
Expand All @@ -132,6 +136,10 @@ def check_time_node(klass, node)
end
end

def attach_timezone_specifier?(date)
date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s[-1])
end

def build_message(klass, method_name, node)
if flexible?
format(
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rails/time_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@
RUBY
end

it 'does not register an offense when attaching timezone specifier `Z`' do
expect_no_offenses(<<~RUBY)
Time.parse("2012-03-02T16:05:37Z")
RUBY
end

it 'does not register an offense when attaching timezone specifier `z`' do
expect_no_offenses(<<~RUBY)
Time.parse("2012-03-02T16:05:37z")
RUBY
end

it 'does not register an offense when attaching timezone specifier `E`' do
expect_no_offenses(<<~RUBY)
Time.parse("2012-03-02T16:05:37E")
RUBY
end

it 'registers an offense for Time.at' do
expect_offense(<<~RUBY)
Time.at(ts)
Expand Down

0 comments on commit a7dfbdf

Please sign in to comment.