blob: b85782a34e695d12bd0d1cf72468bc12411d62cb [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
Walter Dörwald58917a62006-03-31 15:26:22 +000018Most of these functions and classses rely on the \module{datetime}
19module which uses an idealized calendar, the current Gregorian
20calendar indefinitely extended in both directions. This matches
21the definition of the "proleptic Gregorian" calendar in Dershowitz
22and Reingold's book "Calendrical Calculations", where it's the
23base calendar for all computations.
24
25\begin{classdesc}{Calendar}{\optional{firstweekday}}
26Creates a \class{Calendar} object. \var{firstweekday} is an integer
Georg Brandl3bf538f2006-03-31 15:38:44 +000027specifying the first day of the week. \code{0} is Monday (the default),
28\code{6} is Sunday.
Walter Dörwald58917a62006-03-31 15:26:22 +000029
Georg Brandl3bf538f2006-03-31 15:38:44 +000030A \class{Calendar} object provides several methods that can
Walter Dörwald58917a62006-03-31 15:26:22 +000031be used for preparing the calendar data for formatting. This
32class doesn't do any formatting itself. This is the job of
33subclasses.
34\versionadded{2.5}
Georg Brandl3bf538f2006-03-31 15:38:44 +000035\end{classdesc}
36
37\class{Calendar} instances have the following methods:
Walter Dörwald58917a62006-03-31 15:26:22 +000038
39\begin{methoddesc}{firstweekday}{}
40Return the first day of the week (as specified in the constructor
Georg Brandl3bf538f2006-03-31 15:38:44 +000041or changed via \method{setfirstweekday()}).
42\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000043
44\begin{methoddesc}{setfirstweekday}{weekday}
45Set the first day of the week.
Georg Brandl3bf538f2006-03-31 15:38:44 +000046\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000047
48\begin{methoddesc}{iterweekdays}{weekday}
49Return an iterator for the week day numbers that will be used
50for one week. The first number from the iterator will be the
Georg Brandl3bf538f2006-03-31 15:38:44 +000051same as the number returned by \method{firstweekday()}.
52\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000053
54\begin{methoddesc}{itermonthdates}{year, month}
55Return an iterator for the month \var{month} (1-12) in the
56year \var{year}. This iterator will return all days (as
57\class{datetime.date} objects) for the month and all days
58before the start of the month or after the end of the month
59that are required to get a complete week.
Georg Brandl3bf538f2006-03-31 15:38:44 +000060\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000061
62\begin{methoddesc}{itermonthdays2}{year, month}
63Return an iterator for the month \var{month} in the year
64\var{year} similar to \method{itermonthdates()}. Days returned
Georg Brandl3bf538f2006-03-31 15:38:44 +000065will be tuples consisting of a day number and a week day
Walter Dörwald58917a62006-03-31 15:26:22 +000066number.
Georg Brandl3bf538f2006-03-31 15:38:44 +000067\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000068
69\begin{methoddesc}{itermonthdays}{year, month}
70Return an iterator for the month \var{month} in the year
71\var{year} similar to \method{itermonthdates()}. Days returned
72will simply be day numbers.
Georg Brandl3bf538f2006-03-31 15:38:44 +000073\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000074
75\begin{methoddesc}{monthdatescalendar}{year, month}
76Return a list of the weeks in the month \var{month} of
77the \var{year} as full weeks. Weeks are lists of seven
78\class{datetime.date} objects.
Georg Brandl3bf538f2006-03-31 15:38:44 +000079\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000080
81\begin{methoddesc}{monthdays2calendar}{year, month}
82Return a list of the weeks in the month \var{month} of
83the \var{year} as full weeks. Weeks are lists of seven
84tuples of day numbers and weekday numbers.
Georg Brandl3bf538f2006-03-31 15:38:44 +000085\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000086
87\begin{methoddesc}{monthdayscalendar}{year, month}
88Return a list of the weeks in the month \var{month} of
89the \var{year} as full weeks. Weeks are lists of seven
90day numbers.
Georg Brandl3bf538f2006-03-31 15:38:44 +000091\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000092
93\begin{methoddesc}{yeardatescalendar}{year, month\optional{, width}}
94Return the data for the specified year ready for formatting. The return
Georg Brandl3bf538f2006-03-31 15:38:44 +000095value is a list of month rows. Each month row contains up to \var{width}
Walter Dörwald58917a62006-03-31 15:26:22 +000096months (defaulting to 3). Each month contains between 4 and 6 weeks and
Georg Brandl3bf538f2006-03-31 15:38:44 +000097each week contains 1--7 days. Days are \class{datetime.date} objects.
98\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +000099
100\begin{methoddesc}{yeardays2calendar}{year, month\optional{, width}}
101Return the data for the specified year ready for formatting (similar to
102\method{yeardatescalendar()}). Entries in the week lists are tuples of
103day numbers and weekday numbers. Day numbers outside this month are zero.
Georg Brandl3bf538f2006-03-31 15:38:44 +0000104\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000105
106\begin{methoddesc}{yeardayscalendar}{year, month\optional{, width}}
107Return the data for the specified year ready for formatting (similar to
108\method{yeardatescalendar()}). Entries in the week lists are day numbers.
109Day numbers outside this month are zero.
Georg Brandl3bf538f2006-03-31 15:38:44 +0000110\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000111
112
113\begin{classdesc}{TextCalendar}{\optional{firstweekday}}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000114This class can be used to generate plain text calendars.
Walter Dörwald58917a62006-03-31 15:26:22 +0000115
116\versionadded{2.5}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000117\end{classdesc}
118
119\class{TextCalendar} instances have the following methods:
Walter Dörwald58917a62006-03-31 15:26:22 +0000120
121\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, w\optional{, l}}}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000122Return a month's calendar in a multi-line string. If \var{w} is
Walter Dörwald58917a62006-03-31 15:26:22 +0000123provided, it specifies the width of the date columns, which are
124centered. If \var{l} is given, it specifies the number of lines that
125each week will use. Depends on the first weekday as set by
126\function{setfirstweekday()}.
Georg Brandl3bf538f2006-03-31 15:38:44 +0000127\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000128
129\begin{methoddesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000130Print a month's calendar as returned by \method{formatmonth()}.
131\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000132
Georg Brandl3bf538f2006-03-31 15:38:44 +0000133\begin{methoddesc}{formatyear}{theyear, themonth\optional{, w\optional{,
134 l\optional{, c\optional{, m}}}}}
135Return a \var{m}-column calendar for an entire year as a multi-line string.
Walter Dörwald58917a62006-03-31 15:26:22 +0000136Optional parameters \var{w}, \var{l}, and \var{c} are for date column
137width, lines per week, and number of spaces between month columns,
138respectively. Depends on the first weekday as set by
139\method{setfirstweekday()}. The earliest year for which a calendar can
140be generated is platform-dependent.
Georg Brandl3bf538f2006-03-31 15:38:44 +0000141\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000142
Georg Brandl3bf538f2006-03-31 15:38:44 +0000143\begin{methoddesc}{pryear}{theyear\optional{, w\optional{, l\optional{,
144 c\optional{, m}}}}}
145Print the calendar for an entire year as returned by \method{formatyear()}.
146\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000147
148
149\begin{classdesc}{HTMLCalendar}{\optional{firstweekday}}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000150This class can be used to generate HTML calendars.
Walter Dörwald58917a62006-03-31 15:26:22 +0000151
152\versionadded{2.5}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000153\end{classdesc}
154
155\class{HTMLCalendar} instances have the following methods:
Walter Dörwald58917a62006-03-31 15:26:22 +0000156
157\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, withyear}}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000158Return a month's calendar as an HTML table. If \var{withyear} is
Walter Dörwald58917a62006-03-31 15:26:22 +0000159true the year will be included in the header, otherwise just the
Georg Brandl3bf538f2006-03-31 15:38:44 +0000160month name will be used.
161\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000162
163\begin{methoddesc}{formatyear}{theyear, themonth\optional{, width}}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000164Return a year's calendar as an HTML table. \var{width} (defaulting to 3)
Walter Dörwald58917a62006-03-31 15:26:22 +0000165specifies the number of months per row.
Georg Brandl3bf538f2006-03-31 15:38:44 +0000166\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000167
Georg Brandl3bf538f2006-03-31 15:38:44 +0000168\begin{methoddesc}{formatyearpage}{theyear, themonth\optional{,
169 width\optional{, css\optional{, encoding}}}}
170Return a year's calendar as a complete HTML page. \var{width}
Walter Dörwald58917a62006-03-31 15:26:22 +0000171(defaulting to 3) specifies the number of months per row. \var{css}
172is the name for the cascading style sheet to be used. \constant{None}
Georg Brandl3bf538f2006-03-31 15:38:44 +0000173can be passed if no style sheet should be used. \var{encoding}
Walter Dörwald58917a62006-03-31 15:26:22 +0000174specifies the encoding to be used for the output (defaulting
Georg Brandl3bf538f2006-03-31 15:38:44 +0000175to the system default encoding).
176\end{methoddesc}
Walter Dörwald58917a62006-03-31 15:26:22 +0000177
178
179For simple text calendars this module provides the following functions.
Neal Norwitz034c7492002-11-03 00:13:42 +0000180
Skip Montanaro7b828a62000-08-30 14:02:25 +0000181\begin{funcdesc}{setfirstweekday}{weekday}
182Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start
183each week. The values \constant{MONDAY}, \constant{TUESDAY},
184\constant{WEDNESDAY}, \constant{THURSDAY}, \constant{FRIDAY},
185\constant{SATURDAY}, and \constant{SUNDAY} are provided for
186convenience. For example, to set the first weekday to Sunday:
187
188\begin{verbatim}
189import calendar
190calendar.setfirstweekday(calendar.SUNDAY)
191\end{verbatim}
Fred Drakee9996c62002-06-13 01:34:50 +0000192\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000193\end{funcdesc}
194
195\begin{funcdesc}{firstweekday}{}
196Returns the current setting for the weekday to start each week.
Fred Drakee9996c62002-06-13 01:34:50 +0000197\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000198\end{funcdesc}
Fred Drake1c127e71998-04-28 14:28:57 +0000199
200\begin{funcdesc}{isleap}{year}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000201Returns \constant{True} if \var{year} is a leap year, otherwise
202\constant{False}.
Fred Drake1c127e71998-04-28 14:28:57 +0000203\end{funcdesc}
204
Skip Montanaro7b828a62000-08-30 14:02:25 +0000205\begin{funcdesc}{leapdays}{y1, y2}
206Returns the number of leap years in the range
Fred Drake1529ef82001-12-12 05:40:46 +0000207[\var{y1}\ldots\var{y2}), where \var{y1} and \var{y2} are years.
Fred Drakee9996c62002-06-13 01:34:50 +0000208\versionchanged[This function didn't work for ranges spanning
209 a century change in Python 1.5.2]{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000210\end{funcdesc}
211
212\begin{funcdesc}{weekday}{year, month, day}
213Returns the day of the week (\code{0} is Monday) for \var{year}
Fred Drake02379fa1998-05-08 15:39:40 +0000214(\code{1970}--\ldots), \var{month} (\code{1}--\code{12}), \var{day}
Fred Drake1c127e71998-04-28 14:28:57 +0000215(\code{1}--\code{31}).
216\end{funcdesc}
217
Walter Dörwalde2084122004-12-06 07:58:14 +0000218\begin{funcdesc}{weekheader}{n}
219Return a header containing abbreviated weekday names. \var{n} specifies
220the width in characters for one weekday.
221\end{funcdesc}
222
Fred Drake1c127e71998-04-28 14:28:57 +0000223\begin{funcdesc}{monthrange}{year, month}
224Returns weekday of first day of the month and number of days in month,
225for the specified \var{year} and \var{month}.
226\end{funcdesc}
227
228\begin{funcdesc}{monthcalendar}{year, month}
229Returns a matrix representing a month's calendar. Each row represents
230a week; days outside of the month a represented by zeros.
Skip Montanaro7b828a62000-08-30 14:02:25 +0000231Each week begins with Monday unless set by \function{setfirstweekday()}.
Fred Drake1c127e71998-04-28 14:28:57 +0000232\end{funcdesc}
233
Skip Montanaro7b828a62000-08-30 14:02:25 +0000234\begin{funcdesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
235Prints a month's calendar as returned by \function{month()}.
Fred Drake1c127e71998-04-28 14:28:57 +0000236\end{funcdesc}
237
Skip Montanaro7b828a62000-08-30 14:02:25 +0000238\begin{funcdesc}{month}{theyear, themonth\optional{, w\optional{, l}}}
Walter Dörwald58917a62006-03-31 15:26:22 +0000239Returns a month's calendar in a multi-line string using the
240\method{formatmonth} of the \class{TextCalendar} class.
Fred Drakee9996c62002-06-13 01:34:50 +0000241\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000242\end{funcdesc}
243
244\begin{funcdesc}{prcal}{year\optional{, w\optional{, l\optional{c}}}}
245Prints the calendar for an entire year as returned by
246\function{calendar()}.
247\end{funcdesc}
248
249\begin{funcdesc}{calendar}{year\optional{, w\optional{, l\optional{c}}}}
Walter Dörwald58917a62006-03-31 15:26:22 +0000250Returns a 3-column calendar for an entire year as a multi-line string
251using the \method{formatyear} of the \class{TextCalendar} class.
Fred Drakee9996c62002-06-13 01:34:50 +0000252\versionadded{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000253\end{funcdesc}
Guido van Rossum47274561999-06-09 15:11:58 +0000254
255\begin{funcdesc}{timegm}{tuple}
Fred Drake38e5d272000-04-03 20:13:55 +0000256An unrelated but handy function that takes a time tuple such as
257returned by the \function{gmtime()} function in the \refmodule{time}
Fred Drakec37b65e2001-11-28 07:26:15 +0000258module, and returns the corresponding \UNIX{} timestamp value, assuming
Guido van Rossum47274561999-06-09 15:11:58 +0000259an epoch of 1970, and the POSIX encoding. In fact,
Fred Drake38e5d272000-04-03 20:13:55 +0000260\function{time.gmtime()} and \function{timegm()} are each others' inverse.
Fred Drakee9996c62002-06-13 01:34:50 +0000261\versionadded{2.0}
Guido van Rossum47274561999-06-09 15:11:58 +0000262\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000263
Skip Montanarob80bad42005-01-05 07:13:32 +0000264The \module{calendar} module exports the following data attributes:
265
266\begin{datadesc}{day_name}
267An array that represents the days of the week in the
268current locale.
269\end{datadesc}
270
271\begin{datadesc}{day_abbr}
272An array that represents the abbreviated days of the week
273in the current locale.
274\end{datadesc}
275
276\begin{datadesc}{month_name}
277An array that represents the months of the year in the
278current locale. This follows normal convention
279of January being month number 1, so it has a length of 13 and
280\code{month_name[0]} is the empty string.
281\end{datadesc}
282
283\begin{datadesc}{month_abbr}
284An array that represents the abbreviated months of the year
285in the current locale. This follows normal convention
286of January being month number 1, so it has a length of 13 and
287\code{month_abbr[0]} is the empty string.
288\end{datadesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000289
290\begin{seealso}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000291 \seemodule{datetime}{Object-oriented interface to dates and times
292 with similar functionality to the
293 \refmodule{time} module.}
Fred Drake38e5d272000-04-03 20:13:55 +0000294 \seemodule{time}{Low-level time related functions.}
295\end{seealso}