Enables the use of time calculations within Time itself

Methods
Classes and Modules
Module ActiveSupport::CoreExtensions::Time::Calculations::ClassMethods
Constants
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
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: :years, :months, :weeks, :days, :hours, :minutes, :seconds.

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 102
102:         def advance(options)
103:           unless options[:weeks].nil?
104:             options[:weeks], partial_weeks = options[:weeks].divmod(1)
105:             options[:days] = (options[:days] || 0) + 7 * partial_weeks
106:           end
107:           
108:           unless options[:days].nil?
109:             options[:days], partial_days = options[:days].divmod(1)
110:             options[:hours] = (options[:hours] || 0) + 24 * partial_days
111:           end
112:           
113:           d = to_date.advance(options)
114:           time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
115:           seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
116:           seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
117:         end
ago(seconds)

Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 120
120:         def ago(seconds)
121:           self.since(-seconds)
122:         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_end_of_quarter()

Alias for end_of_quarter

at_end_of_week()

Alias for end_of_week

at_end_of_year()

Alias for end_of_year

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 activesupport/lib/active_support/core_ext/time/calculations.rb, line 212
212:         def beginning_of_day
213:           #(self - seconds_since_midnight).change(:usec => 0)
214:           change(:hour => 0, :min => 0, :sec => 0, :usec => 0)
215:         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 activesupport/lib/active_support/core_ext/time/calculations.rb, line 226
226:         def beginning_of_month
227:           #self - ((self.mday-1).days + self.seconds_since_midnight)
228:           change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
229:         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 activesupport/lib/active_support/core_ext/time/calculations.rb, line 241
241:         def beginning_of_quarter
242:           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
243:         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 activesupport/lib/active_support/core_ext/time/calculations.rb, line 191
191:         def beginning_of_week
192:           days_to_monday = self.wday!=0 ? self.wday-1 : 6
193:           (self - days_to_monday.days).midnight
194:         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 activesupport/lib/active_support/core_ext/time/calculations.rb, line 253
253:         def beginning_of_year
254:           change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
255:         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 activesupport/lib/active_support/core_ext/time/calculations.rb, line 85
85:         def change(options)
86:           ::Time.send(
87:             self.utc? ? :utc_time : :local_time,
88:             options[:year]  || self.year,
89:             options[:month] || self.month,
90:             options[:day]   || self.day,
91:             options[:hour]  || self.hour,
92:             options[:min]   || (options[:hour] ? 0 : self.min),
93:             options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
94:             options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
95:           )
96:         end
compare_with_coercion(other)

Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances can be chronologically compared with a Time

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 300
300:         def compare_with_coercion(other)
301:           # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparison
302:           other = other.comparable_time if other.respond_to?(:comparable_time)
303:           if other.acts_like?(:date)
304:             # other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=>
305:             to_datetime.compare_without_coercion(other)
306:           else
307:             compare_without_coercion(other)
308:           end
309:         end
end_of_day()

Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 221
221:         def end_of_day
222:           change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
223:         end
end_of_month()

Returns a new Time representing the end of the month (end of the last day of the month)

This method is also aliased as at_end_of_month
     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 233
233:         def end_of_month
234:           #self - ((self.mday-1).days + self.seconds_since_midnight)
235:           last_day = ::Time.days_in_month( self.month, self.year )
236:           change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
237:         end
end_of_quarter()

Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december)

This method is also aliased as at_end_of_quarter
     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 247
247:         def end_of_quarter
248:           beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
249:         end
end_of_week()

Returns a new Time representing the end of this week (Sunday, 23:59:59)

This method is also aliased as at_end_of_week
     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 199
199:         def end_of_week
200:           days_to_sunday = self.wday!=0 ? 7-self.wday : 0
201:           (self + days_to_sunday.days).end_of_day
202:         end
end_of_year()

Returns a new Time representing the end of the year (end of the 31st of december)

This method is also aliased as at_end_of_year
     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 259
259:         def end_of_year
260:           change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
261:         end
future?()

Tells whether the Time object‘s time lies in the future

    # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 73
73:         def future?
74:           self > ::Time.current
75:         end
in(seconds)

Alias for since

midnight()

Alias for beginning_of_day

minus_with_coercion(other)

Time#- can also be used to determine the number of seconds between two Time instances. We‘re layering on additional behavior so that ActiveSupport::TimeWithZone instances are coerced into values that Time#- will recognize

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 293
293:         def minus_with_coercion(other)
294:           other = other.comparable_time if other.respond_to?(:comparable_time)
295:           other.is_a?(::DateTime) ? to_f - other.to_f : minus_without_coercion(other)
296:         end
monday()

Alias for beginning_of_week

months_ago(months)

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

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 141
141:         def months_ago(months)
142:           advance(:months => -months)
143:         end
months_since(months)

Returns a new Time representing the time a number of specified months in the future

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 146
146:         def months_since(months)
147:           advance(:months => months)
148:         end
next_month()

Short-hand for months_since(1)

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 186
186:         def next_month
187:           months_since(1)
188:         end
next_week(day = :monday)

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

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

Short-hand for years_since(1)

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 171
171:         def next_year
172:           years_since(1)
173:         end
past?()

Tells whether the Time object‘s time lies in the past

    # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 63
63:         def past?
64:           self < ::Time.current
65:         end
prev_month()

Short-hand for months_ago(1)

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 181
181:         def prev_month
182:           months_ago(1)
183:         end
prev_year()

Short-hand for years_ago(1)

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 166
166:         def prev_year
167:           years_ago(1)
168:         end
seconds_since_midnight()
    # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 78
78:         def seconds_since_midnight
79:           self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
80:         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.

This method is also aliased as in
     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 126
126:         def since(seconds)
127:           f = seconds.since(self)
128:           if ActiveSupport::Duration === seconds
129:             f
130:           else
131:             initial_dst = self.dst? ? 1 : 0
132:             final_dst   = f.dst? ? 1 : 0
133:             (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
134:           end
135:         rescue
136:           self.to_datetime.since(seconds)
137:         end
today?()

Tells whether the Time object‘s time is today

    # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 68
68:         def today?
69:           self.to_date == ::Date.current
70:         end
tomorrow()

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

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 270
270:         def tomorrow
271:           advance(:days => 1)
272:         end
years_ago(years)

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

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 151
151:         def years_ago(years)
152:           advance(:years => -years)
153:         end
years_since(years)

Returns a new Time representing the time a number of specified years in the future

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 156
156:         def years_since(years)
157:           advance(:years => years)
158:         end
yesterday()

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

     # File activesupport/lib/active_support/core_ext/time/calculations.rb, line 265
265:         def yesterday
266:           advance(:days => -1)
267:         end