blob: 06a12cdd8f5def45837f6cbb3808ddea7baf43e9 [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
Raymond Hettinger10480942011-01-10 03:26:08 +00009**Source code:** :source:`Lib/calendar.py`
Georg Brandl116aa622007-08-15 14:28:22 +000010
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
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000019Most of these functions and classes rely on the :mod:`datetime` module which
Alexander Belopolskyd4bbab22010-11-22 03:06:56 +000020uses an idealized calendar, the current Gregorian calendar extended
Georg Brandl116aa622007-08-15 14:28:22 +000021in 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
Georg Brandl0d8f0732009-04-05 22:20:44 +000026.. class:: Calendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +000027
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
Benjamin Petersone41251e2008-04-25 01:59:09 +000036 :class:`Calendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000037
Alexandre Vassalottibee32532008-05-16 18:15:12 +000038 .. method:: iterweekdays()
Georg Brandl116aa622007-08-15 14:28:22 +000039
Benjamin Petersone41251e2008-04-25 01:59:09 +000040 Return an iterator for the week day numbers that will be used for one
41 week. The first value from the iterator will be the same as the value of
42 the :attr:`firstweekday` property.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44
Benjamin Petersone41251e2008-04-25 01:59:09 +000045 .. method:: itermonthdates(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 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
49 month and all days before the start of the month or after the end of the
50 month that are required to get a complete week.
Georg Brandl116aa622007-08-15 14:28:22 +000051
52
Benjamin Petersone41251e2008-04-25 01:59:09 +000053 .. method:: itermonthdays2(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000054
Benjamin Petersone41251e2008-04-25 01:59:09 +000055 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
57 number and a week day number.
Georg Brandl116aa622007-08-15 14:28:22 +000058
59
Benjamin Petersone41251e2008-04-25 01:59:09 +000060 .. method:: itermonthdays(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000061
Benjamin Petersone41251e2008-04-25 01:59:09 +000062 Return an iterator for the month *month* in the year *year* similar to
63 :meth:`itermonthdates`. Days returned will simply be day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000064
65
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 .. method:: monthdatescalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 Return a list of the weeks in the month *month* of the *year* as full
69 weeks. Weeks are lists of seven :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000070
71
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 .. method:: monthdays2calendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000073
Benjamin Petersone41251e2008-04-25 01:59:09 +000074 Return a list of the weeks in the month *month* of the *year* as full
75 weeks. Weeks are lists of seven tuples of day numbers and weekday
76 numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000077
78
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 .. method:: monthdayscalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000080
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 Return a list of the weeks in the month *month* of the *year* as full
82 weeks. Weeks are lists of seven day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000083
84
Georg Brandl0d8f0732009-04-05 22:20:44 +000085 .. method:: yeardatescalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +000086
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 Return the data for the specified year ready for formatting. The return
88 value is a list of month rows. Each month row contains up to *width*
89 months (defaulting to 3). Each month contains between 4 and 6 weeks and
90 each week contains 1--7 days. Days are :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000091
92
Georg Brandl0d8f0732009-04-05 22:20:44 +000093 .. method:: yeardays2calendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +000094
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 Return the data for the specified year ready for formatting (similar to
96 :meth:`yeardatescalendar`). Entries in the week lists are tuples of day
97 numbers and weekday numbers. Day numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +000098
99
Georg Brandl0d8f0732009-04-05 22:20:44 +0000100 .. method:: yeardayscalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 Return the data for the specified year ready for formatting (similar to
103 :meth:`yeardatescalendar`). Entries in the week lists are day numbers. Day
104 numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106
Georg Brandl0d8f0732009-04-05 22:20:44 +0000107.. class:: TextCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109 This class can be used to generate plain text calendars.
110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 :class:`TextCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Georg Brandl0d8f0732009-04-05 22:20:44 +0000113 .. method:: formatmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 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
117 given, it specifies the number of lines that each week will use. Depends
118 on the first weekday as specified in the constructor or set by the
119 :meth:`setfirstweekday` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
Georg Brandl0d8f0732009-04-05 22:20:44 +0000122 .. method:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Benjamin Petersone41251e2008-04-25 01:59:09 +0000124 Print a month's calendar as returned by :meth:`formatmonth`.
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126
Benjamin Peterson20211002009-11-25 18:34:42 +0000127 .. method:: formatyear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 Return a *m*-column calendar for an entire year as a multi-line string.
130 Optional parameters *w*, *l*, and *c* are for date column width, lines per
131 week, and number of spaces between month columns, respectively. Depends on
132 the first weekday as specified in the constructor or set by the
133 :meth:`setfirstweekday` method. The earliest year for which a calendar
134 can be generated is platform-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
Georg Brandl0d8f0732009-04-05 22:20:44 +0000137 .. method:: pryear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Benjamin Petersone41251e2008-04-25 01:59:09 +0000139 Print the calendar for an entire year as returned by :meth:`formatyear`.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
Georg Brandl0d8f0732009-04-05 22:20:44 +0000142.. class:: HTMLCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144 This class can be used to generate HTML calendars.
145
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 :class:`HTMLCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Georg Brandl0d8f0732009-04-05 22:20:44 +0000149 .. method:: formatmonth(theyear, themonth, withyear=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Benjamin Petersone41251e2008-04-25 01:59:09 +0000151 Return a month's calendar as an HTML table. If *withyear* is true the year
152 will be included in the header, otherwise just the month name will be
153 used.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
Benjamin Peterson20211002009-11-25 18:34:42 +0000156 .. method:: formatyear(theyear, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Benjamin Petersone41251e2008-04-25 01:59:09 +0000158 Return a year's calendar as an HTML table. *width* (defaulting to 3)
159 specifies the number of months per row.
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161
Georg Brandl0d8f0732009-04-05 22:20:44 +0000162 .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 Return a year's calendar as a complete HTML page. *width* (defaulting to
165 3) specifies the number of months per row. *css* is the name for the
166 cascading style sheet to be used. :const:`None` can be passed if no style
167 sheet should be used. *encoding* specifies the encoding to be used for the
168 output (defaulting to the system default encoding).
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
Georg Brandl0d8f0732009-04-05 22:20:44 +0000171.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173 This subclass of :class:`TextCalendar` can be passed a locale name in the
Georg Brandl7004bd12010-10-19 18:54:25 +0000174 constructor and will return month and weekday names in the specified locale.
175 If this locale includes an encoding all strings containing month and weekday
176 names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Georg Brandl0d8f0732009-04-05 22:20:44 +0000179.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181 This subclass of :class:`HTMLCalendar` can be passed a locale name in the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000182 constructor and will return month and weekday names in the specified
183 locale. If this locale includes an encoding all strings containing month and
184 weekday names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Georg Brandl7004bd12010-10-19 18:54:25 +0000186.. note::
187
188 The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
189 classes temporarily change the current locale to the given *locale*. Because
190 the current locale is a process-wide setting, they are not thread-safe.
191
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193For simple text calendars this module provides the following functions.
194
Georg Brandl116aa622007-08-15 14:28:22 +0000195.. function:: setfirstweekday(weekday)
196
197 Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The
198 values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`,
199 :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for
200 convenience. For example, to set the first weekday to Sunday::
201
202 import calendar
203 calendar.setfirstweekday(calendar.SUNDAY)
204
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206.. function:: firstweekday()
207
208 Returns the current setting for the weekday to start each week.
209
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211.. function:: isleap(year)
212
213 Returns :const:`True` if *year* is a leap year, otherwise :const:`False`.
214
215
216.. function:: leapdays(y1, y2)
217
218 Returns the number of leap years in the range from *y1* to *y2* (exclusive),
219 where *y1* and *y2* are years.
220
Georg Brandl55ac8f02007-09-01 13:51:09 +0000221 This function works for ranges spanning a century change.
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223
224.. function:: weekday(year, month, day)
225
226 Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
227 *month* (``1``--``12``), *day* (``1``--``31``).
228
229
230.. function:: weekheader(n)
231
232 Return a header containing abbreviated weekday names. *n* specifies the width in
233 characters for one weekday.
234
235
236.. function:: monthrange(year, month)
237
238 Returns weekday of first day of the month and number of days in month, for the
239 specified *year* and *month*.
240
241
242.. function:: monthcalendar(year, month)
243
244 Returns a matrix representing a month's calendar. Each row represents a week;
245 days outside of the month a represented by zeros. Each week begins with Monday
246 unless set by :func:`setfirstweekday`.
247
248
Georg Brandl0d8f0732009-04-05 22:20:44 +0000249.. function:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251 Prints a month's calendar as returned by :func:`month`.
252
253
Georg Brandl0d8f0732009-04-05 22:20:44 +0000254.. function:: month(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256 Returns a month's calendar in a multi-line string using the :meth:`formatmonth`
257 of the :class:`TextCalendar` class.
258
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Georg Brandl0d8f0732009-04-05 22:20:44 +0000260.. function:: prcal(year, w=0, l=0, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262 Prints the calendar for an entire year as returned by :func:`calendar`.
263
264
Georg Brandl0d8f0732009-04-05 22:20:44 +0000265.. function:: calendar(year, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Georg Brandl0d8f0732009-04-05 22:20:44 +0000267 Returns a 3-column calendar for an entire year as a multi-line string using
268 the :meth:`formatyear` of the :class:`TextCalendar` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271.. function:: timegm(tuple)
272
273 An unrelated but handy function that takes a time tuple such as returned by the
274 :func:`gmtime` function in the :mod:`time` module, and returns the corresponding
275 Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In
276 fact, :func:`time.gmtime` and :func:`timegm` are each others' inverse.
277
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279The :mod:`calendar` module exports the following data attributes:
280
Georg Brandl116aa622007-08-15 14:28:22 +0000281.. data:: day_name
282
283 An array that represents the days of the week in the current locale.
284
285
286.. data:: day_abbr
287
288 An array that represents the abbreviated days of the week in the current locale.
289
290
291.. data:: month_name
292
293 An array that represents the months of the year in the current locale. This
294 follows normal convention of January being month number 1, so it has a length of
295 13 and ``month_name[0]`` is the empty string.
296
297
298.. data:: month_abbr
299
300 An array that represents the abbreviated months of the year in the current
301 locale. This follows normal convention of January being month number 1, so it
302 has a length of 13 and ``month_abbr[0]`` is the empty string.
303
304
305.. seealso::
306
307 Module :mod:`datetime`
308 Object-oriented interface to dates and times with similar functionality to the
309 :mod:`time` module.
310
311 Module :mod:`time`
312 Low-level time related functions.