Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7164: documentation_for_time_conversions.diff

File documentation_for_time_conversions.diff, 2.8 kB (added by jeremymcanally, 1 year ago)

Added some examples for other methods and made patch apply cleanly to edge.

  • activesupport/lib/active_support/core_ext/time/conversions.rb

    old new  
    1919          end 
    2020        end 
    2121 
     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" 
    2233        def to_formatted_s(format = :default) 
    2334          if formatter = DATE_FORMATS[format] 
    2435            if formatter.respond_to?(:call) 
     
    3141          end 
    3242        end 
    3343 
    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 
    3558        def to_date 
    3659          ::Date.new(year, month, day) 
    3760        end 
    3861 
    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+. 
    4064        def to_time 
    4165          self 
    4266        end 
    4367 
    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 
    4582        def to_datetime 
    4683          ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400), 0) 
    4784        end