5.19 calendar -- General calendar-related functions

Python 2.4

5.19 calendar -- General calendar-related functions

This module allows you to output calendars like the Unix cal program, and provides additional useful functions related to the calendar. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention). Use setfirstweekday() to set the first day of the week to Sunday (6) or to any other weekday. Parameters that specify dates are given as integers.

Most of these functions rely on the datetime module which uses an idealized calendar, the current Gregorian calendar indefinitely extended in both directions. This matches the definition of the "proleptic Gregorian" calendar in Dershowitz and Reingold's book "Calendrical Calculations", where it's the base calendar for all computations.

Sets the weekday (0 is Monday, 6 is Sunday) to start each week. The values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are provided for convenience. For example, to set the first weekday to Sunday:

import calendar
calendar.setfirstweekday(calendar.SUNDAY)
New in version 2.0.

Returns the current setting for the weekday to start each week. New in version 2.0.

Returns True if year is a leap year, otherwise False.

Returns the number of leap years in the range [y1...y2), where y1 and y2 are years. Changed in version 2.0: This function didn't work for ranges spanning a century change in Python 1.5.2.

Returns the day of the week (0 is Monday) for year (1970-...), month (1-12), day (1-31).

Return a header containing abbreviated weekday names. n specifies the width in characters for one weekday.

Returns weekday of first day of the month and number of days in month, for the specified year and month.

Returns a matrix representing a month's calendar. Each row represents a week; days outside of the month a represented by zeros. Each week begins with Monday unless set by setfirstweekday().

Prints a month's calendar as returned by month().

Returns a month's calendar in a multi-line string. If w is provided, it specifies the width of the date columns, which are centered. If l is given, it specifies the number of lines that each week will use. Depends on the first weekday as set by setfirstweekday(). New in version 2.0.

Prints the calendar for an entire year as returned by calendar().

Returns a 3-column calendar for an entire year as a multi-line string. Optional parameters w, l, and c are for date column width, lines per week, and number of spaces between month columns, respectively. Depends on the first weekday as set by setfirstweekday(). The earliest year for which a calendar can be generated is platform-dependent. New in version 2.0.

An unrelated but handy function that takes a time tuple such as returned by the gmtime() function in the time module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In fact, time.gmtime() and timegm() are each others' inverse. New in version 2.0.

See Also:

Object-oriented interface to dates and times with similar functionality to the time module.
Low-level time related functions.
See About this document... for information on suggesting changes.