Ticket #10368: document_date_conversions.diff
| File document_date_conversions.diff, 2.9 kB (added by chuyeow, 11 months ago) |
|---|
-
activesupport/lib/active_support/core_ext/date/conversions.rb
old new 23 23 end 24 24 end 25 25 26 # Convert to a formatted string - see DATE_FORMATS for predefined formats. 27 # You can also add your own formats to the DATE_FORMATS constant and use them with this method. 28 # 29 # This method is also aliased as <tt>to_s</tt>. 30 # 31 # ==== Examples: 32 # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007 33 # 34 # date.to_formatted_s(:db) # => "2007-11-10" 35 # date.to_s(:db) # => "2007-11-10" 36 # 37 # date.to_formatted_s(:short) # => "10 Nov" 38 # date.to_formatted_s(:long) # => "November 10, 2007" 39 # date.to_formatted_s(:long_ordinal) # => "November 10th, 2007" 40 # date.to_formatted_s(:rfc822) # => "10 Nov 2007" 26 41 def to_formatted_s(format = :default) 27 42 if formatter = DATE_FORMATS[format] 28 43 if formatter.respond_to?(:call) … … 40 55 strftime("%a, %d %b %Y") 41 56 end 42 57 43 # To be able to keep Times, Dates and DateTimes interchangeable on conversions 58 # A method to keep Time, Date and DateTime instances interchangeable on conversions. 59 # In this case, it simply returns +self+. 44 60 def to_date 45 61 self 46 62 end if RUBY_VERSION < '1.9' 47 63 48 # Converts self to a Ruby Time object; time is set to beginning of day 49 # Timezone can either be :local or :utc (default :local) 64 # Converts a Date instance to a Time, where the time is set to the beginning of the day. 65 # The timezone can be either :local or :utc (default :local). 66 # 67 # ==== Examples: 68 # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007 69 # 70 # date.to_time # => Sat Nov 10 00:00:00 0800 2007 71 # date.to_time(:local) # => Sat Nov 10 00:00:00 0800 2007 72 # 73 # date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007 50 74 def to_time(form = :local) 51 75 ::Time.send("#{form}_time", year, month, day) 52 76 end 53 77 54 # Converts self to a Ruby DateTime object; time is set to beginning of day 78 # Converts a Date instance to a DateTime, where the time is set to the beginning of the day 79 # and UTC offset is set to 0. 80 # 81 # ==== Example: 82 # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007 83 # 84 # date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000 55 85 def to_datetime 56 86 ::DateTime.civil(year, month, day, 0, 0, 0, 0) 57 87 end if RUBY_VERSION < '1.9'