Enables the use of time calculations within Time itself

Methods
Classes and Modules
Module ActiveSupport::CoreExtensions::Time::Calculations::ClassMethods
Public Instance methods
advance(options)

Uses Date to provide precise Time calculations for years, months, and days. The options parameter takes a hash with any of these keys: :months, :days, :years.

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 49
49:         def advance(options)
50:           d = ::Date.new(year + (options.delete(:years) || 0), month, day)
51:           d = d >> options.delete(:months) if options[:months]
52:           d = d +  options.delete(:days)   if options[:days]
53:           change(options.merge(:year => d.year, :month => d.month, :mday => d.day))
54:         end
ago(seconds)

Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension Do not use this method in combination with x.months, use months_ago instead!

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 58
58:         def ago(seconds)
59:           self.since(-seconds)
60:         end
at_beginning_of_day()

Alias for beginning_of_day

at_beginning_of_month()

Alias for beginning_of_month

at_beginning_of_quarter()
at_beginning_of_week()

Alias for beginning_of_week

at_beginning_of_year()

Alias for beginning_of_year

at_end_of_month()

Alias for end_of_month

at_midnight()

Alias for beginning_of_day

beginning_of_day()

Returns a new Time representing the start of the day (0:00)

This method is also aliased as midnight at_midnight at_beginning_of_day
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 145
145:         def beginning_of_day
146:           (self - self.seconds_since_midnight).change(:usec => 0)
147:         end
beginning_of_month()

Returns a new Time representing the start of the month (1st of the month, 0:00)

This method is also aliased as at_beginning_of_month
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 153
153:         def beginning_of_month
154:           #self - ((self.mday-1).days + self.seconds_since_midnight)
155:           change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
156:         end
beginning_of_quarter()

Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)

This method is also aliased as at_beginning_of_quarter
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 168
168:         def beginning_of_quarter
169:           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
170:         end
beginning_of_week()

Returns a new Time representing the "start" of this week (Monday, 0:00)

This method is also aliased as monday at_beginning_of_week
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 131
131:         def beginning_of_week
132:           days_to_monday = self.wday!=0 ? self.wday-1 : 6
133:           (self - days_to_monday.days).midnight
134:         end
beginning_of_year()

Returns a new Time representing the start of the year (1st of january, 0:00)

This method is also aliased as at_beginning_of_year
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 174
174:         def beginning_of_year
175:           change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
176:         end
change(options)

Returns a new Time where one or more of the elements have been changed according to the options parameter. The time options (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and minute is passed, then sec and usec is set to 0.

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 34
34:         def change(options)
35:           ::Time.send(
36:             self.utc? ? :utc : :local, 
37:             options[:year]  || self.year, 
38:             options[:month] || self.month, 
39:             options[:mday]  || self.mday, 
40:             options[:hour]  || self.hour, 
41:             options[:min]   || (options[:hour] ? 0 : self.min),
42:             options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
43:             options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
44:           )
45:         end
end_of_month()

Returns a new Time representing the end of the month (last day of the month, 0:00)

This method is also aliased as at_end_of_month
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 160
160:         def end_of_month
161:           #self - ((self.mday-1).days + self.seconds_since_midnight)
162:           last_day = ::Time.days_in_month( self.month, self.year )
163:           change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0)
164:         end
in(seconds)

Alias for since

last_month()

Short-hand for months_ago(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 121
121:         def last_month
122:           months_ago(1)
123:         end
last_year()

Short-hand for years_ago(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 110
110:         def last_year
111:           years_ago(1)
112:         end
midnight()

Alias for beginning_of_day

monday()

Alias for beginning_of_week

months_ago(months)

Returns a new Time representing the time a number of specified months ago

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 73
73:         def months_ago(months)
74:           months_since(-months)
75:         end
months_since(months)
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 77
77:         def months_since(months)
78:           year, month, mday = self.year, self.month, self.mday
79: 
80:           month += months
81: 
82:           # in case months is negative
83:           while month < 1
84:             month += 12
85:             year -= 1
86:           end
87: 
88:           # in case months is positive
89:           while month > 12
90:             month -= 12
91:             year += 1
92:           end
93: 
94:           max = ::Time.days_in_month(month, year)
95:           mday = max if mday > max
96: 
97:           change(:year => year, :month => month, :mday => mday)
98:         end
next_month()

Short-hand for months_since(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 126
126:         def next_month
127:           months_since(1)
128:         end
next_week(day = :monday)

Returns a new Time representing the start of the given day in next week (default is Monday).

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 139
139:         def next_week(day = :monday)
140:           days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
141:           since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
142:         end
next_year()

Short-hand for years_since(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 115
115:         def next_year
116:           years_since(1)
117:         end
seconds_since_midnight()
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 27
27:         def seconds_since_midnight
28:           self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
29:         end
since(seconds)
 Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around

the Numeric extension. Do not use this method in combination with x.months, use months_since instead!

This method is also aliased as in
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 64
64:         def since(seconds)
65:           initial_dst = self.dst? ? 1 : 0
66:           f = seconds.since(self)
67:           final_dst   = f.dst? ? 1 : 0
68:           (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
69:         end
tomorrow()

Convenience method which returns a new Time representing the time 1 day since the instance time

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 185
185:         def tomorrow
186:           self.since(1.day)
187:         end
years_ago(years)

Returns a new Time representing the time a number of specified years ago

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 101
101:         def years_ago(years)
102:           change(:year => self.year - years)
103:         end
years_since(years)
     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 105
105:         def years_since(years)
106:           change(:year => self.year + years)
107:         end
yesterday()

Convenience method which returns a new Time representing the time 1 day ago

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 180
180:         def yesterday
181:           self.ago(1.day)
182:         end