blob: acfd2da7e230f9287b3e11005505b11872f20e30 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
27specifying the first day of the week. \code{0} is Monday (the default),
28\code{6} is Sunday.
29
30A \class{Calendar} object provides several methods that can
31be 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}
35\end{classdesc}
36
37\class{Calendar} instances have the following methods:
38
39\begin{methoddesc}{iterweekdays}{weekday}
40Return an iterator for the week day numbers that will be used
41for one week. The first number from the iterator will be the
42same as the number returned by \method{firstweekday()}.
43\end{methoddesc}
44
45\begin{methoddesc}{itermonthdates}{year, month}
46Return an iterator for the month \var{month} (1-12) in the
47year \var{year}. This iterator will return all days (as
48\class{datetime.date} objects) for the month and all days
49before the start of the month or after the end of the month
50that are required to get a complete week.
51\end{methoddesc}
52
53\begin{methoddesc}{itermonthdays2}{year, month}
54Return an iterator for the month \var{month} in the year
55\var{year} similar to \method{itermonthdates()}. Days returned
56will be tuples consisting of a day number and a week day
57number.
58\end{methoddesc}
59
60\begin{methoddesc}{itermonthdays}{year, month}
61Return an iterator for the month \var{month} in the year
62\var{year} similar to \method{itermonthdates()}. Days returned
63will simply be day numbers.
64\end{methoddesc}
65
66\begin{methoddesc}{monthdatescalendar}{year, month}
67Return a list of the weeks in the month \var{month} of
68the \var{year} as full weeks. Weeks are lists of seven
69\class{datetime.date} objects.
70\end{methoddesc}
71
72\begin{methoddesc}{monthdays2calendar}{year, month}
73Return a list of the weeks in the month \var{month} of
74the \var{year} as full weeks. Weeks are lists of seven
75tuples of day numbers and weekday numbers.
76\end{methoddesc}
77
78\begin{methoddesc}{monthdayscalendar}{year, month}
79Return a list of the weeks in the month \var{month} of
80the \var{year} as full weeks. Weeks are lists of seven
81day numbers.
82\end{methoddesc}
83
84\begin{methoddesc}{yeardatescalendar}{year, month\optional{, width}}
85Return the data for the specified year ready for formatting. The return
86value is a list of month rows. Each month row contains up to \var{width}
87months (defaulting to 3). Each month contains between 4 and 6 weeks and
88each week contains 1--7 days. Days are \class{datetime.date} objects.
89\end{methoddesc}
90
91\begin{methoddesc}{yeardays2calendar}{year, month\optional{, width}}
92Return the data for the specified year ready for formatting (similar to
93\method{yeardatescalendar()}). Entries in the week lists are tuples of
94day numbers and weekday numbers. Day numbers outside this month are zero.
95\end{methoddesc}
96
97\begin{methoddesc}{yeardayscalendar}{year, month\optional{, width}}
98Return the data for the specified year ready for formatting (similar to
99\method{yeardatescalendar()}). Entries in the week lists are day numbers.
100Day numbers outside this month are zero.
101\end{methoddesc}
102
103
104\begin{classdesc}{TextCalendar}{\optional{firstweekday}}
105This class can be used to generate plain text calendars.
106
107\versionadded{2.5}
108\end{classdesc}
109
110\class{TextCalendar} instances have the following methods:
111
112\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, w\optional{, l}}}
113Return a month's calendar in a multi-line string. If \var{w} is
114provided, it specifies the width of the date columns, which are
115centered. If \var{l} is given, it specifies the number of lines that
116each week will use. Depends on the first weekday as set by
117\function{setfirstweekday()}.
118\end{methoddesc}
119
120\begin{methoddesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
121Print a month's calendar as returned by \method{formatmonth()}.
122\end{methoddesc}
123
124\begin{methoddesc}{formatyear}{theyear, themonth\optional{, w\optional{,
125 l\optional{, c\optional{, m}}}}}
126Return a \var{m}-column calendar for an entire year as a multi-line string.
127Optional parameters \var{w}, \var{l}, and \var{c} are for date column
128width, lines per week, and number of spaces between month columns,
129respectively. Depends on the first weekday as set by
130\method{setfirstweekday()}. The earliest year for which a calendar can
131be generated is platform-dependent.
132\end{methoddesc}
133
134\begin{methoddesc}{pryear}{theyear\optional{, w\optional{, l\optional{,
135 c\optional{, m}}}}}
136Print the calendar for an entire year as returned by \method{formatyear()}.
137\end{methoddesc}
138
139
140\begin{classdesc}{HTMLCalendar}{\optional{firstweekday}}
141This class can be used to generate HTML calendars.
142
143\versionadded{2.5}
144\end{classdesc}
145
146\class{HTMLCalendar} instances have the following methods:
147
148\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, withyear}}
149Return a month's calendar as an HTML table. If \var{withyear} is
150true the year will be included in the header, otherwise just the
151month name will be used.
152\end{methoddesc}
153
154\begin{methoddesc}{formatyear}{theyear, themonth\optional{, width}}
155Return a year's calendar as an HTML table. \var{width} (defaulting to 3)
156specifies the number of months per row.
157\end{methoddesc}
158
159\begin{methoddesc}{formatyearpage}{theyear, themonth\optional{,
160 width\optional{, css\optional{, encoding}}}}
161Return a year's calendar as a complete HTML page. \var{width}
162(defaulting to 3) specifies the number of months per row. \var{css}
163is the name for the cascading style sheet to be used. \constant{None}
164can be passed if no style sheet should be used. \var{encoding}
165specifies the encoding to be used for the output (defaulting
166to the system default encoding).
167\end{methoddesc}
168
169
170\begin{classdesc}{LocaleTextCalendar}{\optional{firstweekday\optional{, locale}}}
171This subclass of \class{TextCalendar} can be passed a locale name in the
172constructor and will return month and weekday names in the specified locale.
173If this locale includes an encoding all strings containing month and weekday
174names will be returned as unicode.
175\versionadded{2.5}
176\end{classdesc}
177
178
179\begin{classdesc}{LocaleHTMLCalendar}{\optional{firstweekday\optional{, locale}}}
180This subclass of \class{HTMLCalendar} can be passed a locale name in the
181constructor and will return month and weekday names in the specified locale.
182If this locale includes an encoding all strings containing month and weekday
183names will be returned as unicode.
184\versionadded{2.5}
185\end{classdesc}
186
187
188For simple text calendars this module provides the following functions.
Neal Norwitz034c7492002-11-03 00:13:42 +0000189
Skip Montanaro7b828a62000-08-30 14:02:25 +0000190\begin{funcdesc}{setfirstweekday}{weekday}
191Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start
192each week. The values \constant{MONDAY}, \constant{TUESDAY},
193\constant{WEDNESDAY}, \constant{THURSDAY}, \constant{FRIDAY},
194\constant{SATURDAY}, and \constant{SUNDAY} are provided for
195convenience. For example, to set the first weekday to Sunday:
196
197\begin{verbatim}
198import calendar
199calendar.setfirstweekday(calendar.SUNDAY)
200\end{verbatim}
Fred Drakee9996c62002-06-13 01:34:50 +0000201\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000202\end{funcdesc}
203
204\begin{funcdesc}{firstweekday}{}
205Returns the current setting for the weekday to start each week.
Fred Drakee9996c62002-06-13 01:34:50 +0000206\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000207\end{funcdesc}
Fred Drake1c127e71998-04-28 14:28:57 +0000208
209\begin{funcdesc}{isleap}{year}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000210Returns \constant{True} if \var{year} is a leap year, otherwise
211\constant{False}.
Fred Drake1c127e71998-04-28 14:28:57 +0000212\end{funcdesc}
213
Skip Montanaro7b828a62000-08-30 14:02:25 +0000214\begin{funcdesc}{leapdays}{y1, y2}
215Returns the number of leap years in the range
Fred Drake1529ef82001-12-12 05:40:46 +0000216[\var{y1}\ldots\var{y2}), where \var{y1} and \var{y2} are years.
Fred Drakee9996c62002-06-13 01:34:50 +0000217\versionchanged[This function didn't work for ranges spanning
218 a century change in Python 1.5.2]{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000219\end{funcdesc}
220
221\begin{funcdesc}{weekday}{year, month, day}
222Returns the day of the week (\code{0} is Monday) for \var{year}
Fred Drake02379fa1998-05-08 15:39:40 +0000223(\code{1970}--\ldots), \var{month} (\code{1}--\code{12}), \var{day}
Fred Drake1c127e71998-04-28 14:28:57 +0000224(\code{1}--\code{31}).
225\end{funcdesc}
226
Walter Dörwalde2084122004-12-06 07:58:14 +0000227\begin{funcdesc}{weekheader}{n}
228Return a header containing abbreviated weekday names. \var{n} specifies
229the width in characters for one weekday.
230\end{funcdesc}
231
Fred Drake1c127e71998-04-28 14:28:57 +0000232\begin{funcdesc}{monthrange}{year, month}
233Returns weekday of first day of the month and number of days in month,
234for the specified \var{year} and \var{month}.
235\end{funcdesc}
236
237\begin{funcdesc}{monthcalendar}{year, month}
238Returns a matrix representing a month's calendar. Each row represents
239a week; days outside of the month a represented by zeros.
Skip Montanaro7b828a62000-08-30 14:02:25 +0000240Each week begins with Monday unless set by \function{setfirstweekday()}.
Fred Drake1c127e71998-04-28 14:28:57 +0000241\end{funcdesc}
242
Skip Montanaro7b828a62000-08-30 14:02:25 +0000243\begin{funcdesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
244Prints a month's calendar as returned by \function{month()}.
Fred Drake1c127e71998-04-28 14:28:57 +0000245\end{funcdesc}
246
Skip Montanaro7b828a62000-08-30 14:02:25 +0000247\begin{funcdesc}{month}{theyear, themonth\optional{, w\optional{, l}}}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248Returns a month's calendar in a multi-line string using the
249\method{formatmonth} of the \class{TextCalendar} class.
Fred Drakee9996c62002-06-13 01:34:50 +0000250\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000251\end{funcdesc}
252
253\begin{funcdesc}{prcal}{year\optional{, w\optional{, l\optional{c}}}}
254Prints the calendar for an entire year as returned by
255\function{calendar()}.
256\end{funcdesc}
257
258\begin{funcdesc}{calendar}{year\optional{, w\optional{, l\optional{c}}}}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259Returns a 3-column calendar for an entire year as a multi-line string
260using the \method{formatyear} of the \class{TextCalendar} class.
Fred Drakee9996c62002-06-13 01:34:50 +0000261\versionadded{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000262\end{funcdesc}
Guido van Rossum47274561999-06-09 15:11:58 +0000263
264\begin{funcdesc}{timegm}{tuple}
Fred Drake38e5d272000-04-03 20:13:55 +0000265An unrelated but handy function that takes a time tuple such as
266returned by the \function{gmtime()} function in the \refmodule{time}
Fred Drakec37b65e2001-11-28 07:26:15 +0000267module, and returns the corresponding \UNIX{} timestamp value, assuming
Guido van Rossum47274561999-06-09 15:11:58 +0000268an epoch of 1970, and the POSIX encoding. In fact,
Fred Drake38e5d272000-04-03 20:13:55 +0000269\function{time.gmtime()} and \function{timegm()} are each others' inverse.
Fred Drakee9996c62002-06-13 01:34:50 +0000270\versionadded{2.0}
Guido van Rossum47274561999-06-09 15:11:58 +0000271\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000272
Skip Montanarob80bad42005-01-05 07:13:32 +0000273The \module{calendar} module exports the following data attributes:
274
275\begin{datadesc}{day_name}
276An array that represents the days of the week in the
277current locale.
278\end{datadesc}
279
280\begin{datadesc}{day_abbr}
281An array that represents the abbreviated days of the week
282in the current locale.
283\end{datadesc}
284
285\begin{datadesc}{month_name}
286An array that represents the months of the year in the
287current locale. This follows normal convention
288of January being month number 1, so it has a length of 13 and
289\code{month_name[0]} is the empty string.
290\end{datadesc}
291
292\begin{datadesc}{month_abbr}
293An array that represents the abbreviated months of the year
294in the current locale. This follows normal convention
295of January being month number 1, so it has a length of 13 and
296\code{month_abbr[0]} is the empty string.
297\end{datadesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000298
299\begin{seealso}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000300 \seemodule{datetime}{Object-oriented interface to dates and times
301 with similar functionality to the
302 \refmodule{time} module.}
Fred Drake38e5d272000-04-03 20:13:55 +0000303 \seemodule{time}{Low-level time related functions.}
304\end{seealso}