| Module | ActiveSupport::CoreExtensions::Date::Calculations |
| In: |
vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb
|
Enables the use of time calculations within Time itself
Provides precise Date calculations for years, months, and days. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days.
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 72
72: def advance(options)
73: d = self
74: d = d >> options.delete(:years) * 12 if options[:years]
75: d = d >> options.delete(:months) if options[:months]
76: d = d + options.delete(:weeks) * 7 if options[:weeks]
77: d = d + options.delete(:days) if options[:days]
78: d
79: end
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) and then subtracts the specified number of seconds
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 30
30: def ago(seconds)
31: to_time.since(-seconds)
32: end
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 42
42: def beginning_of_day
43: to_time
44: end
Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 152
152: def beginning_of_month
153: self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1)
154: end
Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 165
165: def beginning_of_quarter
166: beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
167: end
Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 136
136: def beginning_of_week
137: days_to_monday = self.wday!=0 ? self.wday-1 : 6
138: result = self - days_to_monday
139: self.acts_like?(:time) ? result.midnight : result
140: end
Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 171
171: def beginning_of_year
172: self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1)
173: end
Returns a new Date where one or more of the elements have been changed according to the options parameter.
Examples:
Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1) Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 87
87: def change(options)
88: ::Date.new(
89: options[:year] || self.year,
90: options[:month] || self.month,
91: options[:day] || self.day
92: )
93: end
Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 50
50: def end_of_day
51: to_time.end_of_day
52: end
Returns a new Date/DateTime representing the end of the month (last day of the month; DateTime objects will have time set to 0:00)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 158
158: def end_of_month
159: last_day = ::Time.days_in_month( self.month, self.year )
160: self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
161: end
Short-hand for months_ago(1)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 126
126: def last_month
127: months_ago(1)
128: end
Short-hand for months_since(1)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 131
131: def next_month
132: months_since(1)
133: end
Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 145
145: def next_week(day = :monday)
146: days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
147: result = (self + 7).beginning_of_week + days_into_week[day]
148: self.acts_like?(:time) ? result.change(:hour => 0) : result
149: end
Short-hand for years_since(1)
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 121
121: def next_year
122: years_since(1)
123: end
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) and then adds the specified number of seconds
# File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 36
36: def since(seconds)
37: to_time.since(seconds)
38: end