Enables the use of time calculations within Time itself

Methods
Classes and Modules
Module ActiveSupport::CoreExtensions::Time::Calculations::ClassMethods
Public Instance methods
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 50
50:         def ago(seconds)
51:           seconds.until(self)
52:         end
at_beginning_of_day()

Alias for beginning_of_day

at_beginning_of_month()

Alias for beginning_of_month

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 134
134:         def beginning_of_day
135:           (self - self.seconds_since_midnight).change(:usec => 0)
136:         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 142
142:         def beginning_of_month
143:           #self - ((self.mday-1).days + self.seconds_since_midnight)
144:           change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
145:         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 120
120:         def beginning_of_week
121:           days_to_monday = self.wday!=0 ? self.wday-1 : 6
122:           (self - days_to_monday.days).midnight
123:         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 157
157:         def beginning_of_year
158:           change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
159:         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 35
35:         def change(options)
36:           ::Time.send(
37:             self.utc? ? :utc : :local, 
38:             options[:year]  || self.year, 
39:             options[:month] || self.month, 
40:             options[:mday]  || self.mday, 
41:             options[:hour]  || self.hour, 
42:             options[:min]   || (options[:hour] ? 0 : self.min),
43:             options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
44:             options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
45:           )
46:         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 149
149:         def end_of_month
150:           #self - ((self.mday-1).days + self.seconds_since_midnight)
151:           last_day = ::Time.days_in_month( self.month, self.year )
152:           change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0)
153:         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 110
110:         def last_month
111:           months_ago(1)
112:         end
last_year()

Short-hand for years_ago(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 99
 99:         def last_year
100:           years_ago(1)
101:         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 62
62:         def months_ago(months)
63:           months_since(-months)
64:         end
months_since(months)
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 66
66:         def months_since(months)
67:           year, month, mday = self.year, self.month, self.mday
68: 
69:           month += months
70: 
71:           # in case months is negative
72:           while month < 1
73:             month += 12
74:             year -= 1
75:           end
76: 
77:           # in case months is positive
78:           while month > 12
79:             month -= 12
80:             year += 1
81:           end
82: 
83:           max = ::Time.days_in_month(month, year)
84:           mday = max if mday > max
85: 
86:           change(:year => year, :month => month, :mday => mday)
87:         end
next_month()

Short-hand for months_since(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 115
115:         def next_month
116:           months_since(1)
117:         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 128
128:         def next_week(day = :monday)
129:           days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
130:           since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
131:         end
next_year()

Short-hand for years_since(1)

     # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 104
104:         def next_year
105:           years_since(1)
106:         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 28
28:         def seconds_since_midnight
29:           self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6)
30:         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 56
56:         def since(seconds)
57:           seconds.since(self)
58:         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 168
168:         def tomorrow
169:           self.since(1.day)
170:         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 90
90:         def years_ago(years)
91:           change(:year => self.year - years)
92:         end
years_since(years)
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 94
94:         def years_since(years)
95:           change(:year => self.year + years)
96:         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 163
163:         def yesterday
164:           self.ago(1.day)
165:         end