Module: PassiveSupport::CoreExtensions::Date::Calculations

Included in:
Date
Defined in:
lib/passive_support/core_ext/date/calculations.rb

Overview

Enables the use of time calculations within Date 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/passive_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
102
# File 'lib/passive_support/core_ext/date/calculations.rb', line 94

def advance(options)
  options = options.dup
  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/passive_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/passive_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)



193
194
195
# File 'lib/passive_support/core_ext/date/calculations.rb', line 193

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)



206
207
208
# File 'lib/passive_support/core_ext/date/calculations.rb', line 206

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)



169
170
171
172
173
# File 'lib/passive_support/core_ext/date/calculations.rb', line 169

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)



218
219
220
# File 'lib/passive_support/core_ext/date/calculations.rb', line 218

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)


110
111
112
113
114
115
116
# File 'lib/passive_support/core_ext/date/calculations.rb', line 110

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/passive_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)



199
200
201
202
# File 'lib/passive_support/core_ext/date/calculations.rb', line 199

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)



212
213
214
# File 'lib/passive_support/core_ext/date/calculations.rb', line 212

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)



178
179
180
181
182
# File 'lib/passive_support/core_ext/date/calculations.rb', line 178

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)



224
225
226
# File 'lib/passive_support/core_ext/date/calculations.rb', line 224

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/passive_support/core_ext/date/calculations.rb', line 46

def future?
  self > ::Date.current
end

#last_monthObject

:nodoc:



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

def last_month # :nodoc:
  PassiveSupport::Deprecation.warn("Date#last_month is deprecated and has been removed in Rails 3, please use Date#prev_month instead", caller)
  prev_month
end

#last_yearObject

:nodoc:



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

def last_year # :nodoc:
  PassiveSupport::Deprecation.warn("Date#last_year is deprecated and has been removed in Rails 3, please use Date#prev_year instead", caller)
  prev_year
end

#minus_with_duration(other) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
# File 'lib/passive_support/core_ext/date/calculations.rb', line 84

def minus_with_duration(other) #:nodoc:
  if PassiveSupport::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



119
120
121
# File 'lib/passive_support/core_ext/date/calculations.rb', line 119

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



124
125
126
# File 'lib/passive_support/core_ext/date/calculations.rb', line 124

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

#next_monthObject

Short-hand for months_since(1)



164
165
166
# File 'lib/passive_support/core_ext/date/calculations.rb', line 164

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



186
187
188
189
190
# File 'lib/passive_support/core_ext/date/calculations.rb', line 186

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)



149
150
151
# File 'lib/passive_support/core_ext/date/calculations.rb', line 149

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/passive_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/passive_support/core_ext/date/calculations.rb', line 76

def plus_with_duration(other) #:nodoc:
  if PassiveSupport::Duration === other
    other.since(self)
  else
    plus_without_duration(other)
  end
end

#prev_monthObject

Short-hand for months_ago(1)



159
160
161
# File 'lib/passive_support/core_ext/date/calculations.rb', line 159

def prev_month
  months_ago(1)
end

#prev_yearObject

Short-hand for years_ago(1)



144
145
146
# File 'lib/passive_support/core_ext/date/calculations.rb', line 144

def prev_year
  years_ago(1)
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/passive_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/passive_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



235
236
237
# File 'lib/passive_support/core_ext/date/calculations.rb', line 235

def tomorrow
  self + 1
end

#years_ago(years) ⇒ Object

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



129
130
131
# File 'lib/passive_support/core_ext/date/calculations.rb', line 129

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



134
135
136
# File 'lib/passive_support/core_ext/date/calculations.rb', line 134

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

#yesterdayObject

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



230
231
232
# File 'lib/passive_support/core_ext/date/calculations.rb', line 230

def yesterday
  self - 1
end