Class Date
In: lib/vpim/date.rb
Parent: Object

Extensions to the standard library Date.



TIME_START =, 1, 1)
SECS_PER_DAY = 24 * 60 * 60

Public Class methods

Create a new Date object for the date specified by year year, month mon, and day-of-the-week wday.

The nth, n, occurrence of wday within the period will be generated (n defaults to 1). 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.


  • Date.bywday(2004, nil, 1, 9) => the ninth Sunday of 2004
  • Date.bywday(2004, nil, 1) => the first Sunday of 2004
  • Date.bywday(2004, nil, 1, -2) => the second last Sunday of 2004
  • Date.bywday(2004, 12, 1) => the first sunday in the 12th month of 2004
  • Date.bywday(2004, 2, 2, -1) => last Tuesday in the 2nd month in 2004
  • Date.bywday(2004, -2, 3, -2) => second last Wednesday in the second last month of 2004

Compare this to, which allows a Date to be created by day-of-the-month, mday, to Date.ordinal, which allows a Date to be created by day-of-the-year, yday, and to Date.commercial, which allows a Date to be created by day-of-the-week, but within a specific week.

If wday responds to to_str, convert it to the wday number by searching for a wday that matches, using as many characters as are in wday to do the comparison. wday must be 2 or more characters long in order to be a unique match, other than that, "mo", "Mon", and "MonDay" are all valid strings for wday 1.

This method can be called on a valid wday, and it will return it. Perhaps it should be called by default inside the Date#new*() methods so that non-integer wday arguments can be used? Perhaps a similar method should exist for months? But with months, we all know January is 1, who can remember where Date chooses to start its wday count!


 Date.bywday(2004, 2, Date.str2wday('TU')) => the first Tuesday in
 Date.bywday(2004, 2, Date.str2wday(2)) => the same day, but notice
   that a valid wday integer can be passed right through.

Return the first day of the week for the specified date. Commercial weeks start on Monday, but the weekstart can be specified (as 0-6, where 0 is sunday, or in formate of Date.str2day).

Public Instance methods

Converts this object to a Time object, or throws an ArgumentError if conversion is not possible because it is before the start of epoch.