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

Ticket #8848: docs_numeric_time.2.diff

File docs_numeric_time.2.diff, 5.9 kB (added by jeremymcanally, 8 months ago)

Fixed markup and added some niceties

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

    old new  
    11module ActiveSupport #:nodoc: 
    22  module CoreExtensions #:nodoc: 
    33    module Numeric #:nodoc: 
    4       # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years. 
     4      # Enables the use of time calculations and declarations, like <tt>45.minutes + 2.hours + 4.years</tt>. 
     5      #  
     6      # These methods use +Time#advance+ for precise date calculations when using from_now, ago, and so on, 
     7      # as well as adding or subtracting their results from a +Time+ object. For example: 
    58      # 
    6       # These methods use Time#advance for precise date calculations when using from_now, ago, etc.  
    7       # as well as adding or subtracting their results from a Time object. For example: 
    8       # 
    99      #   # equivalent to Time.now.advance(:months => 1) 
    1010      #   1.month.from_now 
    1111      # 
     
    1616      #   (4.months + 5.years).from_now 
    1717      #  
    1818      # While these methods provide precise calculation when used as in the examples above, care 
    19       # should be taken to note that this is not true if the result of `months', `years', etc is 
     19      # should be taken to note that this is not true if the result of +months+, +years+, and so on is 
    2020      # converted before use: 
    2121      # 
    2222      #   # equivalent to 30.days.to_i.from_now 
     
    2828      # In such cases, Ruby's core  
    2929      # Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and  
    3030      # Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision 
    31       # date and time arithmetic 
     31      # date and time arithmetic. 
    3232      module Time 
    3333        def seconds 
    3434          ActiveSupport::Duration.new(self, [[:seconds, self]]) 
    3535        end 
    3636        alias :second :seconds 
    3737 
     38        # Returns the number of seconds for the specified number of minutes 
     39        # 
     40        #   30.minutes 
     41        #   # => 1800 seconds 
     42        # 
     43        #   30.minutes.to_i 
     44        #   # => 1800 
    3845        def minutes 
    3946          ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]]) 
    4047        end 
    4148        alias :minute :minutes   
    4249         
     50        # Returns the number of seconds for the specified number of hours 
     51        # 
     52        #   2.hours 
     53        #   # => 7200 seconds 
     54        # 
     55        #   2.hours.to_i 
     56        #   # => 7200 
    4357        def hours 
    4458          ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]]) 
    4559        end 
    4660        alias :hour :hours 
    4761         
     62        # Returns the number of seconds for the specified number of days. 
     63        # The default return is a string representation of the seconds in days. 
     64        # 
     65        #   2.days 
     66        #   # => 2 days 
     67        # 
     68        #   2.days.to_i 
     69        #   # => 172800 
    4870        def days 
    4971          ActiveSupport::Duration.new(self * 24.hours, [[:days, self]]) 
    5072        end 
    5173        alias :day :days 
    5274 
     75        # Returns the number of seconds for the specified number of weeks. 
     76        # The default return is a string representation of the seconds in days. 
     77        # 
     78        #   3.weeks 
     79        #   # => 21 days 
     80        # 
     81        #   3.weeks.to_i 
     82        #   # => 1814400 
    5383        def weeks 
    5484          ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]]) 
    5585        end 
    5686        alias :week :weeks 
    5787         
     88        # Returns the number of seconds for the specified number of fortnights. 
     89        # The default return is a string representation of the seconds in days. 
     90        # 
     91        #   2.fortnights 
     92        #   # => 28 days 
     93        # 
     94        #   2.fortnights 
     95        #   # => 2419200 
    5896        def fortnights 
    5997          ActiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]]) 
    6098        end 
    6199        alias :fortnight :fortnights 
    62100         
     101        # Returns the number of seconds for the specified number of months. 
     102        # The default return is a string representation of the seconds in months.         
     103        # 
     104        #   2.months 
     105        #   # => 2 months 
     106        # 
     107        #   2.months 
     108        #   # => 5184000 
    63109        def months 
    64110          ActiveSupport::Duration.new(self * 30.days, [[:months, self]]) 
    65111        end 
    66112        alias :month :months 
    67113 
     114        # Returns the number of seconds for the number of years specified. 
     115        # The default return is a string representation of the seconds in years. 
     116        # 
     117        #  5.years 
     118        #  # => 5 years 
     119        #   
     120        #  5.years.to_i 
     121        #  # => 157788000 
    68122        def years 
    69123          ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]]) 
    70124        end 
    71125        alias :year :years 
    72126 
    73         # Reads best without arguments:  10.minutes.ago 
     127        # Returns a +Time+ object of the current time adjusted backward by the specified amount. 
     128        # 
     129        #   DateTime.now 
     130        #   # => Sun, 01 Jul 2007 02:00:00 -0500 
     131        # 
     132        #   1.hour.ago 
     133        #   # => Sun, 01 Jul 2007 01:00:00 -0500 
    74134        def ago(time = ::Time.now) 
    75135          time - self 
    76136        end 
    77137 
    78         # Reads best with argument:  10.minutes.until(time) 
     138        # Reads best with argument: <tt>10.minutes.until(time)</tt> 
    79139        alias :until :ago 
    80140 
    81         # Reads best with argument:  10.minutes.since(time) 
     141        # Reads best with argument: <tt>10.minutes.since(time)</tt> 
    82142        def since(time = ::Time.now) 
    83143          time + self 
    84144        end 
    85145 
    86         # Reads best without arguments:  10.minutes.from_now 
     146        # Returns a +Time+ object of the current time adjusted forward by the specified amount. 
     147        # 
     148        #   DateTime.now 
     149        #   # => Sun, 01 Jul 2007 02:00:00 -0500 
     150        # 
     151        #   1.hour.from_now 
     152        #   # => Sun, 01 Jul 2007 03:00:00 -0500 
    87153        alias :from_now :since 
    88154      end 
    89155    end