Class: RuboCop::Cop::Rails::TimeZone

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rails/time_zone.rb

Overview

Checks for the use of Time methods without zone.

Built on top of Ruby on Rails style guide (rails.rubystyle.guide#time) and the article danilenko.org/2012/7/6/rails_timezones/

Two styles are supported for this cop. When `EnforcedStyle` is 'strict' then only use of `Time.zone` is allowed.

When EnforcedStyle is 'flexible' then it's also allowed to use `Time#in_time_zone`.

Examples:

# bad
Time.now
Time.parse('2015-03-02T19:05:37')

# good
Time.current
Time.zone.now
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)

# `flexible` allows usage of `in_time_zone` instead of `zone`.

# good
Time.at(timestamp).in_time_zone

EnforcedStyle: strict

# `strict` means that `Time` should be used with `zone`.

# bad
Time.at(timestamp).in_time_zone

Constant Summary collapse

MSG =
'Do not use `%<current>s` without zone. Use `%<prefer>s` instead.'
MSG_ACCEPTABLE =
'Do not use `%<current>s` without zone. Use one of %<prefer>s instead.'
MSG_LOCALTIME =
'Do not use `Time.localtime` without offset or zone.'
GOOD_METHODS =
%i[zone zone_default find_zone find_zone!].freeze
DANGEROUS_METHODS =
%i[now local new parse at].freeze
ACCEPTED_METHODS =
%i[in_time_zone utc getlocal xmlschema iso8601 jisx0301 rfc3339 httpdate to_i to_f].freeze
TIMEZONE_SPECIFIER =
/[A-z]/.freeze

Instance Method Summary collapse

Instance Method Details

#on_const(node) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/rails/time_zone.rb', line 60

def on_const(node)
  mod, klass = *node
  # we should only check core classes
  # (`Time` or `::Time`)
  return unless (mod.nil? || mod.cbase_type?) && method_send?(node)

  check_time_node(klass, node.parent) if klass == :Time
end