Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activesupport / lib / active_support / core_ext / date_time / conversions.rb
blob45d09f344d6773738397ce2037625dd04a0ce86f
1 module ActiveSupport #:nodoc:
2   module CoreExtensions #:nodoc:
3     module DateTime #:nodoc:
4       # Getting datetimes in different convenient string representations and other objects
5       module Conversions
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           end
13         end
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
19             else
20               strftime(formatter)
21             end
22           else
23             to_datetime_default_s
24           end
25         end
27         # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"
28         def readable_inspect
29           to_s(:rfc822)
30         end
32         # Converts self to a Ruby Date object; time portion is discarded
33         def to_date
34           ::Date.new(year, month, day)
35         end
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
39         def to_time
40           self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self
41         end        
43         # To be able to keep Times, Dates and DateTimes interchangeable on conversions
44         def to_datetime
45           self
46         end
47         
48         def xmlschema
49           strftime("%Y-%m-%dT%H:%M:%S#{offset == 0 ? 'Z' : '%Z'}")
50         end
51       end
52     end
53   end
54 end