1 module ActiveSupport #:nodoc:
2 module CoreExtensions #:nodoc:
3 module DateTime #:nodoc:
4 # Getting datetimes in different convenient string representations and other objects
6 def self.included(base)
8 alias_method :to_datetime_default_s, :to_s
9 alias_method :to_s, :to_formatted_s
10 alias_method :default_inspect, :inspect
11 alias_method :inspect, :readable_inspect
15 def to_formatted_s(format = :default)
16 if formatter = ::Time::DATE_FORMATS[format]
17 if formatter.respond_to?(:call)
18 formatter.call(self).to_s
27 # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"
32 # Converts self to a Ruby Date object; time portion is discarded
34 ::Date.new(year, month, day)
37 # Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class
38 # If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time
40 self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self
43 # To be able to keep Times, Dates and DateTimes interchangeable on conversions
49 strftime("%Y-%m-%dT%H:%M:%S#{offset == 0 ? 'Z' : '%Z'}")