Skip to content

Commit

Permalink
[Fix #800] Make Rails/TimeZone aware of timezone offset
Browse files Browse the repository at this point in the history
  • Loading branch information
inkstak committed Oct 5, 2022
1 parent b1a199d commit 8fedb3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#800](https://github.com/rubocop/rubocop-rails/issues/800): Make `Rails/TimeZone` aware of timezone UTF offset. ([@inkstak][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ 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
TIMEZONE_SPECIFIER = /([A-z]|[+-]\d{2}:?\d{2})\z/.freeze

def on_const(node)
mod, klass = *node
Expand Down Expand Up @@ -126,7 +126,7 @@ def check_time_node(klass, node)
end

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

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

it 'does not register an offense when attaching timezone offset' do
expect_no_offenses(<<~RUBY)
Time.parse("2012-03-02 16:05:37 +0100")
RUBY
end

it 'does not register an offense when attaching timezone offset using a colon' do
expect_no_offenses(<<~RUBY)
Time.parse("2012-03-02 16:05:37 +01:00")
RUBY
end

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

0 comments on commit 8fedb3f

Please sign in to comment.