Skip to content

Commit 7220b5c

Browse files
authored
Merge pull request rubocop#801 from inkstak/make_rails_time_zone_aware_of_timezone_offset
[Fix rubocop#800] Make `Rails/TimeZone` aware of timezone offset
2 parents b1a199d + 8fedb3f commit 7220b5c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#800](https://github.com/rubocop/rubocop-rails/issues/800): Make `Rails/TimeZone` aware of timezone UTF offset. ([@inkstak][])

lib/rubocop/cop/rails/time_zone.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class TimeZone < Base
5555

5656
ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601 jisx0301 rfc3339 httpdate to_i to_f].freeze
5757

58-
TIMEZONE_SPECIFIER = /[A-z]/.freeze
58+
TIMEZONE_SPECIFIER = /([A-z]|[+-]\d{2}:?\d{2})\z/.freeze
5959

6060
def on_const(node)
6161
mod, klass = *node
@@ -126,7 +126,7 @@ def check_time_node(klass, node)
126126
end
127127

128128
def attach_timezone_specifier?(date)
129-
date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s[-1])
129+
date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s)
130130
end
131131

132132
def build_message(klass, method_name, node)

spec/rubocop/cop/rails/time_zone_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@
123123
RUBY
124124
end
125125

126+
it 'does not register an offense when attaching timezone offset' do
127+
expect_no_offenses(<<~RUBY)
128+
Time.parse("2012-03-02 16:05:37 +0100")
129+
RUBY
130+
end
131+
132+
it 'does not register an offense when attaching timezone offset using a colon' do
133+
expect_no_offenses(<<~RUBY)
134+
Time.parse("2012-03-02 16:05:37 +01:00")
135+
RUBY
136+
end
137+
126138
it 'registers an offense for Time.at' do
127139
expect_offense(<<~RUBY)
128140
Time.at(ts)

0 commit comments

Comments
 (0)