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.

setfirstweekday( weekday)
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.

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

isleap( year)
Returns True if year is a leap year, otherwise False.

leapdays( y1, y2)
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.

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

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

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

monthcalendar( year, 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().

prmonth( theyear, themonth[, w[, l]])
Prints a month's calendar as returned by month().

month( theyear, themonth[, w[, l]])
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.

prcal( year[, w[, l[c]]])
Prints the calendar for an entire year as returned by calendar().

calendar( year[, w[, l[c]]])
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.

timegm( tuple)
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.

The calendar module exports the following data attributes:

day_name
An array that represents the days of the week in the current locale.

day_abbr
An array that represents the abbreviated days of the week in the current locale.

month_name
An array that represents the months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and month_name[0] is the empty string.

month_abbr
An array that represents the abbreviated months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and month_abbr[0] is the empty string.

See Also:

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