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
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.
# 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!
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 58 58: def ago(seconds) 59: self.since(-seconds) 60: end
Returns a new Time representing the start of the day (0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 145 145: def beginning_of_day 146: (self - self.seconds_since_midnight).change(:usec => 0) 147: end
Returns a new Time representing the start of the month (1st of the month, 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 153 153: def beginning_of_month 154: #self - ((self.mday-1).days + self.seconds_since_midnight) 155: change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) 156: end
Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 168 168: def beginning_of_quarter 169: beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month }) 170: end
Returns a new Time representing the "start" of this week (Monday, 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 131 131: def beginning_of_week 132: days_to_monday = self.wday!=0 ? self.wday-1 : 6 133: (self - days_to_monday.days).midnight 134: end
Returns a new Time representing the start of the year (1st of january, 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 174 174: def beginning_of_year 175: change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) 176: 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.
# 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)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 160 160: def end_of_month 161: #self - ((self.mday-1).days + self.seconds_since_midnight) 162: last_day = ::Time.days_in_month( self.month, self.year ) 163: change(:mday => last_day,:hour => 0, :min => 0, :sec => 0, :usec => 0) 164: end
Short-hand for months_ago(1)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 121 121: def last_month 122: months_ago(1) 123: end
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 77 77: def months_since(months) 78: year, month, mday = self.year, self.month, self.mday 79: 80: month += months 81: 82: # in case months is negative 83: while month < 1 84: month += 12 85: year -= 1 86: end 87: 88: # in case months is positive 89: while month > 12 90: month -= 12 91: year += 1 92: end 93: 94: max = ::Time.days_in_month(month, year) 95: mday = max if mday > max 96: 97: change(:year => year, :month => month, :mday => mday) 98: end
Short-hand for months_since(1)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 126 126: def next_month 127: months_since(1) 128: end
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 139 139: def next_week(day = :monday) 140: days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} 141: since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) 142: end
Short-hand for years_since(1)
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 115 115: def next_year 116: years_since(1) 117: end
Seconds since midnight: Time.now.seconds_since_midnight
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 27 27: def seconds_since_midnight 28: self.to_i - self.change(:hour => 0).to_i + (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!
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 64 64: def since(seconds) 65: initial_dst = self.dst? ? 1 : 0 66: f = seconds.since(self) 67: final_dst = f.dst? ? 1 : 0 68: (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f 69: end
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 105 105: def years_since(years) 106: change(:year => self.year + years) 107: end