123456789_123456789_123456789_123456789_123456789_

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

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#future?Boolean (readonly)

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

[ GitHub ]

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

def future?
  self > self.class.current
end

#past?Boolean (readonly)

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

[ GitHub ]

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

def past?
  self < self.class.current
end

#today?Boolean (readonly)

Returns true if the date/time is today.

[ GitHub ]

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

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

Instance Method Details

#all_month

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 234

def all_month
  beginning_of_month..end_of_month
end

#all_quarter

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 239

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.week_start or config.week_start when set.

[ GitHub ]

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

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 244

def all_year
  beginning_of_year..end_of_year
end

#at_beginning_of_month

Alias for #beginning_of_month.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 83

alias :at_beginning_of_month :beginning_of_month

#at_beginning_of_quarter

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 92

alias :at_beginning_of_quarter :beginning_of_quarter

#at_beginning_of_week(start_day = Date.beginning_of_week)

Alias for #beginning_of_week.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 189

alias :at_beginning_of_week :beginning_of_week

#at_beginning_of_year

Alias for #beginning_of_year.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 109

alias :at_beginning_of_year :beginning_of_year

#at_end_of_month

Alias for #end_of_month.

[ GitHub ]

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

alias :at_end_of_month :end_of_month

#at_end_of_quarter

Alias for #end_of_quarter.

[ GitHub ]

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

alias :at_end_of_quarter :end_of_quarter

#at_end_of_week(start_day = Date.beginning_of_week)

Alias for #end_of_week.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 204

alias :at_end_of_week :end_of_week

#at_end_of_year

Alias for #end_of_year.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 225

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. ::DateTime objects will have a time set to 0:00.

[ GitHub ]

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

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. Example: 1st January, 1st July, 1st October. ::DateTime objects will have a time set to 0:00.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 88

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 185

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

Return a new date/time at the beginning of the year. Example: 1st January. ::DateTime objects will have a time set to 0:00.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 106

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.

[ GitHub ]

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

def days_ago(days)
  advance(:days => -days)
end

#days_since(days)

Returns a new date/time the specified number of days in the future.

[ GitHub ]

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

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 175

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 214

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. Example: 31st March, 30th June, 30th September. ::DateTime objects will have a time set to 23:59:59.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 97

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 201

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.

[ GitHub ]

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

def end_of_year
  change(:month => 12).end_of_month
end

#last_month

Alias for #prev_month.

[ GitHub ]

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

alias_method :last_month, :prev_month

#last_quarter

Alias for #prev_quarter.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 164

alias_method :last_quarter, :prev_quarter

#last_week(start_day = Date.beginning_of_week)

Alias for #prev_week.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 152

alias_method :last_week, :prev_week

#last_year

Alias for #prev_year.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 170

alias_method :last_year, :prev_year

#monday

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

[ GitHub ]

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

def monday
  beginning_of_week(:monday)
end

#months_ago(months)

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

[ GitHub ]

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

def months_ago(months)
  advance(:months => -months)
end

#months_since(months)

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

[ GitHub ]

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

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

#next_month

Short-hand for months_since(1).

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 131

def next_month
  months_since(1)
end

#next_quarter

Short-hand for months_since(3)

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 136

def next_quarter
  months_since(3)
end

#next_week(given_day_in_next_week = Date.beginning_of_week)

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

today = Date.today       # => Thu, 07 May 2015
today.next_week(:friday) # => Fri, 15 May 2015

when set. ::DateTime objects have their time set to 0:00.

now = Time.current # => Thu, 07 May 2015 13:31:16 UTC +00:00
now.next_week      # => Mon, 11 May 2015 00:00:00 UTC +00:00
[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 126

def next_week(given_day_in_next_week = Date.beginning_of_week)
  first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week)))
end

#next_year

Short-hand for years_since(1).

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 141

def next_year
  years_since(1)
end

#prev_month Also known as: #last_month

Short-hand for months_ago(1).

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 155

def prev_month
  months_ago(1)
end

#prev_quarter Also known as: #last_quarter

Short-hand for months_ago(3).

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 161

def prev_quarter
  months_ago(3)
end

#prev_week(start_day = Date.beginning_of_week) 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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 149

def prev_week(start_day = Date.beginning_of_week)
  first_hour(weeks_ago(1).beginning_of_week.days_since(days_span(start_day)))
end

#prev_year Also known as: #last_year

Short-hand for years_ago(1).

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 167

def prev_year
  years_ago(1)
end

#sunday

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 208

def sunday
  end_of_week(:monday)
end

#tomorrow

Returns a new date/time representing tomorrow.

[ GitHub ]

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

def tomorrow
  advance(:days => 1)
end

#weeks_ago(weeks)

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

[ GitHub ]

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

def weeks_ago(weeks)
  advance(:weeks => -weeks)
end

#weeks_since(weeks)

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

[ GitHub ]

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

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

#years_ago(years)

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

[ GitHub ]

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

def years_ago(years)
  advance(:years => -years)
end

#years_since(years)

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

[ GitHub ]

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

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

#yesterday

Returns a new date/time representing yesterday.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/date_and_time/calculations.rb', line 14

def yesterday
  advance(:days => -1)
end