blob: 2228920fd4459b0e7324c6891c337cb2ac37a277 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`calendar` --- General calendar-related functions
2======================================================
3
4.. module:: calendar
Georg Brandl0d8f0732009-04-05 22:20:44 +00005 :synopsis: Functions for working with calendars, including some emulation
6 of the Unix cal program.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
8
9
10This module allows you to output calendars like the Unix :program:`cal` program,
11and provides additional useful functions related to the calendar. By default,
12these calendars have Monday as the first day of the week, and Sunday as the last
13(the European convention). Use :func:`setfirstweekday` to set the first day of
14the week to Sunday (6) or to any other weekday. Parameters that specify dates
15are given as integers. For related
16functionality, see also the :mod:`datetime` and :mod:`time` modules.
17
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000018Most of these functions and classes rely on the :mod:`datetime` module which
Georg Brandl116aa622007-08-15 14:28:22 +000019uses an idealized calendar, the current Gregorian calendar indefinitely extended
20in both directions. This matches the definition of the "proleptic Gregorian"
21calendar in Dershowitz and Reingold's book "Calendrical Calculations", where
22it's the base calendar for all computations.
23
24
Georg Brandl0d8f0732009-04-05 22:20:44 +000025.. class:: Calendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +000026
27 Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
28 first day of the week. ``0`` is Monday (the default), ``6`` is Sunday.
29
30 A :class:`Calendar` object provides several methods that can be used for
31 preparing the calendar data for formatting. This class doesn't do any formatting
32 itself. This is the job of subclasses.
33
Georg Brandl116aa622007-08-15 14:28:22 +000034
Benjamin Petersone41251e2008-04-25 01:59:09 +000035 :class:`Calendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000036
Alexandre Vassalottibee32532008-05-16 18:15:12 +000037 .. method:: iterweekdays()
Georg Brandl116aa622007-08-15 14:28:22 +000038
Benjamin Petersone41251e2008-04-25 01:59:09 +000039 Return an iterator for the week day numbers that will be used for one
40 week. The first value from the iterator will be the same as the value of
41 the :attr:`firstweekday` property.
Georg Brandl116aa622007-08-15 14:28:22 +000042
43
Benjamin Petersone41251e2008-04-25 01:59:09 +000044 .. method:: itermonthdates(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000045
Benjamin Petersone41251e2008-04-25 01:59:09 +000046 Return an iterator for the month *month* (1-12) in the year *year*. This
47 iterator will return all days (as :class:`datetime.date` objects) for the
48 month and all days before the start of the month or after the end of the
49 month that are required to get a complete week.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
Benjamin Petersone41251e2008-04-25 01:59:09 +000052 .. method:: itermonthdays2(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000053
Benjamin Petersone41251e2008-04-25 01:59:09 +000054 Return an iterator for the month *month* in the year *year* similar to
55 :meth:`itermonthdates`. Days returned will be tuples consisting of a day
56 number and a week day number.
Georg Brandl116aa622007-08-15 14:28:22 +000057
58
Benjamin Petersone41251e2008-04-25 01:59:09 +000059 .. method:: itermonthdays(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000060
Benjamin Petersone41251e2008-04-25 01:59:09 +000061 Return an iterator for the month *month* in the year *year* similar to
62 :meth:`itermonthdates`. Days returned will simply be day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
Benjamin Petersone41251e2008-04-25 01:59:09 +000065 .. method:: monthdatescalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000066
Benjamin Petersone41251e2008-04-25 01:59:09 +000067 Return a list of the weeks in the month *month* of the *year* as full
68 weeks. Weeks are lists of seven :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000069
70
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 .. method:: monthdays2calendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000072
Benjamin Petersone41251e2008-04-25 01:59:09 +000073 Return a list of the weeks in the month *month* of the *year* as full
74 weeks. Weeks are lists of seven tuples of day numbers and weekday
75 numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 .. method:: monthdayscalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000079
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 Return a list of the weeks in the month *month* of the *year* as full
81 weeks. Weeks are lists of seven day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Georg Brandl0d8f0732009-04-05 22:20:44 +000084 .. method:: yeardatescalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 Return the data for the specified year ready for formatting. The return
87 value is a list of month rows. Each month row contains up to *width*
88 months (defaulting to 3). Each month contains between 4 and 6 weeks and
89 each week contains 1--7 days. Days are :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000090
91
Georg Brandl0d8f0732009-04-05 22:20:44 +000092 .. method:: yeardays2calendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +000093
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 Return the data for the specified year ready for formatting (similar to
95 :meth:`yeardatescalendar`). Entries in the week lists are tuples of day
96 numbers and weekday numbers. Day numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
Georg Brandl0d8f0732009-04-05 22:20:44 +000099 .. method:: yeardayscalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 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.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
Georg Brandl0d8f0732009-04-05 22:20:44 +0000106.. class:: TextCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108 This class can be used to generate plain text calendars.
109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 :class:`TextCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Georg Brandl0d8f0732009-04-05 22:20:44 +0000112 .. method:: formatmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 Return a month's calendar in a multi-line string. If *w* is provided, it
115 specifies the width of the date columns, which are centered. If *l* is
116 given, it specifies the number of lines that each week will use. Depends
117 on the first weekday as specified in the constructor or set by the
118 :meth:`setfirstweekday` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
Georg Brandl0d8f0732009-04-05 22:20:44 +0000121 .. method:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 Print a month's calendar as returned by :meth:`formatmonth`.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
Benjamin Peterson20211002009-11-25 18:34:42 +0000126 .. method:: formatyear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 Return a *m*-column calendar for an entire year as a multi-line string.
129 Optional parameters *w*, *l*, and *c* are for date column width, lines per
130 week, and number of spaces between month columns, respectively. Depends on
131 the first weekday as specified in the constructor or set by the
132 :meth:`setfirstweekday` method. The earliest year for which a calendar
133 can be generated is platform-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
Georg Brandl0d8f0732009-04-05 22:20:44 +0000136 .. method:: pryear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 Print the calendar for an entire year as returned by :meth:`formatyear`.
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140
Georg Brandl0d8f0732009-04-05 22:20:44 +0000141.. class:: HTMLCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143 This class can be used to generate HTML calendars.
144
Georg Brandl116aa622007-08-15 14:28:22 +0000145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 :class:`HTMLCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Georg Brandl0d8f0732009-04-05 22:20:44 +0000148 .. method:: formatmonth(theyear, themonth, withyear=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 Return a month's calendar as an HTML table. If *withyear* is true the year
151 will be included in the header, otherwise just the month name will be
152 used.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
Benjamin Peterson20211002009-11-25 18:34:42 +0000155 .. method:: formatyear(theyear, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Benjamin Petersone41251e2008-04-25 01:59:09 +0000157 Return a year's calendar as an HTML table. *width* (defaulting to 3)
158 specifies the number of months per row.
Georg Brandl116aa622007-08-15 14:28:22 +0000159
160
Georg Brandl0d8f0732009-04-05 22:20:44 +0000161 .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Benjamin Petersone41251e2008-04-25 01:59:09 +0000163 Return a year's calendar as a complete HTML page. *width* (defaulting to
164 3) specifies the number of months per row. *css* is the name for the
165 cascading style sheet to be used. :const:`None` can be passed if no style
166 sheet should be used. *encoding* specifies the encoding to be used for the
167 output (defaulting to the system default encoding).
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169
Georg Brandl0d8f0732009-04-05 22:20:44 +0000170.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172 This subclass of :class:`TextCalendar` can be passed a locale name in the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000173 constructor and will return month and weekday names in the specified
174 locale. If this locale includes an encoding all strings containing month and
175 weekday names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Georg Brandl0d8f0732009-04-05 22:20:44 +0000178.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000179
180 This subclass of :class:`HTMLCalendar` can be passed a locale name in the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000181 constructor and will return month and weekday names in the specified
182 locale. If this locale includes an encoding all strings containing month and
183 weekday names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186For simple text calendars this module provides the following functions.
187
Georg Brandl116aa622007-08-15 14:28:22 +0000188.. function:: setfirstweekday(weekday)
189
190 Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The
191 values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`,
192 :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for
193 convenience. For example, to set the first weekday to Sunday::
194
195 import calendar
196 calendar.setfirstweekday(calendar.SUNDAY)
197
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199.. function:: firstweekday()
200
201 Returns the current setting for the weekday to start each week.
202
Georg Brandl116aa622007-08-15 14:28:22 +0000203
204.. function:: isleap(year)
205
206 Returns :const:`True` if *year* is a leap year, otherwise :const:`False`.
207
208
209.. function:: leapdays(y1, y2)
210
211 Returns the number of leap years in the range from *y1* to *y2* (exclusive),
212 where *y1* and *y2* are years.
213
Georg Brandl55ac8f02007-09-01 13:51:09 +0000214 This function works for ranges spanning a century change.
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216
217.. function:: weekday(year, month, day)
218
219 Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
220 *month* (``1``--``12``), *day* (``1``--``31``).
221
222
223.. function:: weekheader(n)
224
225 Return a header containing abbreviated weekday names. *n* specifies the width in
226 characters for one weekday.
227
228
229.. function:: monthrange(year, month)
230
231 Returns weekday of first day of the month and number of days in month, for the
232 specified *year* and *month*.
233
234
235.. function:: monthcalendar(year, month)
236
237 Returns a matrix representing a month's calendar. Each row represents a week;
238 days outside of the month a represented by zeros. Each week begins with Monday
239 unless set by :func:`setfirstweekday`.
240
241
Georg Brandl0d8f0732009-04-05 22:20:44 +0000242.. function:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244 Prints a month's calendar as returned by :func:`month`.
245
246
Georg Brandl0d8f0732009-04-05 22:20:44 +0000247.. function:: month(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249 Returns a month's calendar in a multi-line string using the :meth:`formatmonth`
250 of the :class:`TextCalendar` class.
251
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Georg Brandl0d8f0732009-04-05 22:20:44 +0000253.. function:: prcal(year, w=0, l=0, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255 Prints the calendar for an entire year as returned by :func:`calendar`.
256
257
Georg Brandl0d8f0732009-04-05 22:20:44 +0000258.. function:: calendar(year, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Georg Brandl0d8f0732009-04-05 22:20:44 +0000260 Returns a 3-column calendar for an entire year as a multi-line string using
261 the :meth:`formatyear` of the :class:`TextCalendar` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264.. function:: timegm(tuple)
265
266 An unrelated but handy function that takes a time tuple such as returned by the
267 :func:`gmtime` function in the :mod:`time` module, and returns the corresponding
268 Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In
269 fact, :func:`time.gmtime` and :func:`timegm` are each others' inverse.
270
Georg Brandl116aa622007-08-15 14:28:22 +0000271
272The :mod:`calendar` module exports the following data attributes:
273
Georg Brandl116aa622007-08-15 14:28:22 +0000274.. data:: day_name
275
276 An array that represents the days of the week in the current locale.
277
278
279.. data:: day_abbr
280
281 An array that represents the abbreviated days of the week in the current locale.
282
283
284.. data:: month_name
285
286 An array that represents the months of the year in the current locale. This
287 follows normal convention of January being month number 1, so it has a length of
288 13 and ``month_name[0]`` is the empty string.
289
290
291.. data:: month_abbr
292
293 An array that represents the abbreviated months of the year in the current
294 locale. This follows normal convention of January being month number 1, so it
295 has a length of 13 and ``month_abbr[0]`` is the empty string.
296
297
298.. seealso::
299
300 Module :mod:`datetime`
301 Object-oriented interface to dates and times with similar functionality to the
302 :mod:`time` module.
303
304 Module :mod:`time`
305 Low-level time related functions.
306