Module: DateAndTime::Calculations

Included in:
Date, Time
Defined in:
lib/active_support/core_ext/date_and_time/calculations.rb

Constant Summary collapse

DAYS_INTO_WEEK =
{
  sunday: 0,
  monday: 1,
  tuesday: 2,
  wednesday: 3,
  thursday: 4,
  friday: 5,
  saturday: 6
}
WEEKEND_DAYS =
[ 6, 0 ]

Instance Method Summary collapse

Instance Method Details

#after?(date_or_time) ⇒ Boolean

Returns true if the date/time falls after date_or_time.

Returns:

  • (Boolean)


59
60
61
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 59

def after?(date_or_time)
  self > date_or_time
end

#all_dayObject

Returns a Range representing the whole day of the current date/time.



287
288
289
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 287

def all_day
  beginning_of_day..end_of_day
end

#all_monthObject

Returns a Range representing the whole month of the current date/time.



298
299
300
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 298

def all_month
  beginning_of_month..end_of_month
end

#all_quarterObject

Returns a Range representing the whole quarter of the current date/time.



303
304
305
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 303

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.



293
294
295
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 293

def all_week(start_day = Date.beginning_of_week)
  beginning_of_week(start_day)..end_of_week(start_day)
end

#all_yearObject

Returns a Range representing the whole year of the current date/time.



308
309
310
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 308

def all_year
  beginning_of_year..end_of_year
end

#before?(date_or_time) ⇒ Boolean

Returns true if the date/time falls before date_or_time.

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 54

def before?(date_or_time)
  self < date_or_time
end

#beginning_of_monthObject 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


112
113
114
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 112

def beginning_of_month
  first_hour(change(day: 1))
end

#beginning_of_quarterObject 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


126
127
128
129
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 126

def beginning_of_quarter
  first_quarter_month = month - (2 + month) % 3
  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.



244
245
246
247
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 244

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_yearObject 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


156
157
158
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 156

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.



64
65
66
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 64

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.



69
70
71
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 69

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.



235
236
237
238
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 235

def days_to_week_start(start_day = Date.beginning_of_week)
  start_day_number = DAYS_INTO_WEEK.fetch(start_day)
  (wday - start_day_number) % 7
end

#end_of_monthObject 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.



273
274
275
276
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 273

def end_of_month
  last_day = ::Time.days_in_month(month, year)
  last_hour(days_since(last_day - day))
end

#end_of_quarterObject 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


141
142
143
144
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 141

def end_of_quarter
  last_quarter_month = month + (12 - month) % 3
  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.



260
261
262
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 260

def end_of_week(start_day = Date.beginning_of_week)
  last_hour(days_since(6 - days_to_week_start(start_day)))
end

#end_of_yearObject 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.



281
282
283
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 281

def end_of_year
  change(month: 12).end_of_month
end

#future?Boolean

Returns true if the date/time is in the future.

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 39

def future?
  self > self.class.current
end

#last_monthObject

Short-hand for months_ago(1).



217
218
219
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 217

def last_month
  months_ago(1)
end

#last_yearObject

Short-hand for years_ago(1).



228
229
230
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 228

def last_year
  years_ago(1)
end

#mondayObject

Returns Monday of this week assuming that week starts on Monday. DateTime objects have their time set to 0:00.



252
253
254
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 252

def monday
  beginning_of_week(:monday)
end

#months_ago(months) ⇒ Object

Returns a new date/time the specified number of months ago.



84
85
86
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 84

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.



89
90
91
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 89

def months_since(months)
  advance(months: months)
end

#next_occurring(day_of_week) ⇒ Object

Returns a new date/time representing the next occurrence of the specified day of week.

today = Date.today               # => Thu, 14 Dec 2017
today.next_occurring(:monday)    # => Mon, 18 Dec 2017
today.next_occurring(:thursday)  # => Thu, 21 Dec 2017


317
318
319
320
321
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 317

def next_occurring(day_of_week)
  from_now = DAYS_INTO_WEEK.fetch(day_of_week) - wday
  from_now += 7 unless from_now > 0
  advance(days: from_now)
end

#next_quarterObject

Short-hand for months_since(3)



192
193
194
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 192

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


177
178
179
180
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 177

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_weekdayObject

Returns a new date/time representing the next weekday.



183
184
185
186
187
188
189
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 183

def next_weekday
  if next_day.on_weekend?
    next_week(:monday, same_time: true)
  else
    next_day
  end
end

#on_weekday?Boolean

Returns true if the date/time does not fall on a Saturday or Sunday.

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 49

def on_weekday?
  !WEEKEND_DAYS.include?(wday)
end

#on_weekend?Boolean

Returns true if the date/time falls on a Saturday or Sunday.

Returns:

  • (Boolean)


44
45
46
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 44

def on_weekend?
  WEEKEND_DAYS.include?(wday)
end

#past?Boolean

Returns true if the date/time is in the past.

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 34

def past?
  self < self.class.current
end

#prev_occurring(day_of_week) ⇒ Object

Returns a new date/time representing the previous occurrence of the specified day of week.

today = Date.today               # => Thu, 14 Dec 2017
today.prev_occurring(:monday)    # => Mon, 11 Dec 2017
today.prev_occurring(:thursday)  # => Thu, 07 Dec 2017


328
329
330
331
332
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 328

def prev_occurring(day_of_week)
  ago = wday - DAYS_INTO_WEEK.fetch(day_of_week)
  ago += 7 unless ago > 0
  advance(days: -ago)
end

#prev_quarterObject Also known as: last_quarter

Short-hand for months_ago(3).



222
223
224
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 222

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.



200
201
202
203
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 200

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_weekdayObject Also known as: last_weekday

Returns a new date/time representing the previous weekday.



207
208
209
210
211
212
213
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 207

def prev_weekday
  if prev_day.on_weekend?
    copy_time_to(beginning_of_week(:friday))
  else
    prev_day
  end
end

#sundayObject

Returns Sunday of this week assuming that week starts on Monday. DateTime objects have their time set to 23:59:59.



267
268
269
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 267

def sunday
  end_of_week(:monday)
end

#today?Boolean

Returns true if the date/time is today.

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 29

def today?
  to_date == ::Date.current
end

#tomorrowObject

Returns a new date/time representing tomorrow.



24
25
26
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 24

def tomorrow
  advance(days: 1)
end

#weeks_ago(weeks) ⇒ Object

Returns a new date/time the specified number of weeks ago.



74
75
76
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 74

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.



79
80
81
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 79

def weeks_since(weeks)
  advance(weeks: weeks)
end

#years_ago(years) ⇒ Object

Returns a new date/time the specified number of years ago.



94
95
96
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 94

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.



99
100
101
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 99

def years_since(years)
  advance(years: years)
end

#yesterdayObject

Returns a new date/time representing yesterday.



19
20
21
# File 'lib/active_support/core_ext/date_and_time/calculations.rb', line 19

def yesterday
  advance(days: -1)
end