Module: ActiveSupport::CoreExtensions::Time::Calculations

Enables the use of time calculations within Time itself

Child modules and classes

Module ActiveSupport::CoreExtensions::Time::Calculations::ClassMethods

Constants

NameValue
COMMON_YEAR_DAYS_IN_MONTH [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Public Class Methods


included (base)

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 6
 6:         def self.included(base) #nodoc
 7:           base.extend ClassMethods
 8: 
 9:           base.class_eval do
10:             alias_method :plus_without_duration, :+
11:             alias_method :+, :plus_with_duration
12:             
13:             alias_method :minus_without_duration, :-
14:             alias_method :-, :minus_with_duration
15:             
16:             alias_method :minus_without_coercion, :-
17:             alias_method :-, :minus_with_coercion
18:             
19:             alias_method :compare_without_coercion, :<=>
20:             alias_method :<=>, :compare_with_coercion
21:           end
22:         end

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 vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 83
83:         def advance(options)
84:           d = to_date.advance(options)
85:           time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
86:           seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
87:           seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
88:         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 92
92:         def ago(seconds)
93:           self.since(-seconds)
94:         end











beginning_of_day ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 171
171:         def beginning_of_day
172:           (self - self.seconds_since_midnight).change(:usec => 0)
173:         end

beginning_of_month ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 184
184:         def beginning_of_month
185:           #self - ((self.mday-1).days + self.seconds_since_midnight)
186:           change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
187:         end

beginning_of_quarter ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 199
199:         def beginning_of_quarter
200:           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
201:         end

beginning_of_week ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 150
150:         def beginning_of_week
151:           days_to_monday = self.wday!=0 ? self.wday-1 : 6
152:           (self - days_to_monday.days).midnight
153:         end

beginning_of_year ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 211
211:         def beginning_of_year
212:           change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
213:         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 68
68:         def change(options)
69:           ::Time.send(
70:             self.utc? ? :utc_time : :local_time,
71:             options[:year]  || self.year,
72:             options[:month] || self.month,
73:             options[:day]   || self.day,
74:             options[:hour]  || self.hour,
75:             options[:min]   || (options[:hour] ? 0 : self.min),
76:             options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
77:             options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
78:           )
79:         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 vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 258
258:         def compare_with_coercion(other)
259:           # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparision
260:           other = other.comparable_time if other.respond_to?(:comparable_time)
261:           if other.acts_like?(:date)
262:             # other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=>
263:             to_datetime.compare_without_coercion(other)
264:           else
265:             compare_without_coercion(other)
266:           end
267:         end

end_of_day ()

Returns a new Time representing the end of the day (23:59:59)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 179
179:         def end_of_day
180:           change(:hour => 23, :min => 59, :sec => 59)
181:         end

end_of_month ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 191
191:         def end_of_month
192:           #self - ((self.mday-1).days + self.seconds_since_midnight)
193:           last_day = ::Time.days_in_month( self.month, self.year )
194:           change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0)
195:         end

end_of_quarter ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 205
205:         def end_of_quarter
206:           change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
207:         end

end_of_week ()

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 158
158:         def end_of_week
159:           days_to_sunday = self.wday!=0 ? 7-self.wday : 0
160:           (self + days_to_sunday.days).end_of_day
161:         end

end_of_year ()

Returns a new Time representing the end of the year (31st of december, 23:59:59)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 217
217:         def end_of_year
218:           change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59)
219:         end


last_month ()

Short-hand for months_ago(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 140
140:         def last_month
141:           months_ago(1)
142:         end

last_year ()

Short-hand for years_ago(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 129
129:         def last_year
130:           years_ago(1)
131:         end


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 vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 251
251:         def minus_with_coercion(other)
252:           other = other.comparable_time if other.respond_to?(:comparable_time)
253:           minus_without_coercion(other)
254:         end

minus_with_duration (other)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 240
240:         def minus_with_duration(other) #nodoc
241:           if ActiveSupport::Duration === other
242:             other.until(self)
243:           else
244:             minus_without_duration(other)
245:           end
246:         end


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 109
109:         def months_ago(months)
110:           advance(:months => -months)
111:         end

months_since (months)

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 114
114:         def months_since(months)
115:           advance(:months => months)
116:         end

next_month ()

Short-hand for months_since(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 145
145:         def next_month
146:           months_since(1)
147:         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 165
165:         def next_week(day = :monday)
166:           days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
167:           since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
168:         end

next_year ()

Short-hand for years_since(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 134
134:         def next_year
135:           years_since(1)
136:         end

plus_with_duration (other)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 232
232:         def plus_with_duration(other) #nodoc
233:           if ActiveSupport::Duration === other
234:             other.since(self)
235:           else
236:             plus_without_duration(other)
237:           end
238:         end

seconds_since_midnight ()

Seconds since midnight: Time.now.seconds_since_midnight

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 61
61:         def seconds_since_midnight
62:           self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
63:         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!

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 98
 98:         def since(seconds)
 99:           initial_dst = self.dst? ? 1 : 0
100:           f = seconds.since(self)
101:           final_dst   = f.dst? ? 1 : 0
102:           (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
103:         rescue
104:           self.to_datetime.since(seconds)          
105:         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 228
228:         def tomorrow
229:           self.since(1.day)
230:         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 119
119:         def years_ago(years)
120:           advance(:years => -years)
121:         end

years_since (years)

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

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 124
124:         def years_since(years)
125:           advance(:years => years)
126:         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 223
223:         def yesterday
224:           self.ago(1.day)
225:         end