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

Changeset 8279

Show
Ignore:
Timestamp:
12/05/07 06:24:39 (5 months ago)
Author:
nzkoz
Message:

Document Date conversions. Closes #10368 [chuyeow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/date/conversions.rb

    r8199 r8279  
    2424        end 
    2525 
     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" 
    2641        def to_formatted_s(format = :default) 
    2742          if formatter = DATE_FORMATS[format] 
     
    4156        end 
    4257 
    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+. 
    4460        def to_date 
    4561          self 
    4662        end if RUBY_VERSION < '1.9' 
    4763 
    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 
    5074        def to_time(form = :local) 
    5175          ::Time.send("#{form}_time", year, month, day) 
    5276        end 
    5377 
    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 
    5585        def to_datetime 
    5686          ::DateTime.civil(year, month, day, 0, 0, 0, 0)