class DateGen
DateGen generates arrays of dates matching simple criteria.
Public Class Methods
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.
# File lib/vpim/date.rb, line 204 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
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:
-
::bywday(2004, nil, 1, 9) => the ninth Sunday in 2004
-
::bywday(2004, nil, 1) => all Sundays in 2004
-
::bywday(2004, nil, 1, -2) => second last Sunday in 2004
-
::bywday(2004, 12, 1) => all sundays in December 2004
-
::bywday(2004, 2, 2, -1) => last Tuesday in February in 2004
-
::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.
# File lib/vpim/date.rb, line 174 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
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).
# File lib/vpim/date.rb, line 143 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