Module: ActiveSupport::CoreExtensions::Date::Calculations

Included in:
Date
Defined in:
lib/gems/activesupport-2.2.2/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/gems/activesupport-2.2.2/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.



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

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



52
53
54
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 52

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)



64
65
66
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 64

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)



182
183
184
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 182

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)



195
196
197
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 195

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)



158
159
160
161
162
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 158

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)



207
208
209
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 207

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)


109
110
111
112
113
114
115
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 109

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)



72
73
74
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 72

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)



188
189
190
191
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 188

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)



201
202
203
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 201

def end_of_quarter
  beginning_of_month.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)



167
168
169
170
171
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 167

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)



213
214
215
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 213

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

#future?Boolean

Tells whether the Date object’s date lies in the future

Returns:

  • (Boolean)


46
47
48
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 46

def future?
  self > ::Date.current
end

#last_monthObject

Short-hand for months_ago(1)



148
149
150
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 148

def last_month
  months_ago(1)
end

#last_yearObject

Short-hand for years_ago(1)



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

def last_year
  years_ago(1)
end

#minus_with_duration(other) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 84

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



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

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



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

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

#next_monthObject

Short-hand for months_since(1)



153
154
155
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 153

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



175
176
177
178
179
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 175

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)



143
144
145
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 143

def next_year
  years_since(1)
end

#past?Boolean

Tells whether the Date object’s date lies in the past

Returns:

  • (Boolean)


36
37
38
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 36

def past?
  self < ::Date.current
end

#plus_with_duration(other) ⇒ Object

:nodoc:



76
77
78
79
80
81
82
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 76

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



58
59
60
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 58

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

#today?Boolean

Tells whether the Date object’s date is today

Returns:

  • (Boolean)


41
42
43
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 41

def today?
  self.to_date == ::Date.current # we need the to_date because of DateTime
end

#tomorrowObject

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



224
225
226
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 224

def tomorrow
  self + 1
end

#years_ago(years) ⇒ Object

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



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

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



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

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

#yesterdayObject

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



219
220
221
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb', line 219

def yesterday
  self - 1
end