Module | ActiveSupport::CoreExtensions::DateTime::Calculations |
In: |
vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb
|
Enables the use of time calculations within DateTime 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/date_time/calculations.rb, line 42 42: def advance(options) 43: d = to_date.advance(options) 44: datetime_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day) 45: seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600 46: seconds_to_advance == 0 ? datetime_advanced_by_date : datetime_advanced_by_date.since(seconds_to_advance) 47: end
Returns a new DateTime representing the start of the day (0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 63 63: def beginning_of_day 64: change(:hour => 0) 65: end
Returns a new DateTime where one or more of the elements have been changed according to the options parameter. The time options (hour, minute, sec) reset cascadingly, so if only the hour is passed, then minute and sec is set to 0. If the hour and minute is passed, then sec is set to 0.
# File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 27 27: def change(options) 28: ::DateTime.civil( 29: options[:year] || self.year, 30: options[:month] || self.month, 31: options[:day] || self.day, 32: options[:hour] || self.hour, 33: options[:min] || (options[:hour] ? 0 : self.min), 34: options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec), 35: options[:offset] || self.offset, 36: options[:start] || self.start 37: ) 38: end
Returns a new DateTime representing the end of the day (23:59:59)
# File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 71 71: def end_of_day 72: change(:hour => 23, :min => 59, :sec => 59) 73: end
Seconds since midnight: DateTime.now.seconds_since_midnight
# File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 20 20: def seconds_since_midnight 21: self.sec + (self.min * 60) + (self.hour * 3600) 22: end
Returns a new DateTime representing the time a number of seconds since the instance time Do not use this method in combination with x.months, use months_since instead!
# File vendor/rails/activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 57 57: def since(seconds) 58: self + Rational(seconds.round, 86400) 59: end