Module: ActiveSupport::CoreExtensions::Time::Conversions

Included in:
Time
Defined in:
lib/active_support/core_ext/time/conversions.rb

Overview

Getting times in different convenient string representations and other objects

Constant Summary collapse

DATE_FORMATS =
{
  :db     => "%Y-%m-%d %H:%M:%S",
  :short  => "%d %b %H:%M",
  :long   => "%B %d, %Y %H:%M",
  :rfc822 => "%a, %d %b %Y %H:%M:%S %z"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(klass) ⇒ Object


15
16
17
18
19
# File 'lib/active_support/core_ext/time/conversions.rb', line 15

def self.append_features(klass)
  super
  klass.send(:alias_method, :to_default_s, :to_s)
  klass.send(:alias_method, :to_s, :to_formatted_s)
end

Instance Method Details

#to_dateObject


25
26
27
# File 'lib/active_support/core_ext/time/conversions.rb', line 25

def to_date
  ::Date.new(year, month, day)
end

#to_formatted_s(format = :default) ⇒ Object


21
22
23
# File 'lib/active_support/core_ext/time/conversions.rb', line 21

def to_formatted_s(format = :default)
  DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_default_s          
end

#to_timeObject

To be able to keep Dates and Times interchangeable on conversions


30
31
32
# File 'lib/active_support/core_ext/time/conversions.rb', line 30

def to_time
  self
end