blob: bf3a7d64b8be94be2b2f7a39e39752d976e18e0f [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
Fred Drake1529ef82001-12-12 05:40:46 +000015week to Sunday (6) or to any other weekday. Parameters that specify
16dates are given as integers.
Skip Montanaro7b828a62000-08-30 14:02:25 +000017
Raymond Hettingere11b5102002-12-25 16:37:19 +000018Most of these functions rely on the \module{datetime} module which
19uses an idealized calendar, the current Gregorian calendar indefinitely
20extended in both directions. This matches the definition of the
21"proleptic Gregorian" calendar in Dershowitz and Reingold's book
22"Calendrical Calculations", where it's the base calendar for all
23computations.
Neal Norwitz034c7492002-11-03 00:13:42 +000024
Skip Montanaro7b828a62000-08-30 14:02:25 +000025\begin{funcdesc}{setfirstweekday}{weekday}
26Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start
27each week. The values \constant{MONDAY}, \constant{TUESDAY},
28\constant{WEDNESDAY}, \constant{THURSDAY}, \constant{FRIDAY},
29\constant{SATURDAY}, and \constant{SUNDAY} are provided for
30convenience. For example, to set the first weekday to Sunday:
31
32\begin{verbatim}
33import calendar
34calendar.setfirstweekday(calendar.SUNDAY)
35\end{verbatim}
Fred Drakee9996c62002-06-13 01:34:50 +000036\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +000037\end{funcdesc}
38
39\begin{funcdesc}{firstweekday}{}
40Returns the current setting for the weekday to start each week.
Fred Drakee9996c62002-06-13 01:34:50 +000041\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +000042\end{funcdesc}
Fred Drake1c127e71998-04-28 14:28:57 +000043
44\begin{funcdesc}{isleap}{year}
Fred Drakeceb1fd22003-12-31 04:51:56 +000045Returns \constant{True} if \var{year} is a leap year, otherwise
46\constant{False}.
Fred Drake1c127e71998-04-28 14:28:57 +000047\end{funcdesc}
48
Skip Montanaro7b828a62000-08-30 14:02:25 +000049\begin{funcdesc}{leapdays}{y1, y2}
50Returns the number of leap years in the range
Fred Drake1529ef82001-12-12 05:40:46 +000051[\var{y1}\ldots\var{y2}), where \var{y1} and \var{y2} are years.
Fred Drakee9996c62002-06-13 01:34:50 +000052\versionchanged[This function didn't work for ranges spanning
53 a century change in Python 1.5.2]{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +000054\end{funcdesc}
55
56\begin{funcdesc}{weekday}{year, month, day}
57Returns the day of the week (\code{0} is Monday) for \var{year}
Fred Drake02379fa1998-05-08 15:39:40 +000058(\code{1970}--\ldots), \var{month} (\code{1}--\code{12}), \var{day}
Fred Drake1c127e71998-04-28 14:28:57 +000059(\code{1}--\code{31}).
60\end{funcdesc}
61
Walter Dörwalde2084122004-12-06 07:58:14 +000062\begin{funcdesc}{weekheader}{n}
63Return a header containing abbreviated weekday names. \var{n} specifies
64the width in characters for one weekday.
65\end{funcdesc}
66
Fred Drake1c127e71998-04-28 14:28:57 +000067\begin{funcdesc}{monthrange}{year, month}
68Returns weekday of first day of the month and number of days in month,
69for the specified \var{year} and \var{month}.
70\end{funcdesc}
71
72\begin{funcdesc}{monthcalendar}{year, month}
73Returns a matrix representing a month's calendar. Each row represents
74a week; days outside of the month a represented by zeros.
Skip Montanaro7b828a62000-08-30 14:02:25 +000075Each week begins with Monday unless set by \function{setfirstweekday()}.
Fred Drake1c127e71998-04-28 14:28:57 +000076\end{funcdesc}
77
Skip Montanaro7b828a62000-08-30 14:02:25 +000078\begin{funcdesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
79Prints a month's calendar as returned by \function{month()}.
Fred Drake1c127e71998-04-28 14:28:57 +000080\end{funcdesc}
81
Skip Montanaro7b828a62000-08-30 14:02:25 +000082\begin{funcdesc}{month}{theyear, themonth\optional{, w\optional{, l}}}
83Returns a month's calendar in a multi-line string. If \var{w} is
84provided, it specifies the width of the date columns, which are
85centered. If \var{l} is given, it specifies the number of lines that
86each week will use. Depends on the first weekday as set by
87\function{setfirstweekday()}.
Fred Drakee9996c62002-06-13 01:34:50 +000088\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +000089\end{funcdesc}
90
91\begin{funcdesc}{prcal}{year\optional{, w\optional{, l\optional{c}}}}
92Prints the calendar for an entire year as returned by
93\function{calendar()}.
94\end{funcdesc}
95
96\begin{funcdesc}{calendar}{year\optional{, w\optional{, l\optional{c}}}}
97Returns a 3-column calendar for an entire year as a multi-line string.
98Optional parameters \var{w}, \var{l}, and \var{c} are for date column
99width, lines per week, and number of spaces between month columns,
100respectively. Depends on the first weekday as set by
Skip Montanaro5ff41d12001-08-22 12:43:38 +0000101\function{setfirstweekday()}. The earliest year for which a calendar can
102be generated is platform-dependent.
Fred Drakee9996c62002-06-13 01:34:50 +0000103\versionadded{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000104\end{funcdesc}
Guido van Rossum47274561999-06-09 15:11:58 +0000105
106\begin{funcdesc}{timegm}{tuple}
Fred Drake38e5d272000-04-03 20:13:55 +0000107An unrelated but handy function that takes a time tuple such as
108returned by the \function{gmtime()} function in the \refmodule{time}
Fred Drakec37b65e2001-11-28 07:26:15 +0000109module, and returns the corresponding \UNIX{} timestamp value, assuming
Guido van Rossum47274561999-06-09 15:11:58 +0000110an epoch of 1970, and the POSIX encoding. In fact,
Fred Drake38e5d272000-04-03 20:13:55 +0000111\function{time.gmtime()} and \function{timegm()} are each others' inverse.
Fred Drakee9996c62002-06-13 01:34:50 +0000112\versionadded{2.0}
Guido van Rossum47274561999-06-09 15:11:58 +0000113\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000114
Skip Montanarob80bad42005-01-05 07:13:32 +0000115The \module{calendar} module exports the following data attributes:
116
117\begin{datadesc}{day_name}
118An array that represents the days of the week in the
119current locale.
120\end{datadesc}
121
122\begin{datadesc}{day_abbr}
123An array that represents the abbreviated days of the week
124in the current locale.
125\end{datadesc}
126
127\begin{datadesc}{month_name}
128An array that represents the months of the year in the
129current locale. This follows normal convention
130of January being month number 1, so it has a length of 13 and
131\code{month_name[0]} is the empty string.
132\end{datadesc}
133
134\begin{datadesc}{month_abbr}
135An array that represents the abbreviated months of the year
136in the current locale. This follows normal convention
137of January being month number 1, so it has a length of 13 and
138\code{month_abbr[0]} is the empty string.
139\end{datadesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000140
141\begin{seealso}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000142 \seemodule{datetime}{Object-oriented interface to dates and times
143 with similar functionality to the
144 \refmodule{time} module.}
Fred Drake38e5d272000-04-03 20:13:55 +0000145 \seemodule{time}{Low-level time related functions.}
146\end{seealso}