Enables the use of time calculations within Time itself
- advance
- ago
- at_beginning_of_day
- at_beginning_of_month
- at_beginning_of_quarter
- at_beginning_of_week
- at_beginning_of_year
- at_end_of_month
- at_midnight
- beginning_of_day
- beginning_of_month
- beginning_of_quarter
- beginning_of_week
- beginning_of_year
- change
- end_of_month
- in
- last_month
- last_year
- midnight
- monday
- months_ago
- months_since
- next_month
- next_week
- next_year
- seconds_since_midnight
- since
- tomorrow
- years_ago
- years_since
- yesterday
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.
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 50 50: def advance(options) 51: d = ::Date.new(year + (options.delete(:years) || 0), month, day) 52: d = d >> options.delete(:months) if options[:months] 53: d = d + options.delete(:days) if options[:days] 54: change(options.merge(:year => d.year, :month => d.month, :mday => d.day)) 55: 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!
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 59 59: def ago(seconds) 60: seconds.until(self) 61: end
Alias for beginning_of_day
Alias for beginning_of_month
Alias for beginning_of_quarter
Alias for beginning_of_week
Alias for beginning_of_year
Alias for end_of_month
Alias for beginning_of_day
Returns a new Time representing the start of the day (0:00)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 143 143: def beginning_of_day 144: (self - self.seconds_since_midnight).change(:usec => 0) 145: end
Returns a new Time representing the start of the month (1st of the month, 0:00)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 151 151: def beginning_of_month 152: #self - ((self.mday-1).days + self.seconds_since_midnight) 153: change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) 154: end
Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 166 166: def beginning_of_quarter 167: beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month }) 168: end
Returns a new Time representing the "start" of this week (Monday, 0:00)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 129 129: def beginning_of_week 130: days_to_monday = self.wday!=0 ? self.wday-1 : 6 131: (self - days_to_monday.days).midnight 132: end
Returns a new Time representing the start of the year (1st of january, 0:00)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 172 172: def beginning_of_year 173: change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) 174: 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.
[ show source ]
# 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
Returns a new Time representing the end of the month (last day of the month, 0:00)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 158 158: def end_of_month 159: #self - ((self.mday-1).days + self.seconds_since_midnight) 160: last_day = ::Time.days_in_month( self.month, self.year ) 161: change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0) 162: end
Alias for since
Short-hand for months_ago(1)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 119 119: def last_month 120: months_ago(1) 121: end
Short-hand for years_ago(1)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 108 108: def last_year 109: years_ago(1) 110: end
Alias for beginning_of_day
Alias for beginning_of_week
Returns a new Time representing the time a number of specified months ago
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 71 71: def months_ago(months) 72: months_since(-months) 73: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 75 75: def months_since(months) 76: year, month, mday = self.year, self.month, self.mday 77: 78: month += months 79: 80: # in case months is negative 81: while month < 1 82: month += 12 83: year -= 1 84: end 85: 86: # in case months is positive 87: while month > 12 88: month -= 12 89: year += 1 90: end 91: 92: max = ::Time.days_in_month(month, year) 93: mday = max if mday > max 94: 95: change(:year => year, :month => month, :mday => mday) 96: end
Short-hand for months_since(1)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 124 124: def next_month 125: months_since(1) 126: end
Returns a new Time representing the start of the given day in next week (default is Monday).
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 137 137: def next_week(day = :monday) 138: days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} 139: since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) 140: end
Short-hand for years_since(1)
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 113 113: def next_year 114: years_since(1) 115: end
Seconds since midnight: Time.now.seconds_since_midnight
[ show source ]
# 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
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!
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 65 65: def since(seconds) 66: seconds.since(self) 67: end
Convenience method which returns a new Time representing the time 1 day since the instance time
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 183 183: def tomorrow 184: self.since(1.day) 185: end
Returns a new Time representing the time a number of specified years ago
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 99 99: def years_ago(years) 100: change(:year => self.year - years) 101: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 103 103: def years_since(years) 104: change(:year => self.year + years) 105: end
Convenience method which returns a new Time representing the time 1 day ago
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 178 178: def yesterday 179: self.ago(1.day) 180: end