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

Instance Method Summary collapse

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.


79
80
81
82
83
84
85
86
# File 'lib/active_support/core_ext/date/calculations.rb', line 79

def advance(options)
  d = self
  d = d >> options.delete(:years) * 12 if options[:years]
  d = d >> options.delete(:months)     if options[:months]
  d = d +  options.delete(:weeks) * 7  if options[:weeks]
  d = d +  options.delete(:days)       if options[: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


37
38
39
# File 'lib/active_support/core_ext/date/calculations.rb', line 37

def ago(seconds)
  to_time.since(-seconds)
end

#beginning_of_dayObject 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)


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

def beginning_of_day
  to_time
end

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


167
168
169
# File 'lib/active_support/core_ext/date/calculations.rb', line 167

def beginning_of_month
  self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1)
end

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


180
181
182
# File 'lib/active_support/core_ext/date/calculations.rb', line 180

def beginning_of_quarter
  beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
end

#beginning_of_weekObject 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)


143
144
145
146
147
# File 'lib/active_support/core_ext/date/calculations.rb', line 143

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


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

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

Returns a new Date where one or more of the elements have been changed according to the options parameter.

Examples:

Date.new(2007, 5, 12).change(:day => 1)                  # => Date.new(2007, 5, 1)
Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)

94
95
96
97
98
99
100
# File 'lib/active_support/core_ext/date/calculations.rb', line 94

def change(options)
  ::Date.new(
    options[:year]  || self.year,
    options[:month] || self.month,
    options[:day]   || self.day
  )
end

#end_of_dayObject

Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)


57
58
59
# File 'lib/active_support/core_ext/date/calculations.rb', line 57

def end_of_day
  to_time.end_of_day
end

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


173
174
175
176
# File 'lib/active_support/core_ext/date/calculations.rb', line 173

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

#end_of_quarterObject Also known as: at_end_of_quarter

Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59)


186
187
188
# File 'lib/active_support/core_ext/date/calculations.rb', line 186

def end_of_quarter
  change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
end

#end_of_weekObject Also known as: at_end_of_week

Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59)


152
153
154
155
156
# File 'lib/active_support/core_ext/date/calculations.rb', line 152

def end_of_week
  days_to_sunday = self.wday!=0 ? 7-self.wday : 0
  result = self + days_to_sunday.days
  self.acts_like?(:time) ? result.end_of_day : result
end

#end_of_yearObject Also known as: at_end_of_year

Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)


198
199
200
# File 'lib/active_support/core_ext/date/calculations.rb', line 198

def end_of_year
  self.acts_like?(:time) ? change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31)
end

#last_monthObject

Short-hand for months_ago(1)


133
134
135
# File 'lib/active_support/core_ext/date/calculations.rb', line 133

def last_month
  months_ago(1)
end

#last_yearObject

Short-hand for years_ago(1)


123
124
125
# File 'lib/active_support/core_ext/date/calculations.rb', line 123

def last_year
  years_ago(1)
end

#minus_with_duration(other) ⇒ Object

:nodoc:


69
70
71
72
73
74
75
# File 'lib/active_support/core_ext/date/calculations.rb', line 69

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


103
104
105
# File 'lib/active_support/core_ext/date/calculations.rb', line 103

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


108
109
110
# File 'lib/active_support/core_ext/date/calculations.rb', line 108

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

#next_monthObject

Short-hand for months_since(1)


138
139
140
# File 'lib/active_support/core_ext/date/calculations.rb', line 138

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).


160
161
162
163
164
# File 'lib/active_support/core_ext/date/calculations.rb', line 160

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_yearObject

Short-hand for years_since(1)


128
129
130
# File 'lib/active_support/core_ext/date/calculations.rb', line 128

def next_year
  years_since(1)
end

#plus_with_duration(other) ⇒ Object

:nodoc:


61
62
63
64
65
66
67
# File 'lib/active_support/core_ext/date/calculations.rb', line 61

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


43
44
45
# File 'lib/active_support/core_ext/date/calculations.rb', line 43

def since(seconds)
  to_time.since(seconds)
end

#tomorrowObject

Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time


209
210
211
# File 'lib/active_support/core_ext/date/calculations.rb', line 209

def tomorrow
  self + 1
end

#years_ago(years) ⇒ Object

Returns a new Date/DateTime representing the time a number of specified years ago


113
114
115
# File 'lib/active_support/core_ext/date/calculations.rb', line 113

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


118
119
120
# File 'lib/active_support/core_ext/date/calculations.rb', line 118

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

#yesterdayObject

Convenience method which returns a new Date/DateTime representing the time 1 day ago


204
205
206
# File 'lib/active_support/core_ext/date/calculations.rb', line 204

def yesterday
  self - 1
end