Skip to content

Commit 22ed441

Browse files
committed
Enhance Rails/TimeZone to accept methods with the :in timezone option
1 parent 249c03b commit 22ed441

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#821](https://github.com/rubocop/rubocop-rails/issues/821): Enhance `Rails/TimeZone` to accept methods with the `:in` timezone option. ([@fatkodima][])

lib/rubocop/cop/rails/time_zone.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,25 @@ def acceptable_methods(klass, method_name, node)
228228
acceptable
229229
end
230230

231-
# Time.new can be called with a time zone offset
231+
# Time.new, Time.at, and Time.now can be called with a time zone offset
232232
# When it is, that should be considered safe
233233
# Example:
234234
# Time.new(1988, 3, 15, 3, 0, 0, "-05:00")
235235
def offset_provided?(node)
236-
node.arguments.size >= 7
236+
case node.method_name
237+
when :new
238+
node.arguments.size == 7 || offset_option_provided?(node)
239+
when :at, :now
240+
offset_option_provided?(node)
241+
end
242+
end
243+
244+
def offset_option_provided?(node)
245+
options = node.last_argument
246+
options&.hash_type? &&
247+
options.each_pair.any? do |pair|
248+
pair.key.sym_type? && pair.key.value == :in && !pair.value.nil_type?
249+
end
237250
end
238251
end
239252
end

spec/rubocop/cop/rails/time_zone_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
RUBY
1212
end
1313

14+
it 'registers an offense for Time.now with nil :in option' do
15+
expect_offense(<<~RUBY)
16+
Time.now(in: nil)
17+
^^^ Do not use `Time.now` without zone. Use `Time.zone.now` instead.
18+
RUBY
19+
end
20+
21+
it 'does not register an offense for Time.now with :in option' do
22+
expect_no_offenses(<<~RUBY)
23+
Time.now(in: '+03:00')
24+
RUBY
25+
end
26+
1427
it 'registers an offense for Time.new without argument' do
1528
expect_offense(<<~RUBY)
1629
Time.new
@@ -45,6 +58,19 @@
4558
RUBY
4659
end
4760

61+
it 'registers an offense for Time.new with nil :in option' do
62+
expect_offense(<<~RUBY)
63+
Time.new(2012, 6, 10, 12, 00, in: nil)
64+
^^^ Do not use `Time.new` without zone. Use `Time.zone.local` instead.
65+
RUBY
66+
end
67+
68+
it 'does not register an offense for Time.new with :in option' do
69+
expect_no_offenses(<<~RUBY)
70+
Time.new(2012, 6, 10, 12, 00, in: '-05:00')
71+
RUBY
72+
end
73+
4874
it 'registers an offense for ::Time.now' do
4975
expect_offense(<<~RUBY)
5076
::Time.now
@@ -149,6 +175,19 @@
149175
RUBY
150176
end
151177

178+
it 'registers an offense for Time.at with nil :in option' do
179+
expect_offense(<<~RUBY)
180+
Time.at(ts, in: nil)
181+
^^ Do not use `Time.at` without zone. Use `Time.zone.at` instead.
182+
RUBY
183+
end
184+
185+
it 'does not register an offense for Time.at with :in option' do
186+
expect_no_offenses(<<~RUBY)
187+
Time.at(ts, in: '+03:00')
188+
RUBY
189+
end
190+
152191
it 'registers an offense for Time.parse.localtime(offset)' do
153192
expect_offense(<<~RUBY)
154193
Time.parse('12:00').localtime('+03:00')

0 commit comments

Comments
 (0)