Module: ActiveSupport::CoreExtensions::DateTime::Conversions
- Included in:
- DateTime
- Defined in:
- lib/active_support/core_ext/date_time/conversions.rb
Overview
Getting datetimes in different convenient string representations and other objects
Class Method Summary collapse
Instance Method Summary collapse
-
#readable_inspect ⇒ Object
Overrides the default inspect method with a human readable one, e.g., “Mon, 21 Feb 2005 14:30:00 +0000”.
-
#to_date ⇒ Object
Converts self to a Ruby Date object; time portion is discarded.
-
#to_datetime ⇒ Object
To be able to keep Times, Dates and DateTimes interchangeable on conversions.
-
#to_formatted_s(format = :default) ⇒ Object
Convert to a formatted string - see DATE_FORMATS for predefined formats.
-
#to_time ⇒ Object
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.
-
#xmlschema ⇒ Object
Converts datetime to an appropriate format for use in XML.
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 6 def self.included(base) base.class_eval do alias_method :to_datetime_default_s, :to_s alias_method :to_s, :to_formatted_s alias_method :default_inspect, :inspect alias_method :inspect, :readable_inspect # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows # DateTimes outside the range of what can be created with Time. remove_method :to_time if base.instance_methods.include?(:to_time) end end |
Instance Method Details
#readable_inspect ⇒ Object
Overrides the default inspect method with a human readable one, e.g., “Mon, 21 Feb 2005 14:30:00 +0000”
47 48 49 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 47 def readable_inspect to_s(:rfc822) end |
#to_date ⇒ Object
Converts self to a Ruby Date object; time portion is discarded
52 53 54 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 52 def to_date ::Date.new(year, month, day) end |
#to_datetime ⇒ Object
To be able to keep Times, Dates and DateTimes interchangeable on conversions
63 64 65 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 63 def to_datetime self end |
#to_formatted_s(format = :default) ⇒ Object
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"
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 34 def to_formatted_s(format = :default) if formatter = ::Time::DATE_FORMATS[format] if formatter.respond_to?(:call) formatter.call(self).to_s else strftime(formatter) end else to_datetime_default_s end end |
#to_time ⇒ Object
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
58 59 60 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 58 def to_time self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self end |
#xmlschema ⇒ Object
Converts datetime to an appropriate format for use in XML
68 69 70 |
# File 'lib/active_support/core_ext/date_time/conversions.rb', line 68 def xmlschema strftime("%Y-%m-%dT%H:%M:%S%Z") end |