1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{calendar} ---
|
2000-04-03 17:13:55 -03:00
|
|
|
General calendar-related functions}
|
1998-04-28 11:28:57 -03:00
|
|
|
|
2000-04-03 17:13:55 -03:00
|
|
|
\declaremodule{standard}{calendar}
|
2001-11-06 18:14:35 -04:00
|
|
|
\modulesynopsis{Functions for working with calendars,
|
2001-05-09 12:50:17 -03:00
|
|
|
including some emulation of the \UNIX\ \program{cal}
|
2000-04-03 17:13:55 -03:00
|
|
|
program.}
|
|
|
|
\sectionauthor{Drew Csillag}{drew_csillag@geocities.com}
|
1998-04-28 11:28:57 -03:00
|
|
|
|
|
|
|
This module allows you to output calendars like the \UNIX{}
|
2000-04-03 17:13:55 -03:00
|
|
|
\program{cal} program, and provides additional useful functions
|
2000-08-30 11:02:25 -03:00
|
|
|
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 \function{setfirstweekday()} to set the first day of the
|
2001-12-12 01:40:46 -04:00
|
|
|
week to Sunday (6) or to any other weekday. Parameters that specify
|
|
|
|
dates are given as integers.
|
2000-08-30 11:02:25 -03:00
|
|
|
|
2006-03-31 11:26:22 -04:00
|
|
|
Most of these functions and classses rely on the \module{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.
|
|
|
|
|
|
|
|
\begin{classdesc}{Calendar}{\optional{firstweekday}}
|
|
|
|
Creates a \class{Calendar} object. \var{firstweekday} is an integer
|
2006-03-31 11:38:44 -04:00
|
|
|
specifying the first day of the week. \code{0} is Monday (the default),
|
|
|
|
\code{6} is Sunday.
|
2006-03-31 11:26:22 -04:00
|
|
|
|
2006-03-31 11:38:44 -04:00
|
|
|
A \class{Calendar} object provides several methods that can
|
2006-03-31 11:26:22 -04:00
|
|
|
be used for preparing the calendar data for formatting. This
|
|
|
|
class doesn't do any formatting itself. This is the job of
|
|
|
|
subclasses.
|
|
|
|
\versionadded{2.5}
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\class{Calendar} instances have the following methods:
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{firstweekday}{}
|
|
|
|
Return the first day of the week (as specified in the constructor
|
2006-03-31 11:38:44 -04:00
|
|
|
or changed via \method{setfirstweekday()}).
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{setfirstweekday}{weekday}
|
|
|
|
Set the first day of the week.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{iterweekdays}{weekday}
|
|
|
|
Return an iterator for the week day numbers that will be used
|
|
|
|
for one week. The first number from the iterator will be the
|
2006-03-31 11:38:44 -04:00
|
|
|
same as the number returned by \method{firstweekday()}.
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{itermonthdates}{year, month}
|
|
|
|
Return an iterator for the month \var{month} (1-12) in the
|
|
|
|
year \var{year}. This iterator will return all days (as
|
|
|
|
\class{datetime.date} objects) for the month and all days
|
|
|
|
before the start of the month or after the end of the month
|
|
|
|
that are required to get a complete week.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{itermonthdays2}{year, month}
|
|
|
|
Return an iterator for the month \var{month} in the year
|
|
|
|
\var{year} similar to \method{itermonthdates()}. Days returned
|
2006-03-31 11:38:44 -04:00
|
|
|
will be tuples consisting of a day number and a week day
|
2006-03-31 11:26:22 -04:00
|
|
|
number.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{itermonthdays}{year, month}
|
|
|
|
Return an iterator for the month \var{month} in the year
|
|
|
|
\var{year} similar to \method{itermonthdates()}. Days returned
|
|
|
|
will simply be day numbers.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{monthdatescalendar}{year, month}
|
|
|
|
Return a list of the weeks in the month \var{month} of
|
|
|
|
the \var{year} as full weeks. Weeks are lists of seven
|
|
|
|
\class{datetime.date} objects.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{monthdays2calendar}{year, month}
|
|
|
|
Return a list of the weeks in the month \var{month} of
|
|
|
|
the \var{year} as full weeks. Weeks are lists of seven
|
|
|
|
tuples of day numbers and weekday numbers.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{monthdayscalendar}{year, month}
|
|
|
|
Return a list of the weeks in the month \var{month} of
|
|
|
|
the \var{year} as full weeks. Weeks are lists of seven
|
|
|
|
day numbers.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{yeardatescalendar}{year, month\optional{, width}}
|
|
|
|
Return the data for the specified year ready for formatting. The return
|
2006-03-31 11:38:44 -04:00
|
|
|
value is a list of month rows. Each month row contains up to \var{width}
|
2006-03-31 11:26:22 -04:00
|
|
|
months (defaulting to 3). Each month contains between 4 and 6 weeks and
|
2006-03-31 11:38:44 -04:00
|
|
|
each week contains 1--7 days. Days are \class{datetime.date} objects.
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{yeardays2calendar}{year, month\optional{, width}}
|
|
|
|
Return the data for the specified year ready for formatting (similar to
|
|
|
|
\method{yeardatescalendar()}). Entries in the week lists are tuples of
|
|
|
|
day numbers and weekday numbers. Day numbers outside this month are zero.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{yeardayscalendar}{year, month\optional{, width}}
|
|
|
|
Return the data for the specified year ready for formatting (similar to
|
|
|
|
\method{yeardatescalendar()}). Entries in the week lists are day numbers.
|
|
|
|
Day numbers outside this month are zero.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
\begin{classdesc}{TextCalendar}{\optional{firstweekday}}
|
2006-03-31 11:38:44 -04:00
|
|
|
This class can be used to generate plain text calendars.
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\versionadded{2.5}
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\class{TextCalendar} instances have the following methods:
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, w\optional{, l}}}
|
2006-03-31 11:38:44 -04:00
|
|
|
Return a month's calendar in a multi-line string. If \var{w} is
|
2006-03-31 11:26:22 -04:00
|
|
|
provided, it specifies the width of the date columns, which are
|
|
|
|
centered. If \var{l} is given, it specifies the number of lines that
|
|
|
|
each week will use. Depends on the first weekday as set by
|
|
|
|
\function{setfirstweekday()}.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
|
2006-03-31 11:38:44 -04:00
|
|
|
Print a month's calendar as returned by \method{formatmonth()}.
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
2006-03-31 11:38:44 -04:00
|
|
|
\begin{methoddesc}{formatyear}{theyear, themonth\optional{, w\optional{,
|
|
|
|
l\optional{, c\optional{, m}}}}}
|
|
|
|
Return a \var{m}-column calendar for an entire year as a multi-line string.
|
2006-03-31 11:26:22 -04:00
|
|
|
Optional parameters \var{w}, \var{l}, and \var{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
|
|
|
|
\method{setfirstweekday()}. The earliest year for which a calendar can
|
|
|
|
be generated is platform-dependent.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
2006-03-31 11:38:44 -04:00
|
|
|
\begin{methoddesc}{pryear}{theyear\optional{, w\optional{, l\optional{,
|
|
|
|
c\optional{, m}}}}}
|
|
|
|
Print the calendar for an entire year as returned by \method{formatyear()}.
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
\begin{classdesc}{HTMLCalendar}{\optional{firstweekday}}
|
2006-03-31 11:38:44 -04:00
|
|
|
This class can be used to generate HTML calendars.
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\versionadded{2.5}
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
\class{HTMLCalendar} instances have the following methods:
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, withyear}}
|
2006-03-31 11:38:44 -04:00
|
|
|
Return a month's calendar as an HTML table. If \var{withyear} is
|
2006-03-31 11:26:22 -04:00
|
|
|
true the year will be included in the header, otherwise just the
|
2006-03-31 11:38:44 -04:00
|
|
|
month name will be used.
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
\begin{methoddesc}{formatyear}{theyear, themonth\optional{, width}}
|
2006-03-31 11:38:44 -04:00
|
|
|
Return a year's calendar as an HTML table. \var{width} (defaulting to 3)
|
2006-03-31 11:26:22 -04:00
|
|
|
specifies the number of months per row.
|
2006-03-31 11:38:44 -04:00
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
2006-03-31 11:38:44 -04:00
|
|
|
\begin{methoddesc}{formatyearpage}{theyear, themonth\optional{,
|
|
|
|
width\optional{, css\optional{, encoding}}}}
|
|
|
|
Return a year's calendar as a complete HTML page. \var{width}
|
2006-03-31 11:26:22 -04:00
|
|
|
(defaulting to 3) specifies the number of months per row. \var{css}
|
|
|
|
is the name for the cascading style sheet to be used. \constant{None}
|
2006-03-31 11:38:44 -04:00
|
|
|
can be passed if no style sheet should be used. \var{encoding}
|
2006-03-31 11:26:22 -04:00
|
|
|
specifies the encoding to be used for the output (defaulting
|
2006-03-31 11:38:44 -04:00
|
|
|
to the system default encoding).
|
|
|
|
\end{methoddesc}
|
2006-03-31 11:26:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
For simple text calendars this module provides the following functions.
|
2002-11-02 20:13:42 -04:00
|
|
|
|
2000-08-30 11:02:25 -03:00
|
|
|
\begin{funcdesc}{setfirstweekday}{weekday}
|
|
|
|
Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start
|
|
|
|
each week. The values \constant{MONDAY}, \constant{TUESDAY},
|
|
|
|
\constant{WEDNESDAY}, \constant{THURSDAY}, \constant{FRIDAY},
|
|
|
|
\constant{SATURDAY}, and \constant{SUNDAY} are provided for
|
|
|
|
convenience. For example, to set the first weekday to Sunday:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
import calendar
|
|
|
|
calendar.setfirstweekday(calendar.SUNDAY)
|
|
|
|
\end{verbatim}
|
2002-06-12 22:34:50 -03:00
|
|
|
\versionadded{2.0}
|
2000-08-30 11:02:25 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{firstweekday}{}
|
|
|
|
Returns the current setting for the weekday to start each week.
|
2002-06-12 22:34:50 -03:00
|
|
|
\versionadded{2.0}
|
2000-08-30 11:02:25 -03:00
|
|
|
\end{funcdesc}
|
1998-04-28 11:28:57 -03:00
|
|
|
|
|
|
|
\begin{funcdesc}{isleap}{year}
|
2003-12-31 00:51:56 -04:00
|
|
|
Returns \constant{True} if \var{year} is a leap year, otherwise
|
|
|
|
\constant{False}.
|
1998-04-28 11:28:57 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-08-30 11:02:25 -03:00
|
|
|
\begin{funcdesc}{leapdays}{y1, y2}
|
|
|
|
Returns the number of leap years in the range
|
2001-12-12 01:40:46 -04:00
|
|
|
[\var{y1}\ldots\var{y2}), where \var{y1} and \var{y2} are years.
|
2002-06-12 22:34:50 -03:00
|
|
|
\versionchanged[This function didn't work for ranges spanning
|
|
|
|
a century change in Python 1.5.2]{2.0}
|
1998-04-28 11:28:57 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{weekday}{year, month, day}
|
|
|
|
Returns the day of the week (\code{0} is Monday) for \var{year}
|
1998-05-08 12:39:40 -03:00
|
|
|
(\code{1970}--\ldots), \var{month} (\code{1}--\code{12}), \var{day}
|
1998-04-28 11:28:57 -03:00
|
|
|
(\code{1}--\code{31}).
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2004-12-06 03:58:14 -04:00
|
|
|
\begin{funcdesc}{weekheader}{n}
|
|
|
|
Return a header containing abbreviated weekday names. \var{n} specifies
|
|
|
|
the width in characters for one weekday.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1998-04-28 11:28:57 -03:00
|
|
|
\begin{funcdesc}{monthrange}{year, month}
|
|
|
|
Returns weekday of first day of the month and number of days in month,
|
|
|
|
for the specified \var{year} and \var{month}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{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.
|
2000-08-30 11:02:25 -03:00
|
|
|
Each week begins with Monday unless set by \function{setfirstweekday()}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
|
|
|
|
Prints a month's calendar as returned by \function{month()}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{month}{theyear, themonth\optional{, w\optional{, l}}}
|
2006-03-31 11:26:22 -04:00
|
|
|
Returns a month's calendar in a multi-line string using the
|
|
|
|
\method{formatmonth} of the \class{TextCalendar} class.
|
2002-06-12 22:34:50 -03:00
|
|
|
\versionadded{2.0}
|
1998-04-28 11:28:57 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-08-30 11:02:25 -03:00
|
|
|
\begin{funcdesc}{prcal}{year\optional{, w\optional{, l\optional{c}}}}
|
|
|
|
Prints the calendar for an entire year as returned by
|
|
|
|
\function{calendar()}.
|
1998-04-28 11:28:57 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-08-30 11:02:25 -03:00
|
|
|
\begin{funcdesc}{calendar}{year\optional{, w\optional{, l\optional{c}}}}
|
2006-03-31 11:26:22 -04:00
|
|
|
Returns a 3-column calendar for an entire year as a multi-line string
|
|
|
|
using the \method{formatyear} of the \class{TextCalendar} class.
|
2002-06-12 22:34:50 -03:00
|
|
|
\versionadded{2.0}
|
1998-04-28 11:28:57 -03:00
|
|
|
\end{funcdesc}
|
1999-06-09 12:11:58 -03:00
|
|
|
|
|
|
|
\begin{funcdesc}{timegm}{tuple}
|
2000-04-03 17:13:55 -03:00
|
|
|
An unrelated but handy function that takes a time tuple such as
|
|
|
|
returned by the \function{gmtime()} function in the \refmodule{time}
|
2001-11-28 03:26:15 -04:00
|
|
|
module, and returns the corresponding \UNIX{} timestamp value, assuming
|
1999-06-09 12:11:58 -03:00
|
|
|
an epoch of 1970, and the POSIX encoding. In fact,
|
2000-04-03 17:13:55 -03:00
|
|
|
\function{time.gmtime()} and \function{timegm()} are each others' inverse.
|
2002-06-12 22:34:50 -03:00
|
|
|
\versionadded{2.0}
|
1999-06-09 12:11:58 -03:00
|
|
|
\end{funcdesc}
|
2000-04-03 17:13:55 -03:00
|
|
|
|
2005-01-05 03:13:32 -04:00
|
|
|
The \module{calendar} module exports the following data attributes:
|
|
|
|
|
|
|
|
\begin{datadesc}{day_name}
|
|
|
|
An array that represents the days of the week in the
|
|
|
|
current locale.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{day_abbr}
|
|
|
|
An array that represents the abbreviated days of the week
|
|
|
|
in the current locale.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{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
|
|
|
|
\code{month_name[0]} is the empty string.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{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
|
|
|
|
\code{month_abbr[0]} is the empty string.
|
|
|
|
\end{datadesc}
|
2000-04-03 17:13:55 -03:00
|
|
|
|
|
|
|
\begin{seealso}
|
2003-12-31 00:51:56 -04:00
|
|
|
\seemodule{datetime}{Object-oriented interface to dates and times
|
|
|
|
with similar functionality to the
|
|
|
|
\refmodule{time} module.}
|
2000-04-03 17:13:55 -03:00
|
|
|
\seemodule{time}{Low-level time related functions.}
|
|
|
|
\end{seealso}
|