Class: DateGen

Inherits:
Object
  • Object
show all
Defined in:
lib/vpim/date.rb

Overview

DateGen generates arrays of dates matching simple criteria.

Class Method Summary collapse

Class Method Details

.bymonthday(year, month, mday) ⇒ Object

Generate an array of dates on mday (the day-of-month, 1-31). For months in which the mday is not present, no date will be generated.

The period is a year, unless month is non-nil, in which case it is just that month.

Compare to Date.new(), which allows a single Date to be created with similar criteria.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/vpim/date.rb', line 205

def DateGen.bymonthday(year, month, mday)
  months = month ? [ month ] : 1..12
  dates = [ ]

  months.each do |m|
    begin
      dates << Date.new(year, m, mday)
    rescue ArgumentError
      # Don't generate dates for invalid combinations (Feb 29, when it's not
      # a leap year, for example).
      #
      # TODO - should we raise when month is out of range, or mday can never
      # be in range (32)?
    end
  end
  dates
end

.bywday(year, month, wday, n = nil) ⇒ Object

Generate an array of dates on wday (the day-of-week, 0-6, where 0 is Sunday).

If n is specified, only the nth occurrence of wday within the period will be generated. If n is positive, the nth occurrence from the beginning of the period will be returned, if negative, the nth occurrence from the end of the period will be returned.

The period is a year, unless month is non-nil, in which case it is just that month.

Examples:

  • DateGen.bywday(2004, nil, 1, 9) => the ninth Sunday in 2004

  • DateGen.bywday(2004, nil, 1) => all Sundays in 2004

  • DateGen.bywday(2004, nil, 1, -2) => second last Sunday in 2004

  • DateGen.bywday(2004, 12, 1) => all sundays in December 2004

  • DateGen.bywday(2004, 2, 2, -1) => last Tuesday in February in 2004

  • DateGen.bywday(2004, -2, 3, -2) => second last Wednesday in November of 2004

Compare to Date.bywday(), which allows a single Date to be created with similar criteria.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/vpim/date.rb', line 175

def DateGen.bywday(year, month, wday, n = nil)
  seed = Date.bywday(year, month, wday, n ? n : 1)

  dates = [ seed ]

  return dates if n

  succ = seed.clone

  # Collect all matches until we're out of the year (or month, if specified)
  loop do
    succ += 7

    break if succ.year != year
    break if month && succ.month != seed.month

    dates.push succ
  end
  dates.sort!
  dates
end

.weekofdate(year, mon, day, weekstart) ⇒ Object

Generate an array of a week’s dates, where week is specified by year, mon, day, and the weekstart (the day-of-week that is considered the “first” day of that week, 0-6, where 0 is sunday).



144
145
146
147
148
149
150
151
152
# File 'lib/vpim/date.rb', line 144

def DateGen.weekofdate(year, mon, day, weekstart)
  d = Date.weekstart(year, mon, day, weekstart)
  week = []
  7.times do
    week << d
    d = d + 1
  end
  week
end