| 32 | | |
|---|
| 33 | | # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with |
|---|
| 34 | | # any of these keys: :months, :days, :years. |
|---|
| 35 | | def advance(options) |
|---|
| 36 | | d = ::Date.new(year + (options.delete(:years) || 0), month, day) |
|---|
| 37 | | d = d >> options.delete(:months) if options[:months] |
|---|
| 38 | | d = d + options.delete(:days) if options[:days] |
|---|
| 39 | | d |
|---|
| | 24 | end |
|---|
| | 25 | |
|---|
| | 26 | def plus_with_duration(other) #:nodoc: |
|---|
| | 27 | if ActiveSupport::Duration === other |
|---|
| | 28 | other.since(self) |
|---|
| | 29 | else |
|---|
| | 30 | plus_without_duration(other) |
|---|
| 41 | | |
|---|
| 42 | | # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter. |
|---|
| 43 | | # |
|---|
| 44 | | # Examples: |
|---|
| 45 | | # |
|---|
| 46 | | # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 12) |
|---|
| 47 | | # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12) |
|---|
| 48 | | def change(options) |
|---|
| 49 | | ::Date.new( |
|---|
| 50 | | options[:year] || self.year, |
|---|
| 51 | | options[:month] || self.month, |
|---|
| 52 | | options[:day] || options[:mday] || self.day # mday is deprecated |
|---|
| 53 | | ) |
|---|
| | 32 | end |
|---|
| | 33 | |
|---|
| | 34 | def minus_with_duration(other) #:nodoc: |
|---|
| | 35 | if ActiveSupport::Duration === other |
|---|
| | 36 | plus_with_duration(-other) |
|---|
| | 37 | else |
|---|
| | 38 | minus_without_duration(other) |
|---|
| 55 | | |
|---|
| 56 | | # Returns a new Date/DateTime representing the time a number of specified months ago |
|---|
| 57 | | def months_ago(months) |
|---|
| 58 | | months_since(-months) |
|---|
| 59 | | end |
|---|
| | 40 | end |
|---|
| | 41 | |
|---|
| | 42 | # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with |
|---|
| | 43 | # any of these keys: :months, :days, :years. |
|---|
| | 44 | def advance(options) |
|---|
| | 45 | d = ::Date.new(year + (options.delete(:years) || 0), month, day) |
|---|
| | 46 | d = d >> options.delete(:months) if options[:months] |
|---|
| | 47 | d = d + options.delete(:days) if options[:days] |
|---|
| | 48 | d |
|---|
| | 49 | end |
|---|
| 61 | | def months_since(months) |
|---|
| 62 | | year, month, day = self.year, self.month, self.day |
|---|
| | 51 | # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter. |
|---|
| | 52 | # |
|---|
| | 53 | # Examples: |
|---|
| | 54 | # |
|---|
| | 55 | # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 12) |
|---|
| | 56 | # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12) |
|---|
| | 57 | def change(options) |
|---|
| | 58 | ::Date.new( |
|---|
| | 59 | options[:year] || self.year, |
|---|
| | 60 | options[:month] || self.month, |
|---|
| | 61 | options[:day] || options[:mday] || self.day # mday is deprecated |
|---|
| | 62 | ) |
|---|
| | 63 | end |
|---|
| | 64 | |
|---|
| | 65 | # Returns a new Date/DateTime representing the time a number of specified months ago |
|---|
| | 66 | def months_ago(months) |
|---|
| | 67 | months_since(-months) |
|---|
| | 68 | end |
|---|
| 122 | | # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). |
|---|
| 123 | | def next_week(day = :monday) |
|---|
| 124 | | days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} |
|---|
| 125 | | result = (self + 7).beginning_of_week + days_into_week[day] |
|---|
| 126 | | self.acts_like?(:time) ? result.change(:hour => 0) : result |
|---|
| 127 | | end |
|---|
| | 112 | # Short-hand for months_ago(1) |
|---|
| | 113 | def last_month |
|---|
| | 114 | months_ago(1) |
|---|
| | 115 | end |
|---|
| 135 | | # 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) |
|---|
| 136 | | def end_of_month |
|---|
| 137 | | last_day = ::Time.days_in_month( self.month, self.year ) |
|---|
| 138 | | self.acts_like?(:time) ? change(:day => last_day,:hour => 0, :min => 0, :sec => 0) : change(:day => last_day) |
|---|
| 139 | | end |
|---|
| 140 | | alias :at_end_of_month :end_of_month |
|---|
| | 122 | # Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00) |
|---|
| | 123 | def beginning_of_week |
|---|
| | 124 | days_to_monday = self.wday!=0 ? self.wday-1 : 6 |
|---|
| | 125 | result = self - days_to_monday |
|---|
| | 126 | self.acts_like?(:time) ? result.midnight : result |
|---|
| | 127 | end |
|---|
| | 128 | alias :monday :beginning_of_week |
|---|
| | 129 | alias :at_beginning_of_week :beginning_of_week |
|---|
| 142 | | # 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) |
|---|
| 143 | | def beginning_of_quarter |
|---|
| 144 | | beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month }) |
|---|
| 145 | | end |
|---|
| 146 | | alias :at_beginning_of_quarter :beginning_of_quarter |
|---|
| | 131 | # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). |
|---|
| | 132 | def next_week(day = :monday) |
|---|
| | 133 | days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} |
|---|
| | 134 | result = (self + 7).beginning_of_week + days_into_week[day] |
|---|
| | 135 | self.acts_like?(:time) ? result.change(:hour => 0) : result |
|---|
| | 136 | end |
|---|
| 148 | | # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00) |
|---|
| 149 | | def beginning_of_year |
|---|
| 150 | | self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) |
|---|
| 151 | | end |
|---|
| 152 | | alias :at_beginning_of_year :beginning_of_year |
|---|
| | 138 | # 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) |
|---|
| | 139 | def beginning_of_month |
|---|
| | 140 | self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1) |
|---|
| | 141 | end |
|---|
| | 142 | alias :at_beginning_of_month :beginning_of_month |
|---|
| | 155 | alias :at_beginning_of_quarter :beginning_of_quarter |
|---|
| | 156 | |
|---|
| | 157 | # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00) |
|---|
| | 158 | def beginning_of_year |
|---|
| | 159 | self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) |
|---|
| | 160 | end |
|---|
| | 161 | alias :at_beginning_of_year :beginning_of_year |
|---|
| | 162 | |
|---|
| | 163 | # Convenience method which returns a new Date/DateTime representing the time 1 day ago |
|---|
| | 164 | def yesterday |
|---|
| | 165 | self - 1 |
|---|
| | 166 | end |
|---|
| | 167 | |
|---|
| | 168 | # Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time |
|---|
| | 169 | def tomorrow |
|---|
| | 170 | self + 1 |
|---|
| | 171 | end |
|---|