blob: e125cccaee3005655dc899d6c30605815c44c180 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`calendar` --- General calendar-related functions
3======================================================
4
5.. module:: calendar
6 :synopsis: Functions for working with calendars, including some emulation of the Unix cal
7 program.
8.. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
9
10
11This module allows you to output calendars like the Unix :program:`cal` program,
12and provides additional useful functions related to the calendar. By default,
13these calendars have Monday as the first day of the week, and Sunday as the last
14(the European convention). Use :func:`setfirstweekday` to set the first day of
15the week to Sunday (6) or to any other weekday. Parameters that specify dates
16are given as integers. For related
17functionality, see also the :mod:`datetime` and :mod:`time` modules.
18
19Most of these functions and classses rely on the :mod:`datetime` module which
20uses an idealized calendar, the current Gregorian calendar indefinitely extended
21in both directions. This matches the definition of the "proleptic Gregorian"
22calendar in Dershowitz and Reingold's book "Calendrical Calculations", where
23it's the base calendar for all computations.
24
25
26.. class:: Calendar([firstweekday])
27
28 Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
29 first day of the week. ``0`` is Monday (the default), ``6`` is Sunday.
30
31 A :class:`Calendar` object provides several methods that can be used for
32 preparing the calendar data for formatting. This class doesn't do any formatting
33 itself. This is the job of subclasses.
34
Georg Brandl116aa622007-08-15 14:28:22 +000035
36:class:`Calendar` instances have the following methods:
37
Georg Brandl116aa622007-08-15 14:28:22 +000038.. method:: Calendar.iterweekdays(weekday)
39
40 Return an iterator for the week day numbers that will be used for one week. The
41 first number from the iterator will be the same as the number returned by
42 :meth:`firstweekday`.
43
44
45.. method:: Calendar.itermonthdates(year, month)
46
47 Return an iterator for the month *month* (1-12) in the year *year*. This
48 iterator will return all days (as :class:`datetime.date` objects) for the month
49 and all days before the start of the month or after the end of the month that
50 are required to get a complete week.
51
52
53.. method:: Calendar.itermonthdays2(year, month)
54
55 Return an iterator for the month *month* in the year *year* similar to
56 :meth:`itermonthdates`. Days returned will be tuples consisting of a day number
57 and a week day number.
58
59
60.. method:: Calendar.itermonthdays(year, month)
61
62 Return an iterator for the month *month* in the year *year* similar to
63 :meth:`itermonthdates`. Days returned will simply be day numbers.
64
65
66.. method:: Calendar.monthdatescalendar(year, month)
67
68 Return a list of the weeks in the month *month* of the *year* as full weeks.
69 Weeks are lists of seven :class:`datetime.date` objects.
70
71
72.. method:: Calendar.monthdays2calendar(year, month)
73
74 Return a list of the weeks in the month *month* of the *year* as full weeks.
75 Weeks are lists of seven tuples of day numbers and weekday numbers.
76
77
78.. method:: Calendar.monthdayscalendar(year, month)
79
80 Return a list of the weeks in the month *month* of the *year* as full weeks.
81 Weeks are lists of seven day numbers.
82
83
84.. method:: Calendar.yeardatescalendar(year, month[, width])
85
86 Return the data for the specified year ready for formatting. The return value is
87 a list of month rows. Each month row contains up to *width* months (defaulting
88 to 3). Each month contains between 4 and 6 weeks and each week contains 1--7
89 days. Days are :class:`datetime.date` objects.
90
91
92.. method:: Calendar.yeardays2calendar(year, month[, width])
93
94 Return the data for the specified year ready for formatting (similar to
95 :meth:`yeardatescalendar`). Entries in the week lists are tuples of day numbers
96 and weekday numbers. Day numbers outside this month are zero.
97
98
99.. method:: Calendar.yeardayscalendar(year, month[, width])
100
101 Return the data for the specified year ready for formatting (similar to
102 :meth:`yeardatescalendar`). Entries in the week lists are day numbers. Day
103 numbers outside this month are zero.
104
105
106.. class:: TextCalendar([firstweekday])
107
108 This class can be used to generate plain text calendars.
109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111:class:`TextCalendar` instances have the following methods:
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113.. method:: TextCalendar.formatmonth(theyear, themonth[, w[, l]])
114
115 Return a month's calendar in a multi-line string. If *w* is provided, it
116 specifies the width of the date columns, which are centered. If *l* is given, it
117 specifies the number of lines that each week will use. Depends on the first
118 weekday as set by :func:`setfirstweekday`.
119
120
121.. method:: TextCalendar.prmonth(theyear, themonth[, w[, l]])
122
123 Print a month's calendar as returned by :meth:`formatmonth`.
124
125
126.. method:: TextCalendar.formatyear(theyear, themonth[, w[, l[, c[, m]]]])
127
128 Return a *m*-column calendar for an entire year as a multi-line string. Optional
129 parameters *w*, *l*, and *c* are for date column width, lines per week, and
130 number of spaces between month columns, respectively. Depends on the first
131 weekday as set by :meth:`setfirstweekday`. The earliest year for which a
132 calendar can be generated is platform-dependent.
133
134
135.. method:: TextCalendar.pryear(theyear[, w[, l[, c[, m]]]])
136
137 Print the calendar for an entire year as returned by :meth:`formatyear`.
138
139
140.. class:: HTMLCalendar([firstweekday])
141
142 This class can be used to generate HTML calendars.
143
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145:class:`HTMLCalendar` instances have the following methods:
146
Georg Brandl116aa622007-08-15 14:28:22 +0000147.. method:: HTMLCalendar.formatmonth(theyear, themonth[, withyear])
148
149 Return a month's calendar as an HTML table. If *withyear* is true the year will
150 be included in the header, otherwise just the month name will be used.
151
152
153.. method:: HTMLCalendar.formatyear(theyear, themonth[, width])
154
155 Return a year's calendar as an HTML table. *width* (defaulting to 3) specifies
156 the number of months per row.
157
158
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000159.. method:: HTMLCalendar.formatyearpage(theyear[, width[, css[, encoding]]])
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161 Return a year's calendar as a complete HTML page. *width* (defaulting to 3)
162 specifies the number of months per row. *css* is the name for the cascading
163 style sheet to be used. :const:`None` can be passed if no style sheet should be
164 used. *encoding* specifies the encoding to be used for the output (defaulting to
165 the system default encoding).
166
167
168.. class:: LocaleTextCalendar([firstweekday[, locale]])
169
170 This subclass of :class:`TextCalendar` can be passed a locale name in the
171 constructor and will return month and weekday names in the specified locale. If
172 this locale includes an encoding all strings containing month and weekday names
173 will be returned as unicode.
174
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176.. class:: LocaleHTMLCalendar([firstweekday[, locale]])
177
178 This subclass of :class:`HTMLCalendar` can be passed a locale name in the
179 constructor and will return month and weekday names in the specified locale. If
180 this locale includes an encoding all strings containing month and weekday names
181 will be returned as unicode.
182
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184For simple text calendars this module provides the following functions.
185
Georg Brandl116aa622007-08-15 14:28:22 +0000186.. function:: setfirstweekday(weekday)
187
188 Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The
189 values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`,
190 :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for
191 convenience. For example, to set the first weekday to Sunday::
192
193 import calendar
194 calendar.setfirstweekday(calendar.SUNDAY)
195
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197.. function:: firstweekday()
198
199 Returns the current setting for the weekday to start each week.
200
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202.. function:: isleap(year)
203
204 Returns :const:`True` if *year* is a leap year, otherwise :const:`False`.
205
206
207.. function:: leapdays(y1, y2)
208
209 Returns the number of leap years in the range from *y1* to *y2* (exclusive),
210 where *y1* and *y2* are years.
211
Georg Brandl55ac8f02007-09-01 13:51:09 +0000212 This function works for ranges spanning a century change.
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214
215.. function:: weekday(year, month, day)
216
217 Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
218 *month* (``1``--``12``), *day* (``1``--``31``).
219
220
221.. function:: weekheader(n)
222
223 Return a header containing abbreviated weekday names. *n* specifies the width in
224 characters for one weekday.
225
226
227.. function:: monthrange(year, month)
228
229 Returns weekday of first day of the month and number of days in month, for the
230 specified *year* and *month*.
231
232
233.. function:: monthcalendar(year, month)
234
235 Returns a matrix representing a month's calendar. Each row represents a week;
236 days outside of the month a represented by zeros. Each week begins with Monday
237 unless set by :func:`setfirstweekday`.
238
239
240.. function:: prmonth(theyear, themonth[, w[, l]])
241
242 Prints a month's calendar as returned by :func:`month`.
243
244
245.. function:: month(theyear, themonth[, w[, l]])
246
247 Returns a month's calendar in a multi-line string using the :meth:`formatmonth`
248 of the :class:`TextCalendar` class.
249
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251.. function:: prcal(year[, w[, l[c]]])
252
253 Prints the calendar for an entire year as returned by :func:`calendar`.
254
255
256.. function:: calendar(year[, w[, l[c]]])
257
258 Returns a 3-column calendar for an entire year as a multi-line string using the
259 :meth:`formatyear` of the :class:`TextCalendar` class.
260
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262.. function:: timegm(tuple)
263
264 An unrelated but handy function that takes a time tuple such as returned by the
265 :func:`gmtime` function in the :mod:`time` module, and returns the corresponding
266 Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In
267 fact, :func:`time.gmtime` and :func:`timegm` are each others' inverse.
268
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270The :mod:`calendar` module exports the following data attributes:
271
Georg Brandl116aa622007-08-15 14:28:22 +0000272.. data:: day_name
273
274 An array that represents the days of the week in the current locale.
275
276
277.. data:: day_abbr
278
279 An array that represents the abbreviated days of the week in the current locale.
280
281
282.. data:: month_name
283
284 An array that represents the months of the year in the current locale. This
285 follows normal convention of January being month number 1, so it has a length of
286 13 and ``month_name[0]`` is the empty string.
287
288
289.. data:: month_abbr
290
291 An array that represents the abbreviated months of the year in the current
292 locale. This follows normal convention of January being month number 1, so it
293 has a length of 13 and ``month_abbr[0]`` is the empty string.
294
295
296.. seealso::
297
298 Module :mod:`datetime`
299 Object-oriented interface to dates and times with similar functionality to the
300 :mod:`time` module.
301
302 Module :mod:`time`
303 Low-level time related functions.
304