Module ActiveSupport::CoreExtensions::Time::Calculations
In: vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb

Enables the use of time calculations within Time itself

Methods

Classes and Modules

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

Public Instance methods

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.

[Source]

    # 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

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!

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 58
58:         def ago(seconds)
59:           seconds.until(self)
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

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

[Source]

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

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

[Source]

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

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

[Source]

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

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

[Source]

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

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 171
171:         def beginning_of_year
172:           change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
173:         end

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.

[Source]

    # 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

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 157
157:         def end_of_month
158:           #self - ((self.mday-1).days + self.seconds_since_midnight)
159:           last_day = ::Time.days_in_month( self.month, self.year )
160:           change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0)
161:         end
in(seconds)

Alias for since

Short-hand for months_ago(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 118
118:         def last_month
119:           months_ago(1)
120:         end

Short-hand for years_ago(1)

[Source]

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

Alias for beginning_of_day

monday()

Alias for beginning_of_week

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 70
70:         def months_ago(months)
71:           months_since(-months)
72:         end

[Source]

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

Short-hand for months_since(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 123
123:         def next_month
124:           months_since(1)
125:         end

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

[Source]

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

Short-hand for years_since(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 112
112:         def next_year
113:           years_since(1)
114:         end

Seconds since midnight: Time.now.seconds_since_midnight

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 27
27:         def seconds_since_midnight
28:           self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6)
29:         end
 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!

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 64
64:         def since(seconds)
65:           seconds.since(self)
66:         end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 182
182:         def tomorrow
183:           self.since(1.day)
184:         end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 98
 98:         def years_ago(years)
 99:           change(:year => self.year - years)
100:         end

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 102
102:         def years_since(years)
103:           change(:year => self.year + years)
104:         end

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

[Source]

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

[Validate]