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: :years, :months, :weeks, :days, :hours, :minutes, :seconds.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 76
76:         def advance(options)
77:           d = to_date.advance(options)
78:           time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
79:           seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
80:           seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
81:         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 85
85:         def ago(seconds)
86:           self.since(-seconds)
87:         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 157
157:         def beginning_of_day
158:           (self - self.seconds_since_midnight).change(:usec => 0)
159:         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 170
170:         def beginning_of_month
171:           #self - ((self.mday-1).days + self.seconds_since_midnight)
172:           change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
173:         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 185
185:         def beginning_of_quarter
186:           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
187:         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 143
143:         def beginning_of_week
144:           days_to_monday = self.wday!=0 ? self.wday-1 : 6
145:           (self - days_to_monday.days).midnight
146:         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 191
191:         def beginning_of_year
192:           change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
193:         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 61
61:         def change(options)
62:           ::Time.send(
63:             self.utc? ? :utc_time : :local_time,
64:             options[:year]  || self.year,
65:             options[:month] || self.month,
66:             options[:day]   || self.day,
67:             options[:hour]  || self.hour,
68:             options[:min]   || (options[:hour] ? 0 : self.min),
69:             options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
70:             options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
71:           )
72:         end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 165
165:         def end_of_day
166:           change(:hour => 23, :min => 59, :sec => 59)
167:         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 177
177:         def end_of_month
178:           #self - ((self.mday-1).days + self.seconds_since_midnight)
179:           last_day = ::Time.days_in_month( self.month, self.year )
180:           change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0)
181:         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 133
133:         def last_month
134:           months_ago(1)
135:         end

Short-hand for years_ago(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 122
122:         def last_year
123:           years_ago(1)
124:         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 102
102:         def months_ago(months)
103:           advance(:months => -months)
104:         end

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

[Source]

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

Short-hand for months_since(1)

[Source]

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

Short-hand for years_since(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 127
127:         def next_year
128:           years_since(1)
129:         end

Seconds since midnight: Time.now.seconds_since_midnight

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 54
54:         def seconds_since_midnight
55:           self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
56:         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 91
91:         def since(seconds)
92:           initial_dst = self.dst? ? 1 : 0
93:           f = seconds.since(self)
94:           final_dst   = f.dst? ? 1 : 0
95:           (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
96:         rescue
97:           self.to_datetime.since(seconds)          
98:         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 202
202:         def tomorrow
203:           self.since(1.day)
204:         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 112
112:         def years_ago(years)
113:           advance(:years => -years)
114:         end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 117
117:         def years_since(years)
118:           advance(:years => years)
119:         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 197
197:         def yesterday
198:           self.ago(1.day)
199:         end

[Validate]