Module ActiveSupport::CoreExtensions::DateTime::Conversions
In: vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb

Getting datetimes in different convenient string representations and other objects

Methods

Public Class methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 6
 6:         def self.included(base)
 7:           base.class_eval do
 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
12: 
13:             # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
14:             # DateTimes outside the range of what can be created with Time.
15:             remove_method :to_time if base.instance_methods.include?(:to_time)
16:           end
17:         end

Public Instance methods

Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 47
47:         def readable_inspect
48:           to_s(:rfc822)
49:         end

Converts self to a Ruby Date object; time portion is discarded

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 52
52:         def to_date
53:           ::Date.new(year, month, day)
54:         end

To be able to keep Times, Dates and DateTimes interchangeable on conversions

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 63
63:         def to_datetime
64:           self
65:         end

Convert to a formatted string - see DATE_FORMATS for predefined formats. You can also add your own formats to the DATE_FORMATS constant and use them with this method.

This method is also aliased as to_s.

Examples:

  datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0)   # => Tue, 04 Dec 2007 00:00:00 +0000

  datetime.to_formatted_s(:db)            # => "2007-12-04 00:00:00"
  datetime.to_s(:db)                      # => "2007-12-04 00:00:00"
  datetime.to_s(:number)                  # => "20071204000000"
  datetime.to_formatted_s(:short)         # => "04 Dec 00:00"
  datetime.to_formatted_s(:long)          # => "December 04, 2007 00:00"
  datetime.to_formatted_s(:long_ordinal)  # => "December 4th, 2007 00:00"
  datetime.to_formatted_s(:rfc822)        # => "Tue, 04 Dec 2007 00:00:00 +0000"

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 34
34:         def to_formatted_s(format = :default)
35:           if formatter = ::Time::DATE_FORMATS[format]
36:             if formatter.respond_to?(:call)
37:               formatter.call(self).to_s
38:             else
39:               strftime(formatter)
40:             end
41:           else
42:             to_datetime_default_s
43:           end
44:         end

Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class 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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 58
58:         def to_time
59:           self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self
60:         end

Converts datetime to an appropriate format for use in XML

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 68
68:         def xmlschema
69:           strftime("%Y-%m-%dT%H:%M:%S%Z")
70:         end

[Validate]