Enables the use of time calculations within Time itself

Methods
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 29
29:         def ago(seconds)
30:           seconds.until(self)
31:         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_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 101
101:         def beginning_of_day
102:           self - self.seconds_since_midnight
103:         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 109
109:         def beginning_of_month
110:           #self - ((self.mday-1).days + self.seconds_since_midnight)
111:           change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
112:         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 88
88:         def beginning_of_week
89:           (self - self.wday.days).midnight + 1.day
90:         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 116
116:         def beginning_of_year
117:           change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
118:         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 14
14:         def change(options)
15:           ::Time.send(
16:             self.utc? ? :utc : :local, 
17:             options[:year]  || self.year, 
18:             options[:month] || self.month, 
19:             options[:mday]  || self.mday, 
20:             options[:hour]  || self.hour, 
21:             options[:min]   || (options[:hour] ? 0 : self.min),
22:             options[:sec]   || ((options[:hour] || options[:min]) ? 0 : self.sec),
23:             options[:usec]  || ((options[:hour] || options[:min] || options[:usec]) ? 0 : self.usec)
24:           )
25:         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 78
78:         def last_month
79:           months_ago(1)
80:         end
last_year()

Short-hand for months_ago(1)

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 67
67:         def last_year
68:           years_since(1)
69:         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 41
41:         def months_ago(months)
42:           if months >= self.month 
43:             change(:year => self.year - 1, :month => 12).months_ago(months - self.month)
44:           else
45:             change(:year => self.year, :month => self.month - months)
46:           end
47:         end
months_since(months)
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 49
49:         def months_since(months)
50:           if months + self.month > 12
51:             change(:year => self.year + 1, :month => 1).months_since(months - (self.month == 1 ? 12 : (self.month + 1)))
52:           else
53:             change(:year => self.year, :month => self.month + months)
54:           end
55:         end
next_month()

Short-hand for months_since(1)

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

Short-hand for months_since(1)

    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 72
72:         def next_year
73:           years_since(1)
74:         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 7
7:         def seconds_since_midnight
8:           self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6)
9:         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 35
35:         def since(seconds)
36:           seconds.since(self)
37:         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 127
127:         def tomorrow
128:           self.since(1.day)
129:         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 58
58:         def years_ago(years)
59:           change(:year => self.year - years)
60:         end
years_since(years)
    # File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 62
62:         def years_since(years)
63:           change(:year => self.year + years)
64:         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 122
122:         def yesterday
123:           self.ago(1.day)
124:         end