Ticket #8848: docs_numeric_time.diff
| File docs_numeric_time.diff, 3.2 kB (added by dlehman, 1 year ago) |
|---|
-
activesupport/lib/active_support/core_ext/numeric/time.rb
old new 2 2 module CoreExtensions #:nodoc: 3 3 module Numeric #:nodoc: 4 4 # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years. 5 # 5 # 6 6 # These methods use Time#advance for precise date calculations when using from_now, ago, etc. 7 7 # as well as adding or subtracting their results from a Time object. For example: 8 8 # … … 35 35 end 36 36 alias :second :seconds 37 37 38 # Returns the number of seconds for the specified number of minutes 39 # 40 # 30.minutes 41 # # => 1800 seconds 38 42 def minutes 39 43 ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]]) 40 44 end 41 45 alias :minute :minutes 42 46 47 # Returns the number of seconds for the specified number of hours 48 # 49 # 2.hours 50 # # => 7200 seconds 43 51 def hours 44 52 ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]]) 45 53 end 46 54 alias :hour :hours 47 55 56 # Returns the number of days for the specified number of days 57 # 58 # 2.days 59 # # => 2 days 48 60 def days 49 61 ActiveSupport::Duration.new(self * 24.hours, [[:days, self]]) 50 62 end 51 63 alias :day :days 52 64 65 # Returns the number of days for the specified number of weeks 66 # 67 # 3.weeks 68 # # => 21 days 53 69 def weeks 54 70 ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]]) 55 71 end 56 72 alias :week :weeks 57 73 74 # Returns the number of days for the specified number of fortnights 75 # 76 # 2.fortnights 77 # # => 28 days 58 78 def fortnights 59 79 ActiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]]) 60 80 end 61 81 alias :fortnight :fortnights 62 82 83 # Returns the number of months for the specified number of months 84 # 85 # 2.months 86 # # => 2 months 63 87 def months 64 88 ActiveSupport::Duration.new(self * 30.days, [[:months, self]]) 65 89 end … … 70 94 end 71 95 alias :year :years 72 96 73 # Reads best without arguments: 10.minutes.ago 97 # Returns Time object of the current time adjusted backward by the specified amount 98 # 99 # DateTime.now 100 # # => Sun, 01 Jul 2007 02:00:00 -0500 101 # 1.hour.ago 102 # # => Sun, 01 Jul 2007 01:00:00 -0500 74 103 def ago(time = ::Time.now) 75 104 time - self 76 105 end … … 83 112 time + self 84 113 end 85 114 86 # Reads best without arguments: 10.minutes.from_now 115 # Returns Time object of the current time adjusted forwards by the specified amount 116 # 117 # DateTime.now 118 # # => Sun, 01 Jul 2007 02:00:00 -0500 119 # 1.hour.from_now 120 # # => Sun, 01 Jul 2007 03:00:00 -0500 87 121 alias :from_now :since 88 122 end 89 123 end