blob: 6dde6dd4d84589b7bd81fcf252512878dec9249d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{calendar} ---
Fred Drake38e5d272000-04-03 20:13:55 +00002 General calendar-related functions}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{calendar}
Fred Drake1a670c82001-11-06 22:14:35 +00005\modulesynopsis{Functions for working with calendars,
Fred Drakec116b822001-05-09 15:50:17 +00006 including some emulation of the \UNIX\ \program{cal}
Fred Drake38e5d272000-04-03 20:13:55 +00007 program.}
8\sectionauthor{Drew Csillag}{drew_csillag@geocities.com}
Fred Drake1c127e71998-04-28 14:28:57 +00009
10This module allows you to output calendars like the \UNIX{}
Fred Drake38e5d272000-04-03 20:13:55 +000011\program{cal} program, and provides additional useful functions
Skip Montanaro7b828a62000-08-30 14:02:25 +000012related to the calendar. By default, these calendars have Monday as
13the first day of the week, and Sunday as the last (the European
14convention). Use \function{setfirstweekday()} to set the first day of the
15week to Sunday (6) or to any other weekday.
16
17\begin{funcdesc}{setfirstweekday}{weekday}
18Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start
19each week. The values \constant{MONDAY}, \constant{TUESDAY},
20\constant{WEDNESDAY}, \constant{THURSDAY}, \constant{FRIDAY},
21\constant{SATURDAY}, and \constant{SUNDAY} are provided for
22convenience. For example, to set the first weekday to Sunday:
23
24\begin{verbatim}
25import calendar
26calendar.setfirstweekday(calendar.SUNDAY)
27\end{verbatim}
28\end{funcdesc}
29
30\begin{funcdesc}{firstweekday}{}
31Returns the current setting for the weekday to start each week.
32\end{funcdesc}
Fred Drake1c127e71998-04-28 14:28:57 +000033
34\begin{funcdesc}{isleap}{year}
Fred Drake38e5d272000-04-03 20:13:55 +000035Returns true if \var{year} is a leap year.
Fred Drake1c127e71998-04-28 14:28:57 +000036\end{funcdesc}
37
Skip Montanaro7b828a62000-08-30 14:02:25 +000038\begin{funcdesc}{leapdays}{y1, y2}
39Returns the number of leap years in the range
Fred Drake33837922000-10-09 15:27:31 +000040[\var{y1}\ldots\var{y2}).
Fred Drake1c127e71998-04-28 14:28:57 +000041\end{funcdesc}
42
43\begin{funcdesc}{weekday}{year, month, day}
44Returns the day of the week (\code{0} is Monday) for \var{year}
Fred Drake02379fa1998-05-08 15:39:40 +000045(\code{1970}--\ldots), \var{month} (\code{1}--\code{12}), \var{day}
Fred Drake1c127e71998-04-28 14:28:57 +000046(\code{1}--\code{31}).
47\end{funcdesc}
48
49\begin{funcdesc}{monthrange}{year, month}
50Returns weekday of first day of the month and number of days in month,
51for the specified \var{year} and \var{month}.
52\end{funcdesc}
53
54\begin{funcdesc}{monthcalendar}{year, month}
55Returns a matrix representing a month's calendar. Each row represents
56a week; days outside of the month a represented by zeros.
Skip Montanaro7b828a62000-08-30 14:02:25 +000057Each week begins with Monday unless set by \function{setfirstweekday()}.
Fred Drake1c127e71998-04-28 14:28:57 +000058\end{funcdesc}
59
Skip Montanaro7b828a62000-08-30 14:02:25 +000060\begin{funcdesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
61Prints a month's calendar as returned by \function{month()}.
Fred Drake1c127e71998-04-28 14:28:57 +000062\end{funcdesc}
63
Skip Montanaro7b828a62000-08-30 14:02:25 +000064\begin{funcdesc}{month}{theyear, themonth\optional{, w\optional{, l}}}
65Returns a month's calendar in a multi-line string. If \var{w} is
66provided, it specifies the width of the date columns, which are
67centered. If \var{l} is given, it specifies the number of lines that
68each week will use. Depends on the first weekday as set by
69\function{setfirstweekday()}.
70\end{funcdesc}
71
72\begin{funcdesc}{prcal}{year\optional{, w\optional{, l\optional{c}}}}
73Prints the calendar for an entire year as returned by
74\function{calendar()}.
75\end{funcdesc}
76
77\begin{funcdesc}{calendar}{year\optional{, w\optional{, l\optional{c}}}}
78Returns a 3-column calendar for an entire year as a multi-line string.
79Optional parameters \var{w}, \var{l}, and \var{c} are for date column
80width, lines per week, and number of spaces between month columns,
81respectively. Depends on the first weekday as set by
Skip Montanaro5ff41d12001-08-22 12:43:38 +000082\function{setfirstweekday()}. The earliest year for which a calendar can
83be generated is platform-dependent.
Fred Drake1c127e71998-04-28 14:28:57 +000084\end{funcdesc}
Guido van Rossum47274561999-06-09 15:11:58 +000085
86\begin{funcdesc}{timegm}{tuple}
Fred Drake38e5d272000-04-03 20:13:55 +000087An unrelated but handy function that takes a time tuple such as
88returned by the \function{gmtime()} function in the \refmodule{time}
Fred Drakec37b65e2001-11-28 07:26:15 +000089module, and returns the corresponding \UNIX{} timestamp value, assuming
Guido van Rossum47274561999-06-09 15:11:58 +000090an epoch of 1970, and the POSIX encoding. In fact,
Fred Drake38e5d272000-04-03 20:13:55 +000091\function{time.gmtime()} and \function{timegm()} are each others' inverse.
Guido van Rossum47274561999-06-09 15:11:58 +000092\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +000093
94
95\begin{seealso}
96 \seemodule{time}{Low-level time related functions.}
97\end{seealso}