Module: ActiveSupport::CoreExtensions::Date::Calculations
- Included in:
- Date
- Defined in:
- lib/active_support/core_ext/date/calculations.rb
Overview
Enables the use of time calculations within Time itself
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#advance(options) ⇒ Object
Provides precise Date calculations for years, months, and days.
-
#ago(seconds) ⇒ Object
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.
-
#beginning_of_day ⇒ Object
(also: #midnight, #at_midnight, #at_beginning_of_day)
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00).
-
#beginning_of_month ⇒ Object
(also: #at_beginning_of_month)
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).
-
#beginning_of_quarter ⇒ Object
(also: #at_beginning_of_quarter)
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).
-
#beginning_of_week ⇒ Object
(also: #monday, #at_beginning_of_week)
Returns a new Date/DateTime representing the “start” of this week (i.e, Monday; DateTime objects will have time set to 0:00).
-
#beginning_of_year ⇒ Object
(also: #at_beginning_of_year)
Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00).
-
#change(options) ⇒ Object
Returns a new Date where one or more of the elements have been changed according to the
options
parameter. -
#end_of_day ⇒ Object
Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59).
-
#end_of_month ⇒ Object
(also: #at_end_of_month)
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).
-
#last_month ⇒ Object
Short-hand for months_ago(1).
-
#last_year ⇒ Object
Short-hand for years_ago(1).
-
#minus_with_duration(other) ⇒ Object
:nodoc:.
-
#months_ago(months) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified months ago.
-
#months_since(months) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified months in the future.
-
#next_month ⇒ Object
Short-hand for months_since(1).
-
#next_week(day = :monday) ⇒ Object
Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
-
#next_year ⇒ Object
Short-hand for years_since(1).
-
#plus_with_duration(other) ⇒ Object
:nodoc:.
-
#since(seconds) ⇒ Object
(also: #in)
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.
-
#tomorrow ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time.
-
#years_ago(years) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified years ago.
-
#years_since(years) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified years in the future.
-
#yesterday ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day ago.
Class Method Details
.included(base) ⇒ Object
:nodoc:
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 6 def self.included(base) #:nodoc: base.extend ClassMethods base.instance_eval do alias_method :plus_without_duration, :+ alias_method :+, :plus_with_duration alias_method :minus_without_duration, :- alias_method :-, :minus_with_duration end end |
Instance Method Details
#advance(options) ⇒ Object
Provides precise Date calculations for years, months, and days. The options
parameter takes a hash with any of these keys: :years, :months, :weeks, :days.
72 73 74 75 76 77 78 79 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 72 def advance() d = self d = d >> .delete(:years) * 12 if [:years] d = d >> .delete(:months) if [:months] d = d + .delete(:weeks) * 7 if [:weeks] d = d + .delete(:days) if [:days] d end |
#ago(seconds) ⇒ Object
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
30 31 32 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 30 def ago(seconds) to_time.since(-seconds) end |
#beginning_of_day ⇒ Object Also known as: midnight, at_midnight, at_beginning_of_day
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
42 43 44 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 42 def beginning_of_day to_time end |
#beginning_of_month ⇒ Object Also known as: at_beginning_of_month
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)
152 153 154 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 152 def beginning_of_month self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1) end |
#beginning_of_quarter ⇒ Object Also known as: at_beginning_of_quarter
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)
165 166 167 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 165 def beginning_of_quarter beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month }) end |
#beginning_of_week ⇒ Object Also known as: monday, at_beginning_of_week
Returns a new Date/DateTime representing the “start” of this week (i.e, Monday; DateTime objects will have time set to 0:00)
136 137 138 139 140 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 136 def beginning_of_week days_to_monday = self.wday!=0 ? self.wday-1 : 6 result = self - days_to_monday self.acts_like?(:time) ? result.midnight : result end |
#beginning_of_year ⇒ Object Also known as: at_beginning_of_year
Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
171 172 173 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 171 def beginning_of_year self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1) end |
#change(options) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 87 def change() ::Date.new( [:year] || self.year, [:month] || self.month, [:day] || self.day ) end |
#end_of_day ⇒ Object
Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
50 51 52 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 50 def end_of_day to_time.end_of_day end |
#end_of_month ⇒ Object Also known as: at_end_of_month
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)
158 159 160 161 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 158 def end_of_month last_day = ::Time.days_in_month( self.month, self.year ) self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day) end |
#last_month ⇒ Object
Short-hand for months_ago(1)
126 127 128 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 126 def last_month months_ago(1) end |
#last_year ⇒ Object
Short-hand for years_ago(1)
116 117 118 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 116 def last_year years_ago(1) end |
#minus_with_duration(other) ⇒ Object
:nodoc:
62 63 64 65 66 67 68 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 62 def minus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other plus_with_duration(-other) else minus_without_duration(other) end end |
#months_ago(months) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified months ago
96 97 98 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 96 def months_ago(months) advance(:months => -months) end |
#months_since(months) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified months in the future
101 102 103 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 101 def months_since(months) advance(:months => months) end |
#next_month ⇒ Object
Short-hand for months_since(1)
131 132 133 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 131 def next_month months_since(1) end |
#next_week(day = :monday) ⇒ Object
Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
145 146 147 148 149 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 145 def next_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} result = (self + 7).beginning_of_week + days_into_week[day] self.acts_like?(:time) ? result.change(:hour => 0) : result end |
#next_year ⇒ Object
Short-hand for years_since(1)
121 122 123 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 121 def next_year years_since(1) end |
#plus_with_duration(other) ⇒ Object
:nodoc:
54 55 56 57 58 59 60 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 54 def plus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other other.since(self) else plus_without_duration(other) end end |
#since(seconds) ⇒ Object Also known as: in
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
36 37 38 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 36 def since(seconds) to_time.since(seconds) end |
#tomorrow ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
182 183 184 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 182 def tomorrow self + 1 end |
#years_ago(years) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified years ago
106 107 108 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 106 def years_ago(years) advance(:years => -years) end |
#years_since(years) ⇒ Object
Returns a new Date/DateTime representing the time a number of specified years in the future
111 112 113 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 111 def years_since(years) advance(:years => years) end |
#yesterday ⇒ Object
Convenience method which returns a new Date/DateTime representing the time 1 day ago
177 178 179 |
# File 'lib/active_support/core_ext/date/calculations.rb', line 177 def yesterday self - 1 end |