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: :years, :months, :weeks, :days, :hours, :minutes, :seconds.
# 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!
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 85 85: def ago(seconds) 86: self.since(-seconds) 87: 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 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)
# 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)
# 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)
# 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)
# 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.
# 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)
# 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)
# 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
Short-hand for months_ago(1)
# 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 months_since(1)
# 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).
# 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)
# 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
# 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!
# 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