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

Ticket #7164: documentation_for_time_conversions.with_alias.r8258.2.diff

File documentation_for_time_conversions.with_alias.r8258.2.diff, 3.0 kB (added by chuyeow, 7 months ago)
  • 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        # This method is also aliased as <tt>to_s</tt>. 
     26        # 
     27        # ==== Examples: 
     28        #   time = Time.now                     # => Thu Jan 18 06:10:17 CST 2007 
     29        # 
     30        #   time.to_formatted_s(:time)          # => "06:10:17" 
     31        #   time.to_s(:time)                    # => "06:10:17" 
     32        # 
     33        #   time.to_formatted_s(:db)            # => "2007-01-18 06:10:17" 
     34        #   time.to_formatted_s(:short)         # => "18 Jan 06:10" 
     35        #   time.to_formatted_s(:long)          # => "January 18, 2007 06:10" 
     36        #   time.to_formatted_s(:long_ordinal)  # => "January 18th, 2007 06:10" 
     37        #   time.to_formatted_s(:rfc822)        # => "Thu, 18 Jan 2007 06:10:17 -0600" 
    2238        def to_formatted_s(format = :default) 
    2339          if formatter = DATE_FORMATS[format] 
    2440            if formatter.respond_to?(:call) 
     
    3147          end 
    3248        end 
    3349 
    34         # Converts self to a Ruby Date object; time portion is discarded 
     50        # Convert a Time object to a Date, dropping hour, minute, and second precision. 
     51        # 
     52        # ==== Examples 
     53        #   my_time = Time.now 
     54        #   # => Mon Nov 12 22:59:51 -0500 2007 
     55        # 
     56        #   my_time.to_date 
     57        #   #=> Mon, 12 Nov 2007 
     58        # 
     59        #   your_time = Time.parse("1/13/2009 1:13:03 P.M.") 
     60        #   # => Tue Jan 13 13:13:03 -0500 2009 
     61        # 
     62        #   your_time.to_date 
     63        #   # => Tue, 13 Jan 2009 
    3564        def to_date 
    3665          ::Date.new(year, month, day) 
    3766        end 
    3867 
    39         # To be able to keep Times, Dates and DateTimes interchangeable on conversions 
     68        # A method to keep Time, Date and DateTime instances interchangeable on conversions. 
     69        # In this case, it simply returns +self+. 
    4070        def to_time 
    4171          self 
    4272        end 
    4373 
    44         # converts to a Ruby DateTime instance; preserves utc offset 
     74        # Converts a Time instance to a Ruby DateTime instance, preserving UTC offset. 
     75        # 
     76        # ==== Examples 
     77        #   my_time = Time.now 
     78        #   # => Mon Nov 12 23:04:21 -0500 2007 
     79        # 
     80        #   my_time.to_datetime 
     81        #   # => Mon, 12 Nov 2007 23:04:21 -0500 
     82        # 
     83        #   your_time = Time.parse("1/13/2009 1:13:03 P.M.") 
     84        #   # => Tue Jan 13 13:13:03 -0500 2009 
     85        # 
     86        #   your_time.to_datetime 
     87        #   # => Tue, 13 Jan 2009 13:13:03 -0500 
    4588        def to_datetime 
    4689          ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) 
    4790        end