Module: DateAndTime::Calculations
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | activesupport/lib/active_support/core_ext/date_and_time/calculations.rb |
Constant Summary
-
DAYS_INTO_WEEK =
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 7{ monday: 0, tuesday: 1, wednesday: 2, thursday: 3, friday: 4, saturday: 5, sunday: 6 }
-
WEEKEND_DAYS =
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 16[ 6, 0 ]
Instance Attribute Summary
-
#future? ⇒ Boolean
readonly
Returns true if the date/time is in the future.
-
#on_weekday? ⇒ Boolean
readonly
Returns true if the date/time does not fall on a Saturday or Sunday.
-
#on_weekend? ⇒ Boolean
readonly
Returns true if the date/time falls on a Saturday or Sunday.
-
#past? ⇒ Boolean
readonly
Returns true if the date/time is in the past.
-
#today? ⇒ Boolean
readonly
Returns true if the date/time is today.
Instance Method Summary
-
#all_day
Returns a
::Range
representing the whole day of the current date/time. -
#all_month
Returns a
::Range
representing the whole month of the current date/time. -
#all_quarter
Returns a
::Range
representing the whole quarter of the current date/time. -
#all_week(start_day = Date.beginning_of_week)
Returns a
::Range
representing the whole week of the current date/time. -
#all_year
Returns a
::Range
representing the whole year of the current date/time. -
#at_beginning_of_month
Alias for #beginning_of_month.
-
#at_beginning_of_quarter
Alias for #beginning_of_quarter.
-
#at_beginning_of_week(start_day = Date.beginning_of_week)
Alias for #beginning_of_week.
-
#at_beginning_of_year
Alias for #beginning_of_year.
-
#at_end_of_month
Alias for #end_of_month.
-
#at_end_of_quarter
Alias for #end_of_quarter.
-
#at_end_of_week(start_day = Date.beginning_of_week)
Alias for #end_of_week.
-
#at_end_of_year
Alias for #end_of_year.
-
#beginning_of_month
(also: #at_beginning_of_month)
Returns a new date/time at the start of the month.
-
#beginning_of_quarter
(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)
(also: #at_beginning_of_week)
Returns a new date/time representing the start of this week on the given day.
-
#beginning_of_year
(also: #at_beginning_of_year)
Returns a new date/time at the beginning of the year.
-
#days_ago(days)
Returns a new date/time the specified number of days ago.
-
#days_since(days)
Returns a new date/time the specified number of days in the future.
-
#days_to_week_start(start_day = Date.beginning_of_week)
Returns the number of days to the start of the week on the given day.
-
#end_of_month
(also: #at_end_of_month)
Returns a new date/time representing the end of the month.
-
#end_of_quarter
(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)
(also: #at_end_of_week)
Returns a new date/time representing the end of this week on the given day.
-
#end_of_year
(also: #at_end_of_year)
Returns a new date/time representing the end of the year.
-
#last_month
Short-hand for months_ago(1).
-
#last_quarter
Alias for #prev_quarter.
-
#last_week(start_day = Date.beginning_of_week, same_time: false)
Alias for #prev_week.
-
#last_weekday
Alias for #prev_weekday.
-
#last_year
Short-hand for years_ago(1).
-
#monday
Returns Monday of this week assuming that week starts on Monday.
-
#months_ago(months)
Returns a new date/time the specified number of months ago.
-
#months_since(months)
Returns a new date/time the specified number of months in the future.
-
#next_day(days = 1)
Returns a new date/time the specified number of days in the future.
-
#next_month(months = 1)
Returns a new date/time the specified number of months in the future.
-
#next_occurring(day_of_week)
Returns a new date/time representing the next occurrence of the specified day of week.
-
#next_quarter
Short-hand for months_since(3).
-
#next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false)
Returns a new date/time representing the given day in the next week.
-
#next_weekday
Returns a new date/time representing the next weekday.
-
#next_year(years = 1)
Returns a new date/time the specified number of years in the future.
-
#prev_day(days = 1)
Returns a new date/time the specified number of days ago.
-
#prev_month(months = 1)
Returns a new date/time the specified number of months ago.
-
#prev_occurring(day_of_week)
Returns a new date/time representing the previous occurrence of the specified day of week.
-
#prev_quarter
(also: #last_quarter)
Short-hand for months_ago(3).
-
#prev_week(start_day = Date.beginning_of_week, same_time: false)
(also: #last_week)
Returns a new date/time representing the given day in the previous week.
-
#prev_weekday
(also: #last_weekday)
Returns a new date/time representing the previous weekday.
-
#prev_year(years = 1)
Returns a new date/time the specified number of years ago.
-
#sunday
Returns Sunday of this week assuming that week starts on Monday.
-
#tomorrow
Returns a new date/time representing tomorrow.
-
#weeks_ago(weeks)
Returns a new date/time the specified number of weeks ago.
-
#weeks_since(weeks)
Returns a new date/time the specified number of weeks in the future.
-
#years_ago(years)
Returns a new date/time the specified number of years ago.
-
#years_since(years)
Returns a new date/time the specified number of years in the future.
-
#yesterday
Returns a new date/time representing yesterday.
Instance Attribute Details
#future? ⇒ Boolean
(readonly)
Returns true if the date/time is in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 49
def future? self > self.class.current end
#on_weekday? ⇒ Boolean
(readonly)
Returns true if the date/time does not fall on a Saturday or Sunday.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 59
def on_weekday? !WEEKEND_DAYS.include?(wday) end
#on_weekend? ⇒ Boolean
(readonly)
Returns true if the date/time falls on a Saturday or Sunday.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 54
def on_weekend? WEEKEND_DAYS.include?(wday) end
#past? ⇒ Boolean
(readonly)
Returns true if the date/time is in the past.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 44
def past? self < self.class.current end
#today? ⇒ Boolean
(readonly)
Returns true if the date/time is today.
Instance Method Details
#all_day
Returns a ::Range
representing the whole day of the current date/time.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 308
def all_day beginning_of_day..end_of_day end
#all_month
Returns a ::Range
representing the whole month of the current date/time.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 319
def all_month beginning_of_month..end_of_month end
#all_quarter
Returns a ::Range
representing the whole quarter of the current date/time.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 324
def all_quarter beginning_of_quarter..end_of_quarter end
#all_week(start_day = Date.beginning_of_week)
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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 314
def all_week(start_day = Date.beginning_of_week) beginning_of_week(start_day)..end_of_week(start_day) end
#all_year
Returns a ::Range
representing the whole year of the current date/time.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 329
def all_year beginning_of_year..end_of_year end
#at_beginning_of_month
Alias for #beginning_of_month.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 115
alias :at_beginning_of_month :beginning_of_month
#at_beginning_of_quarter
Alias for #beginning_of_quarter.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 130
alias :at_beginning_of_quarter :beginning_of_quarter
#at_beginning_of_week(start_day = Date.beginning_of_week)
Alias for #beginning_of_week.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 269
alias :at_beginning_of_week :beginning_of_week
#at_beginning_of_year
Alias for #beginning_of_year.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 159
alias :at_beginning_of_year :beginning_of_year
#at_end_of_month
Alias for #end_of_month.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 298
alias :at_end_of_month :end_of_month
#at_end_of_quarter
Alias for #end_of_quarter.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 145
alias :at_end_of_quarter :end_of_quarter
#at_end_of_week(start_day = Date.beginning_of_week)
Alias for #end_of_week.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 284
alias :at_end_of_week :end_of_week
#at_end_of_year
Alias for #end_of_year.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 305
alias :at_end_of_year :end_of_year
#beginning_of_month 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
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 112
def beginning_of_month first_hour(change(day: 1)) end
#beginning_of_quarter 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
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 126
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) 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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 265
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 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
# File 'activesupport/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)
Returns a new date/time the specified number of days ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 64
def days_ago(days) advance(days: -days) end
#days_since(days)
Returns a new date/time the specified number of days in the future.
# File 'activesupport/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)
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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 255
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 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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 294
def end_of_month last_day = ::Time.days_in_month(month, year) last_hour(days_since(last_day - day)) end
#end_of_quarter 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
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 141
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) 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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 281
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 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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 302
def end_of_year change(month: 12).end_of_month end
#last_month
Short-hand for months_ago(1).
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 232
def last_month months_ago(1) end
#last_quarter
Alias for #prev_quarter.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 240
alias_method :last_quarter, :prev_quarter
#last_week(start_day = Date.beginning_of_week, same_time: false)
Alias for #prev_week.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 214
alias_method :last_week, :prev_week
#last_weekday
Alias for #prev_weekday.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 224
alias_method :last_weekday, :prev_weekday
#last_year
Short-hand for years_ago(1).
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 248
def last_year years_ago(1) end
#monday
Returns Monday of this week assuming that week starts on Monday. ::DateTime
objects have their time set to 0:00.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 273
def monday beginning_of_week(:monday) end
#months_ago(months)
Returns a new date/time the specified number of months ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 84
def months_ago(months) advance(months: -months) end
#months_since(months)
Returns a new date/time the specified number of months in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 89
def months_since(months) advance(months: months) end
#next_day(days = 1)
Returns a new date/time the specified number of days in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 34
def next_day(days = 1) advance(days: days) end
#next_month(months = 1)
Returns a new date/time the specified number of months in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 192
def next_month(months = 1) advance(months: months) end
#next_occurring(day_of_week)
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 338
def next_occurring(day_of_week) current_day_number = wday != 0 ? wday - 1 : 6 from_now = DAYS_INTO_WEEK.fetch(day_of_week) - current_day_number from_now += 7 unless from_now > 0 advance(days: from_now) end
#next_quarter
Short-hand for months_since(3)
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 197
def next_quarter months_since(3) end
#next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false)
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
# File 'activesupport/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_weekday
Returns a new date/time representing the next weekday.
# File 'activesupport/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
#next_year(years = 1)
Returns a new date/time the specified number of years in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 202
def next_year(years = 1) advance(years: years) end
#prev_day(days = 1)
Returns a new date/time the specified number of days ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 24
def prev_day(days = 1) advance(days: -days) end
#prev_month(months = 1)
Returns a new date/time the specified number of months ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 227
def prev_month(months = 1) advance(months: -months) end
#prev_occurring(day_of_week)
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 350
def prev_occurring(day_of_week) current_day_number = wday != 0 ? wday - 1 : 6 ago = current_day_number - DAYS_INTO_WEEK.fetch(day_of_week) ago += 7 unless ago > 0 advance(days: -ago) end
#prev_quarter Also known as: #last_quarter
Short-hand for months_ago(3).
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 237
def prev_quarter months_ago(3) end
#prev_week(start_day = Date.beginning_of_week, same_time: false) 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.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 210
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 Also known as: #last_weekday
Returns a new date/time representing the previous weekday.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 217
def prev_weekday if prev_day.on_weekend? copy_time_to(beginning_of_week(:friday)) else prev_day end end
#prev_year(years = 1)
Returns a new date/time the specified number of years ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 243
def prev_year(years = 1) advance(years: -years) end
#sunday
Returns Sunday of this week assuming that week starts on Monday. ::DateTime
objects have their time set to 23:59:59.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 288
def sunday end_of_week(:monday) end
#tomorrow
Returns a new date/time representing tomorrow.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 29
def tomorrow advance(days: 1) end
#weeks_ago(weeks)
Returns a new date/time the specified number of weeks ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 74
def weeks_ago(weeks) advance(weeks: -weeks) end
#weeks_since(weeks)
Returns a new date/time the specified number of weeks in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 79
def weeks_since(weeks) advance(weeks: weeks) end
#years_ago(years)
Returns a new date/time the specified number of years ago.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 94
def years_ago(years) advance(years: -years) end
#years_since(years)
Returns a new date/time the specified number of years in the future.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 99
def years_since(years) advance(years: years) end
#yesterday
Returns a new date/time representing yesterday.
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 19
def yesterday advance(days: -1) end