5.3.2 draco.date -- Date and Time handling

The module draco.date contains two objects to work with date and date/times in the Gregorian (western) calendar. You might wonder why Draco provides its own date and time classes. The answer is that I have needed them for many dynamic web sites, where stuff like expiration and reading/formatting of dates is very common. Because I didn't want Draco to depend on too many external packages, I decided to provide an implementation of them with Draco. These classes are used internally by Draco too.

class Date( [date])
An object representing a date in the Gregorian calendar. It support parsing of various input formats, formatting to various output formats, and simple arithmetic. The optional argument date specifies the date to initialize the object with. It can be one of: If date is not specified, the object is initialized to the current date.

Internally the date is kept as a (year, month, day) tuple. Representable dates are, in principle, only limited by their storage type. However, this class uses the function time.strptime to parse string inputs which may limit the range of possible dates.

The public interface of Date is described below.

year
The year (read-write).

month
The month, in the range from 1 to 12 (read-write).

day
The day of the month, in the range from 1 to 31 (read-write).

weekday
The day of the week, in the range from 0 to 6. Monday is day 0 (read-only).

yearday
The day of the year, in the range from 1 to 366 (read-only).

monthdays
The number of days in this month (read-only).

isleap
Is this year leap (read-only)?

parse( date)
Parse the date string date. If date is not in a supported format, a ValueError is raised.

strftime( format)
Use time.strftime to format the date to a string. The argument format specifies the strftime format string.

format( name)
Format the date using a predefined date format name. Initially, the following formats are defined:

"iso8601"
ISO-8601 compatible format (yyyy-mm-dd).
"ctime"
Unix ctime(3) format.

Formats can be changed and added using setFormat() and setDefaultFormat().

setFormat( name, value)
Set the date format name to value. The argument value must be a strftime type format string.

setDefaultFormat( name, value)
Set the date format name to value. This function is the same as setFormat() above, with the difference that it is a class method. After this call, the format will be in effect for all newly created instances of this class.

addDate( dy, dm, dd)
Add dy years, dm months and dd days to the date. All three arguments can be an arbitrary positive or negative numeric value.

The class DateTime is a subclass of Date that supports time handling too.

class DateTime( [date])
An object representing a date and time in the Gregorian calendar. It support parsing of various input formats, formatting to various output formats, and simple arithmetic. The optional argument date specifies the date and time to initialize the object with. It can be one of: If date is not specified, the object is initialized to the current date and time.

Internally, the date is represented as a (year, month, day, hour, minute, second) tuple. The same note about representable dates for Date instances is valid for DateTime instances.

In addition to the members of Date, DateTime has the following members:

hour
The hour in the range from 0 to 23 (read-write).

minute
The minute in the range from 0 to 59 (read-write).

second
The second in the range from 0 to 59 (read-write).

format( format)
In addition to the default formats of Date, the following formats are provided:

"rfc1123"
RFC1123 format, which is the RFC822 format with a 4 digit year: Wdy, dd Mon yyyy hh:mm:ss.
"nscookie"
Netscape's specification for the ``expires'' argument of a cookie: Wdy, dd-Mon-yyyy hh:mm:ss.
"short"
Unix ps(1) like, very condensed format. Either hh:mm, ddMon or Monyyyy, depending on the date relative to the current date.

addTime( dh, dm, ds)
Add dh hours, dm minutes and ds seconds to the datetime. All three arguments can be an arbitrary positive or negative numeric value.

Date and DateTime objects can be compared with each other and tested for equality. In a mixed comparision between a Date and a DateTime object, the Date object will behave as it would have a time of 00:00:00.