Ticket #7164: documentation_for_time_conversions.diff
| File documentation_for_time_conversions.diff, 2.8 kB (added by jeremymcanally, 1 year ago) |
|---|
-
activesupport/lib/active_support/core_ext/time/conversions.rb
old new 19 19 end 20 20 end 21 21 22 # Convert to a formatted string - see DATE_FORMATS for predefined formats. 23 # You can also add your own formats to the DATE_FORMATS constant and use them with this method. 24 # 25 # ==== Examples: 26 # time = Time.now # => Thu Jan 18 06:10:17 CST 2007 27 # 28 # time.to_formatted_s(:time) # => "Thu Jan 18 06:10:17 CST 2007" 29 # time.to_formatted_s(:db) # => "2007-01-18 06:10:17" 30 # time.to_formatted_s(:short) # => "18 Jan 06:10"</tt> 31 # time.to_formatted_s(:long) # => "January 18, 2007 06:10" 32 # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600" 22 33 def to_formatted_s(format = :default) 23 34 if formatter = DATE_FORMATS[format] 24 35 if formatter.respond_to?(:call) … … 31 41 end 32 42 end 33 43 34 # Converts self to a Ruby Date object; time portion is discarded 44 # Convert a Time object to a Date, dropping hour, minute, and second precision. 45 # 46 # ==== Examples 47 # my_time = Time.now 48 # # => Mon Nov 12 22:59:51 -0500 2007 49 # 50 # my_time.to_date 51 # #=> Mon, 12 Nov 2007 52 # 53 # your_time = Time.parse("1/13/2009 1:13:03 P.M.") 54 # # => Tue Jan 13 13:13:03 -0500 2009 55 # 56 # your_time.to_date 57 # # => Tue, 13 Jan 2009 35 58 def to_date 36 59 ::Date.new(year, month, day) 37 60 end 38 61 39 # To be able to keep Times, Dates and DateTimes interchangeable on conversions 62 # A method to be able to keep Time, Date and DateTime instances interchangeable on conversions. 63 # In this case, it simply returns +self+. 40 64 def to_time 41 65 self 42 66 end 43 67 44 # converts to a Ruby DateTime instance; preserves utc offset 68 # Converts a Time instance to a Ruby DateTime instance, preserving UTC offset. 69 # 70 # ==== Examples 71 # my_time = Time.now 72 # # => Mon Nov 12 23:04:21 -0500 2007 73 # 74 # my_time.to_datetime 75 # # => Mon, 12 Nov 2007 23:04:21 -0500 76 # 77 # your_time = Time.parse("1/13/2009 1:13:03 P.M.") 78 # # => Tue Jan 13 13:13:03 -0500 2009 79 # 80 # your_time.to_datetime 81 # # => Tue, 13 Jan 2009 13:13:03 -0500 45 82 def to_datetime 46 83 ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400), 0) 47 84 end