blob: eb40008c882918633a2be3070ccae58f09a0bf84 [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
27specifying the first day of the week. 0 is Monday, 6 is Sunday.
28
29A \class{Calendar} object provides several method that can
30be used for preparing the calendar data for formatting. This
31class doesn't do any formatting itself. This is the job of
32subclasses.
33\versionadded{2.5}
34
35\begin{methoddesc}{firstweekday}{}
36Return the first day of the week (as specified in the constructor
37or changed via \method{setfirstweekday()}.
38
39\begin{methoddesc}{setfirstweekday}{weekday}
40Set the first day of the week.
41
42\begin{methoddesc}{iterweekdays}{weekday}
43Return an iterator for the week day numbers that will be used
44for one week. The first number from the iterator will be the
45same as the number return by \method{firstweekday()}.
46
47\begin{methoddesc}{itermonthdates}{year, month}
48Return an iterator for the month \var{month} (1-12) in the
49year \var{year}. This iterator will return all days (as
50\class{datetime.date} objects) for the month and all days
51before the start of the month or after the end of the month
52that are required to get a complete week.
53
54\begin{methoddesc}{itermonthdays2}{year, month}
55Return an iterator for the month \var{month} in the year
56\var{year} similar to \method{itermonthdates()}. Days returned
57will be tuple consisting of a day number and a week day
58number.
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
65\begin{methoddesc}{monthdatescalendar}{year, month}
66Return a list of the weeks in the month \var{month} of
67the \var{year} as full weeks. Weeks are lists of seven
68\class{datetime.date} objects.
69
70\begin{methoddesc}{monthdays2calendar}{year, month}
71Return a list of the weeks in the month \var{month} of
72the \var{year} as full weeks. Weeks are lists of seven
73tuples of day numbers and weekday numbers.
74
75\begin{methoddesc}{monthdayscalendar}{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
78day numbers.
79
80\begin{methoddesc}{yeardatescalendar}{year, month\optional{, width}}
81Return the data for the specified year ready for formatting. The return
82value is a list of month rows. Each month row contains upto \var{width}
83months (defaulting to 3). Each month contains between 4 and 6 weeks and
84each week contains 1-7 days. Days are \class{datetime.date} objects.
85
86\begin{methoddesc}{yeardays2calendar}{year, month\optional{, width}}
87Return the data for the specified year ready for formatting (similar to
88\method{yeardatescalendar()}). Entries in the week lists are tuples of
89day numbers and weekday numbers. Day numbers outside this month are zero.
90
91\begin{methoddesc}{yeardayscalendar}{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 day numbers.
94Day numbers outside this month are zero.
95
96
97\begin{classdesc}{TextCalendar}{\optional{firstweekday}}
98This class can be used for generating plain text calendars.
99
100\versionadded{2.5}
101
102\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, w\optional{, l}}}
103Returns a month's calendar in a multi-line string. If \var{w} is
104provided, it specifies the width of the date columns, which are
105centered. If \var{l} is given, it specifies the number of lines that
106each week will use. Depends on the first weekday as set by
107\function{setfirstweekday()}.
108
109\begin{methoddesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
110Prints a month's calendar as returned by \method{formatmonth()}.
111
112\begin{methoddesc}{formatyear}{theyear, themonth\optional{, w\optional{, l\optional{, c\optional{, m}}}}}
113Returns a \var{m}-column calendar for an entire year as a multi-line string.
114Optional parameters \var{w}, \var{l}, and \var{c} are for date column
115width, lines per week, and number of spaces between month columns,
116respectively. Depends on the first weekday as set by
117\method{setfirstweekday()}. The earliest year for which a calendar can
118be generated is platform-dependent.
119
120\begin{methoddesc}{pryear}{theyear\optional{, w\optional{, l\optional{, c\optional{, m}}}}}
121Prints the calendar for an entire year as returned by \method{formatyear()}.
122\end{funcdesc}
123
124
125\begin{classdesc}{HTMLCalendar}{\optional{firstweekday}}
126This class can be used for generating HTML calendars.
127
128\versionadded{2.5}
129
130\begin{methoddesc}{formatmonth}{theyear, themonth\optional{, withyear}}
131Returns a month's calendar as an HTML table. If \var{withyear} is
132true the year will be included in the header, otherwise just the
133monthname will be used.
134
135\begin{methoddesc}{formatyear}{theyear, themonth\optional{, width}}
136Returns a year's calendar as an HTML table. \var{width} (defaulting to 3)
137specifies the number of months per row.
138
139\begin{methoddesc}{formatyearpage}{theyear, themonth\optional{, width\optional{, css\optional{, encoding}}}}
140Returns a year's calendar as an complete HTML page. \var{width}
141(defaulting to 3) specifies the number of months per row. \var{css}
142is the name for the cascading style sheet to be used. \constant{None}
143can be passed, if no style sheet should be used. \var{encoding}
144specifies the encoding to be used for the output (defaulting
145the the system default encoding).
146
147
148For simple text calendars this module provides the following functions.
Neal Norwitz034c7492002-11-03 00:13:42 +0000149
Skip Montanaro7b828a62000-08-30 14:02:25 +0000150\begin{funcdesc}{setfirstweekday}{weekday}
151Sets the weekday (\code{0} is Monday, \code{6} is Sunday) to start
152each week. The values \constant{MONDAY}, \constant{TUESDAY},
153\constant{WEDNESDAY}, \constant{THURSDAY}, \constant{FRIDAY},
154\constant{SATURDAY}, and \constant{SUNDAY} are provided for
155convenience. For example, to set the first weekday to Sunday:
156
157\begin{verbatim}
158import calendar
159calendar.setfirstweekday(calendar.SUNDAY)
160\end{verbatim}
Fred Drakee9996c62002-06-13 01:34:50 +0000161\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000162\end{funcdesc}
163
164\begin{funcdesc}{firstweekday}{}
165Returns the current setting for the weekday to start each week.
Fred Drakee9996c62002-06-13 01:34:50 +0000166\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000167\end{funcdesc}
Fred Drake1c127e71998-04-28 14:28:57 +0000168
169\begin{funcdesc}{isleap}{year}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000170Returns \constant{True} if \var{year} is a leap year, otherwise
171\constant{False}.
Fred Drake1c127e71998-04-28 14:28:57 +0000172\end{funcdesc}
173
Skip Montanaro7b828a62000-08-30 14:02:25 +0000174\begin{funcdesc}{leapdays}{y1, y2}
175Returns the number of leap years in the range
Fred Drake1529ef82001-12-12 05:40:46 +0000176[\var{y1}\ldots\var{y2}), where \var{y1} and \var{y2} are years.
Fred Drakee9996c62002-06-13 01:34:50 +0000177\versionchanged[This function didn't work for ranges spanning
178 a century change in Python 1.5.2]{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000179\end{funcdesc}
180
181\begin{funcdesc}{weekday}{year, month, day}
182Returns the day of the week (\code{0} is Monday) for \var{year}
Fred Drake02379fa1998-05-08 15:39:40 +0000183(\code{1970}--\ldots), \var{month} (\code{1}--\code{12}), \var{day}
Fred Drake1c127e71998-04-28 14:28:57 +0000184(\code{1}--\code{31}).
185\end{funcdesc}
186
Walter Dörwalde2084122004-12-06 07:58:14 +0000187\begin{funcdesc}{weekheader}{n}
188Return a header containing abbreviated weekday names. \var{n} specifies
189the width in characters for one weekday.
190\end{funcdesc}
191
Fred Drake1c127e71998-04-28 14:28:57 +0000192\begin{funcdesc}{monthrange}{year, month}
193Returns weekday of first day of the month and number of days in month,
194for the specified \var{year} and \var{month}.
195\end{funcdesc}
196
197\begin{funcdesc}{monthcalendar}{year, month}
198Returns a matrix representing a month's calendar. Each row represents
199a week; days outside of the month a represented by zeros.
Skip Montanaro7b828a62000-08-30 14:02:25 +0000200Each week begins with Monday unless set by \function{setfirstweekday()}.
Fred Drake1c127e71998-04-28 14:28:57 +0000201\end{funcdesc}
202
Skip Montanaro7b828a62000-08-30 14:02:25 +0000203\begin{funcdesc}{prmonth}{theyear, themonth\optional{, w\optional{, l}}}
204Prints a month's calendar as returned by \function{month()}.
Fred Drake1c127e71998-04-28 14:28:57 +0000205\end{funcdesc}
206
Skip Montanaro7b828a62000-08-30 14:02:25 +0000207\begin{funcdesc}{month}{theyear, themonth\optional{, w\optional{, l}}}
Walter Dörwald58917a62006-03-31 15:26:22 +0000208Returns a month's calendar in a multi-line string using the
209\method{formatmonth} of the \class{TextCalendar} class.
Fred Drakee9996c62002-06-13 01:34:50 +0000210\versionadded{2.0}
Skip Montanaro7b828a62000-08-30 14:02:25 +0000211\end{funcdesc}
212
213\begin{funcdesc}{prcal}{year\optional{, w\optional{, l\optional{c}}}}
214Prints the calendar for an entire year as returned by
215\function{calendar()}.
216\end{funcdesc}
217
218\begin{funcdesc}{calendar}{year\optional{, w\optional{, l\optional{c}}}}
Walter Dörwald58917a62006-03-31 15:26:22 +0000219Returns a 3-column calendar for an entire year as a multi-line string
220using the \method{formatyear} of the \class{TextCalendar} class.
Fred Drakee9996c62002-06-13 01:34:50 +0000221\versionadded{2.0}
Fred Drake1c127e71998-04-28 14:28:57 +0000222\end{funcdesc}
Guido van Rossum47274561999-06-09 15:11:58 +0000223
224\begin{funcdesc}{timegm}{tuple}
Fred Drake38e5d272000-04-03 20:13:55 +0000225An unrelated but handy function that takes a time tuple such as
226returned by the \function{gmtime()} function in the \refmodule{time}
Fred Drakec37b65e2001-11-28 07:26:15 +0000227module, and returns the corresponding \UNIX{} timestamp value, assuming
Guido van Rossum47274561999-06-09 15:11:58 +0000228an epoch of 1970, and the POSIX encoding. In fact,
Fred Drake38e5d272000-04-03 20:13:55 +0000229\function{time.gmtime()} and \function{timegm()} are each others' inverse.
Fred Drakee9996c62002-06-13 01:34:50 +0000230\versionadded{2.0}
Guido van Rossum47274561999-06-09 15:11:58 +0000231\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000232
Skip Montanarob80bad42005-01-05 07:13:32 +0000233The \module{calendar} module exports the following data attributes:
234
235\begin{datadesc}{day_name}
236An array that represents the days of the week in the
237current locale.
238\end{datadesc}
239
240\begin{datadesc}{day_abbr}
241An array that represents the abbreviated days of the week
242in the current locale.
243\end{datadesc}
244
245\begin{datadesc}{month_name}
246An array that represents the months of the year in the
247current locale. This follows normal convention
248of January being month number 1, so it has a length of 13 and
249\code{month_name[0]} is the empty string.
250\end{datadesc}
251
252\begin{datadesc}{month_abbr}
253An array that represents the abbreviated months of the year
254in the current locale. This follows normal convention
255of January being month number 1, so it has a length of 13 and
256\code{month_abbr[0]} is the empty string.
257\end{datadesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000258
259\begin{seealso}
Fred Drakeceb1fd22003-12-31 04:51:56 +0000260 \seemodule{datetime}{Object-oriented interface to dates and times
261 with similar functionality to the
262 \refmodule{time} module.}
Fred Drake38e5d272000-04-03 20:13:55 +0000263 \seemodule{time}{Low-level time related functions.}
264\end{seealso}