class RuboCop::Cop::Style::DateTime
Checks for consistent usage of the ‘DateTime` class over the `Time` class. This cop is disabled by default since these classes, although highly overlapping, have particularities that make them not replaceable in certain situations when dealing with multiple timezones and/or DST.
@safety
Autocorrection is not safe, because `DateTime` and `Time` do not have exactly the same behavior, although in most cases the autocorrection will be fine.
@example
# bad - uses `DateTime` for current time DateTime.now # good - uses `Time` for current time Time.now # bad - uses `DateTime` for modern date DateTime.iso8601('2016-06-29') # good - uses `Time` for modern date Time.iso8601('2016-06-29') # good - uses `DateTime` with start argument for historical date DateTime.iso8601('1751-04-23', Date::ENGLAND)
@example AllowCoercion: false (default)
# bad - coerces to `DateTime` something.to_datetime # good - coerces to `Time` something.to_time
@example AllowCoercion: true
# good something.to_datetime # good something.to_time
Constants
- CLASS_MSG
- COERCION_MSG
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/date_time.rb, line 70 def on_send(node) return unless date_time?(node) || (to_datetime?(node) && disallow_coercion?) return if historic_date?(node) message = to_datetime?(node) ? COERCION_MSG : CLASS_MSG add_offense(node, message: message) { |corrector| autocorrect(corrector, node) } end
Also aliased as: on_csend
Private Instance Methods
autocorrect(corrector, node)
click to toggle source
# File lib/rubocop/cop/style/date_time.rb, line 86 def autocorrect(corrector, node) return if to_datetime?(node) corrector.replace(node.receiver.loc.name, 'Time') end
disallow_coercion?()
click to toggle source
# File lib/rubocop/cop/style/date_time.rb, line 82 def disallow_coercion? !cop_config['AllowCoercion'] end