Module: DateAndTime::Calculations
Constant Summary collapse
- DAYS_INTO_WEEK =
{ monday: 0, tuesday: 1, wednesday: 2, thursday: 3, friday: 4, saturday: 5, sunday: 6 }
- WEEKEND_DAYS =
[ 6, 0 ]
Instance Method Summary collapse
-
#all_day ⇒ Object
Returns a Range representing the whole day of the current date/time.
-
#all_month ⇒ Object
Returns a Range representing the whole month of the current date/time.
-
#all_quarter ⇒ Object
Returns a Range representing the whole quarter of the current date/time.
-
#all_week(start_day = Date.beginning_of_week) ⇒ Object
Returns a Range representing the whole week of the current date/time.
-
#all_year ⇒ Object
Returns a Range representing the whole year of the current date/time.
-
#beginning_of_month ⇒ Object
(also: #at_beginning_of_month)
Returns a new date/time at the start of the month.
-
#beginning_of_quarter ⇒ Object
(also: #at_beginning_of_quarter)
Returns a new date/time at the start of the quarter.
-
#beginning_of_week(start_day = Date.beginning_of_week) ⇒ Object
(also: #at_beginning_of_week)
Returns a new date/time representing the start of this week on the given day.
-
#beginning_of_year ⇒ Object
(also: #at_beginning_of_year)
Returns a new date/time at the beginning of the year.
-
#days_ago(days) ⇒ Object
Returns a new date/time the specified number of days ago.
-
#days_since(days) ⇒ Object
Returns a new date/time the specified number of days in the future.
-
#days_to_week_start(start_day = Date.beginning_of_week) ⇒ Object
Returns the number of days to the start of the week on the given day.
-
#end_of_month ⇒ Object
(also: #at_end_of_month)
Returns a new date/time representing the end of the month.
-
#end_of_quarter ⇒ Object
(also: #at_end_of_quarter)
Returns a new date/time at the end of the quarter.
-
#end_of_week(start_day = Date.beginning_of_week) ⇒ Object
(also: #at_end_of_week)
Returns a new date/time representing the end of this week on the given day.
-
#end_of_year ⇒ Object
(also: #at_end_of_year)
Returns a new date/time representing the end of the year.
-
#future? ⇒ Boolean
Returns true if the date/time is in the future.
-
#monday ⇒ Object
Returns Monday of this week assuming that week starts on Monday.
-
#months_ago(months) ⇒ Object
Returns a new date/time the specified number of months ago.
-
#months_since(months) ⇒ Object
Returns a new date/time the specified number of months in the future.
-
#next_day ⇒ Object
Returns a new date/time representing the next day.
-
#next_month ⇒ Object
Short-hand for months_since(1).
-
#next_quarter ⇒ Object
Short-hand for months_since(3).
-
#next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false) ⇒ Object
Returns a new date/time representing the given day in the next week.
-
#next_weekday ⇒ Object
Returns a new date/time representing the next weekday.
-
#next_year ⇒ Object
Short-hand for years_since(1).
-
#on_weekday? ⇒ Boolean
Returns true if the date/time does not fall on a Saturday or Sunday.
-
#on_weekend? ⇒ Boolean
Returns true if the date/time falls on a Saturday or Sunday.
-
#past? ⇒ Boolean
Returns true if the date/time is in the past.
-
#prev_day ⇒ Object
Returns a new date/time representing the previous day.
-
#prev_month ⇒ Object
(also: #last_month)
Short-hand for months_ago(1).
-
#prev_quarter ⇒ Object
(also: #last_quarter)
Short-hand for months_ago(3).
-
#prev_week(start_day = Date.beginning_of_week, same_time: false) ⇒ Object
(also: #last_week)
Returns a new date/time representing the given day in the previous week.
-
#prev_weekday ⇒ Object
(also: #last_weekday)
Returns a new date/time representing the previous weekday.
-
#prev_year ⇒ Object
(also: #last_year)
Short-hand for years_ago(1).
-
#sunday ⇒ Object
Returns Sunday of this week assuming that week starts on Monday.
-
#today? ⇒ Boolean
Returns true if the date/time is today.
-
#tomorrow ⇒ Object
Returns a new date/time representing tomorrow.
-
#weeks_ago(weeks) ⇒ Object
Returns a new date/time the specified number of weeks ago.
-
#weeks_since(weeks) ⇒ Object
Returns a new date/time the specified number of weeks in the future.
-
#years_ago(years) ⇒ Object
Returns a new date/time the specified number of years ago.
-
#years_since(years) ⇒ Object
Returns a new date/time the specified number of years in the future.
-
#yesterday ⇒ Object
Returns a new date/time representing yesterday.
Instance Method Details
#all_day ⇒ Object
Returns a Range representing the whole day of the current date/time.
298 299 300 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 298 def all_day beginning_of_day..end_of_day end |
#all_month ⇒ Object
Returns a Range representing the whole month of the current date/time.
309 310 311 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 309 def all_month beginning_of_month..end_of_month end |
#all_quarter ⇒ Object
Returns a Range representing the whole quarter of the current date/time.
314 315 316 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 314 def all_quarter beginning_of_quarter..end_of_quarter end |
#all_week(start_day = Date.beginning_of_week) ⇒ Object
Returns a Range representing the whole week of the current date/time. Week starts on start_day, default is Date.beginning_of_week
or config.beginning_of_week
when set.
304 305 306 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 304 def all_week(start_day = Date.beginning_of_week) beginning_of_week(start_day)..end_of_week(start_day) end |
#all_year ⇒ Object
Returns a Range representing the whole year of the current date/time.
319 320 321 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 319 def all_year beginning_of_year..end_of_year end |
#beginning_of_month ⇒ Object Also known as: at_beginning_of_month
Returns a new date/time at the start of the month.
today = Date.today # => Thu, 18 Jun 2015
today.beginning_of_month # => Mon, 01 Jun 2015
DateTime
objects will have a time set to 0:00.
now = DateTime.current # => Thu, 18 Jun 2015 15:23:13 +0000
now.beginning_of_month # => Mon, 01 Jun 2015 00:00:00 +0000
110 111 112 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 110 def beginning_of_month first_hour(change(day: 1)) end |
#beginning_of_quarter ⇒ Object Also known as: at_beginning_of_quarter
Returns a new date/time at the start of the quarter.
today = Date.today # => Fri, 10 Jul 2015
today.beginning_of_quarter # => Wed, 01 Jul 2015
DateTime
objects will have a time set to 0:00.
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.beginning_of_quarter # => Wed, 01 Jul 2015 00:00:00 +0000
124 125 126 127 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 124 def beginning_of_quarter first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month } beginning_of_month.change(month: first_quarter_month) end |
#beginning_of_week(start_day = Date.beginning_of_week) ⇒ Object Also known as: at_beginning_of_week
Returns a new date/time representing the start of this week on the given day. Week is assumed to start on start_day
, default is Date.beginning_of_week
or config.beginning_of_week
when set. DateTime
objects have their time set to 0:00.
255 256 257 258 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 255 def beginning_of_week(start_day = Date.beginning_of_week) result = days_ago(days_to_week_start(start_day)) acts_like?(:time) ? result.midnight : result end |
#beginning_of_year ⇒ Object Also known as: at_beginning_of_year
Returns a new date/time at the beginning of the year.
today = Date.today # => Fri, 10 Jul 2015
today.beginning_of_year # => Thu, 01 Jan 2015
DateTime
objects will have a time set to 0:00.
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.beginning_of_year # => Thu, 01 Jan 2015 00:00:00 +0000
154 155 156 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 154 def beginning_of_year change(month: 1).beginning_of_month end |
#days_ago(days) ⇒ Object
Returns a new date/time the specified number of days ago.
62 63 64 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 62 def days_ago(days) advance(days: -days) end |
#days_since(days) ⇒ Object
Returns a new date/time the specified number of days in the future.
67 68 69 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 67 def days_since(days) advance(days: days) end |
#days_to_week_start(start_day = Date.beginning_of_week) ⇒ Object
Returns the number of days to the start of the week on the given day. Week is assumed to start on start_day
, default is Date.beginning_of_week
or config.beginning_of_week
when set.
245 246 247 248 249 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 245 def days_to_week_start(start_day = Date.beginning_of_week) start_day_number = DAYS_INTO_WEEK[start_day] current_day_number = wday != 0 ? wday - 1 : 6 (current_day_number - start_day_number) % 7 end |
#end_of_month ⇒ Object Also known as: at_end_of_month
Returns a new date/time representing the end of the month. DateTime objects will have a time set to 23:59:59.
284 285 286 287 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 284 def end_of_month last_day = ::Time.days_in_month(month, year) last_hour(days_since(last_day - day)) end |
#end_of_quarter ⇒ Object Also known as: at_end_of_quarter
Returns a new date/time at the end of the quarter.
today = Date.today # => Fri, 10 Jul 2015
today.end_of_quarter # => Wed, 30 Sep 2015
DateTime
objects will have a time set to 23:59:59.
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.end_of_quarter # => Wed, 30 Sep 2015 23:59:59 +0000
139 140 141 142 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 139 def end_of_quarter last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month } beginning_of_month.change(month: last_quarter_month).end_of_month end |
#end_of_week(start_day = Date.beginning_of_week) ⇒ Object Also known as: at_end_of_week
Returns a new date/time representing the end of this week on the given day. Week is assumed to start on start_day
, default is Date.beginning_of_week
or config.beginning_of_week
when set. DateTime objects have their time set to 23:59:59.
271 272 273 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 271 def end_of_week(start_day = Date.beginning_of_week) last_hour(days_since(6 - days_to_week_start(start_day))) end |
#end_of_year ⇒ Object Also known as: at_end_of_year
Returns a new date/time representing the end of the year. DateTime objects will have a time set to 23:59:59.
292 293 294 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 292 def end_of_year change(month: 12).end_of_month end |
#future? ⇒ Boolean
Returns true if the date/time is in the future.
47 48 49 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 47 def future? self > self.class.current end |
#monday ⇒ Object
Returns Monday of this week assuming that week starts on Monday. DateTime
objects have their time set to 0:00.
263 264 265 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 263 def monday beginning_of_week(:monday) end |
#months_ago(months) ⇒ Object
Returns a new date/time the specified number of months ago.
82 83 84 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 82 def months_ago(months) advance(months: -months) end |
#months_since(months) ⇒ Object
Returns a new date/time the specified number of months in the future.
87 88 89 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 87 def months_since(months) advance(months: months) end |
#next_day ⇒ Object
Returns a new date/time representing the next day.
32 33 34 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 32 def next_day advance(days: 1) end |
#next_month ⇒ Object
Short-hand for months_since(1).
190 191 192 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 190 def next_month months_since(1) end |
#next_quarter ⇒ Object
Short-hand for months_since(3)
195 196 197 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 195 def next_quarter months_since(3) end |
#next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false) ⇒ Object
Returns a new date/time representing the given day in the next week.
today = Date.today # => Thu, 07 May 2015
today.next_week # => Mon, 11 May 2015
The given_day_in_next_week
defaults to the beginning of the week which is determined by Date.beginning_of_week
or config.beginning_of_week
when set.
today = Date.today # => Thu, 07 May 2015
today.next_week(:friday) # => Fri, 15 May 2015
DateTime
objects have their time set to 0:00 unless same_time
is true.
now = DateTime.current # => Thu, 07 May 2015 13:31:16 +0000
now.next_week # => Mon, 11 May 2015 00:00:00 +0000
175 176 177 178 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 175 def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false) result = first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week))) same_time ? copy_time_to(result) : result end |
#next_weekday ⇒ Object
Returns a new date/time representing the next weekday.
181 182 183 184 185 186 187 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 181 def next_weekday if next_day.on_weekend? next_week(:monday, same_time: true) else next_day end end |
#next_year ⇒ Object
Short-hand for years_since(1).
200 201 202 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 200 def next_year years_since(1) end |
#on_weekday? ⇒ Boolean
Returns true if the date/time does not fall on a Saturday or Sunday.
57 58 59 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 57 def on_weekday? !WEEKEND_DAYS.include?(wday) end |
#on_weekend? ⇒ Boolean
Returns true if the date/time falls on a Saturday or Sunday.
52 53 54 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 52 def on_weekend? WEEKEND_DAYS.include?(wday) end |
#past? ⇒ Boolean
Returns true if the date/time is in the past.
42 43 44 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 42 def past? self < self.class.current end |
#prev_day ⇒ Object
Returns a new date/time representing the previous day.
22 23 24 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 22 def prev_day advance(days: -1) end |
#prev_month ⇒ Object Also known as: last_month
Short-hand for months_ago(1).
225 226 227 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 225 def prev_month months_ago(1) end |
#prev_quarter ⇒ Object Also known as: last_quarter
Short-hand for months_ago(3).
231 232 233 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 231 def prev_quarter months_ago(3) end |
#prev_week(start_day = Date.beginning_of_week, same_time: false) ⇒ Object Also known as: last_week
Returns a new date/time representing the given day in the previous week. Week is assumed to start on start_day
, default is Date.beginning_of_week
or config.beginning_of_week
when set. DateTime objects have their time set to 0:00 unless same_time
is true.
208 209 210 211 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 208 def prev_week(start_day = Date.beginning_of_week, same_time: false) result = first_hour(weeks_ago(1).beginning_of_week.days_since(days_span(start_day))) same_time ? copy_time_to(result) : result end |
#prev_weekday ⇒ Object Also known as: last_weekday
Returns a new date/time representing the previous weekday.
215 216 217 218 219 220 221 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 215 def prev_weekday if prev_day.on_weekend? copy_time_to(beginning_of_week(:friday)) else prev_day end end |
#prev_year ⇒ Object Also known as: last_year
Short-hand for years_ago(1).
237 238 239 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 237 def prev_year years_ago(1) end |
#sunday ⇒ Object
Returns Sunday of this week assuming that week starts on Monday. DateTime
objects have their time set to 23:59:59.
278 279 280 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 278 def sunday end_of_week(:monday) end |
#today? ⇒ Boolean
Returns true if the date/time is today.
37 38 39 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 37 def today? to_date == ::Date.current end |
#tomorrow ⇒ Object
Returns a new date/time representing tomorrow.
27 28 29 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 27 def tomorrow advance(days: 1) end |
#weeks_ago(weeks) ⇒ Object
Returns a new date/time the specified number of weeks ago.
72 73 74 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 72 def weeks_ago(weeks) advance(weeks: -weeks) end |
#weeks_since(weeks) ⇒ Object
Returns a new date/time the specified number of weeks in the future.
77 78 79 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 77 def weeks_since(weeks) advance(weeks: weeks) end |
#years_ago(years) ⇒ Object
Returns a new date/time the specified number of years ago.
92 93 94 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 92 def years_ago(years) advance(years: -years) end |
#years_since(years) ⇒ Object
Returns a new date/time the specified number of years in the future.
97 98 99 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 97 def years_since(years) advance(years: years) end |
#yesterday ⇒ Object
Returns a new date/time representing yesterday.
17 18 19 |
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 17 def yesterday advance(days: -1) end |