lib/rubocop/cop/rails/time_zone.rb in rubocop-rails-2.9.1 vs lib/rubocop/cop/rails/time_zone.rb in rubocop-rails-2.10.0

- old
+ new

@@ -17,30 +17,31 @@ # @example EnforcedStyle: strict # # `strict` means that `Time` should be used with `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. # # @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 # Time.at(timestamp).in_time_zone class TimeZone < Base @@ -61,10 +62,12 @@ DANGEROUS_METHODS = %i[now local new parse at current].freeze 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 # (`Time` or `::Time`) return unless (mod.nil? || mod.cbase_type?) && method_send?(node) @@ -114,13 +117,14 @@ node = node.parent end 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('.') return if offset_provided?(node) @@ -128,9 +132,13 @@ message = build_message(klass, method_name, node) add_offense(node.loc.selector, message: message) do |corrector| autocorrect(corrector, 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(