blob: a415b4792a6bd6be79e58b7694c22a6a782c51ad [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
9
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/calendar.py`
Georg Brandl116aa622007-08-15 14:28:22 +000011
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +000014This module allows you to output calendars like the Unix :program:`cal` program,
15and provides additional useful functions related to the calendar. By default,
16these calendars have Monday as the first day of the week, and Sunday as the last
17(the European convention). Use :func:`setfirstweekday` to set the first day of
18the week to Sunday (6) or to any other weekday. Parameters that specify dates
19are given as integers. For related
20functionality, see also the :mod:`datetime` and :mod:`time` modules.
21
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000022Most of these functions and classes rely on the :mod:`datetime` module which
Alexander Belopolskyd4bbab22010-11-22 03:06:56 +000023uses an idealized calendar, the current Gregorian calendar extended
Georg Brandl116aa622007-08-15 14:28:22 +000024in both directions. This matches the definition of the "proleptic Gregorian"
25calendar in Dershowitz and Reingold's book "Calendrical Calculations", where
26it's the base calendar for all computations.
27
28
Georg Brandl0d8f0732009-04-05 22:20:44 +000029.. class:: Calendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
32 first day of the week. ``0`` is Monday (the default), ``6`` is Sunday.
33
34 A :class:`Calendar` object provides several methods that can be used for
35 preparing the calendar data for formatting. This class doesn't do any formatting
36 itself. This is the job of subclasses.
37
Georg Brandl116aa622007-08-15 14:28:22 +000038
Benjamin Petersone41251e2008-04-25 01:59:09 +000039 :class:`Calendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000040
Alexandre Vassalottibee32532008-05-16 18:15:12 +000041 .. method:: iterweekdays()
Georg Brandl116aa622007-08-15 14:28:22 +000042
Benjamin Petersone41251e2008-04-25 01:59:09 +000043 Return an iterator for the week day numbers that will be used for one
44 week. The first value from the iterator will be the same as the value of
45 the :attr:`firstweekday` property.
Georg Brandl116aa622007-08-15 14:28:22 +000046
47
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 .. method:: itermonthdates(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000049
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +020050 Return an iterator for the month *month* (1--12) in the year *year*. This
Benjamin Petersone41251e2008-04-25 01:59:09 +000051 iterator will return all days (as :class:`datetime.date` objects) for the
52 month and all days before the start of the month or after the end of the
53 month that are required to get a complete week.
Georg Brandl116aa622007-08-15 14:28:22 +000054
55
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 .. method:: itermonthdays2(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000057
Benjamin Petersone41251e2008-04-25 01:59:09 +000058 Return an iterator for the month *month* in the year *year* similar to
59 :meth:`itermonthdates`. Days returned will be tuples consisting of a day
60 number and a week day number.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
Benjamin Petersone41251e2008-04-25 01:59:09 +000063 .. method:: itermonthdays(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000064
Benjamin Petersone41251e2008-04-25 01:59:09 +000065 Return an iterator for the month *month* in the year *year* similar to
66 :meth:`itermonthdates`. Days returned will simply be day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
Benjamin Petersone41251e2008-04-25 01:59:09 +000069 .. method:: monthdatescalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000070
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 Return a list of the weeks in the month *month* of the *year* as full
72 weeks. Weeks are lists of seven :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 .. method:: monthdays2calendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000076
Benjamin Petersone41251e2008-04-25 01:59:09 +000077 Return a list of the weeks in the month *month* of the *year* as full
78 weeks. Weeks are lists of seven tuples of day numbers and weekday
79 numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. method:: monthdayscalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 Return a list of the weeks in the month *month* of the *year* as full
85 weeks. Weeks are lists of seven day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87
Georg Brandl0d8f0732009-04-05 22:20:44 +000088 .. method:: yeardatescalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +000089
Benjamin Petersone41251e2008-04-25 01:59:09 +000090 Return the data for the specified year ready for formatting. The return
91 value is a list of month rows. Each month row contains up to *width*
92 months (defaulting to 3). Each month contains between 4 and 6 weeks and
93 each week contains 1--7 days. Days are :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000094
95
Georg Brandl0d8f0732009-04-05 22:20:44 +000096 .. method:: yeardays2calendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +000097
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 Return the data for the specified year ready for formatting (similar to
99 :meth:`yeardatescalendar`). Entries in the week lists are tuples of day
100 numbers and weekday numbers. Day numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102
Georg Brandl0d8f0732009-04-05 22:20:44 +0000103 .. method:: yeardayscalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 Return the data for the specified year ready for formatting (similar to
106 :meth:`yeardatescalendar`). Entries in the week lists are day numbers. Day
107 numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109
Georg Brandl0d8f0732009-04-05 22:20:44 +0000110.. class:: TextCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112 This class can be used to generate plain text calendars.
113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 :class:`TextCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Georg Brandl0d8f0732009-04-05 22:20:44 +0000116 .. method:: formatmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 Return a month's calendar in a multi-line string. If *w* is provided, it
119 specifies the width of the date columns, which are centered. If *l* is
120 given, it specifies the number of lines that each week will use. Depends
121 on the first weekday as specified in the constructor or set by the
122 :meth:`setfirstweekday` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124
Georg Brandl0d8f0732009-04-05 22:20:44 +0000125 .. method:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Benjamin Petersone41251e2008-04-25 01:59:09 +0000127 Print a month's calendar as returned by :meth:`formatmonth`.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129
Benjamin Peterson20211002009-11-25 18:34:42 +0000130 .. method:: formatyear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Benjamin Petersone41251e2008-04-25 01:59:09 +0000132 Return a *m*-column calendar for an entire year as a multi-line string.
133 Optional parameters *w*, *l*, and *c* are for date column width, lines per
134 week, and number of spaces between month columns, respectively. Depends on
135 the first weekday as specified in the constructor or set by the
136 :meth:`setfirstweekday` method. The earliest year for which a calendar
137 can be generated is platform-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139
Georg Brandl0d8f0732009-04-05 22:20:44 +0000140 .. method:: pryear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 Print the calendar for an entire year as returned by :meth:`formatyear`.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144
Georg Brandl0d8f0732009-04-05 22:20:44 +0000145.. class:: HTMLCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147 This class can be used to generate HTML calendars.
148
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Oz N Tiram8b7a4cc2017-06-06 11:35:59 +0200150 :class:`!HTMLCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Georg Brandl0d8f0732009-04-05 22:20:44 +0000152 .. method:: formatmonth(theyear, themonth, withyear=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 Return a month's calendar as an HTML table. If *withyear* is true the year
155 will be included in the header, otherwise just the month name will be
156 used.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
Benjamin Peterson20211002009-11-25 18:34:42 +0000159 .. method:: formatyear(theyear, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Benjamin Petersone41251e2008-04-25 01:59:09 +0000161 Return a year's calendar as an HTML table. *width* (defaulting to 3)
162 specifies the number of months per row.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164
Georg Brandl0d8f0732009-04-05 22:20:44 +0000165 .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 Return a year's calendar as a complete HTML page. *width* (defaulting to
168 3) specifies the number of months per row. *css* is the name for the
169 cascading style sheet to be used. :const:`None` can be passed if no style
170 sheet should be used. *encoding* specifies the encoding to be used for the
171 output (defaulting to the system default encoding).
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173
Oz N Tiram8b7a4cc2017-06-06 11:35:59 +0200174 :class:`!HTMLCalendar` has the following attributes you can override to
175 customize the CSS classes used by the calendar:
176
177 .. attribute:: cssclasses
178
179 A list of CSS classes used for each weekday. The default class list is::
180
181 cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
182
183 more styles can be added for each day::
184
185 cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
186
187 Note that the length of this list must be seven items.
188
189
190 .. attribute:: cssclass_noday
191
192 The CSS class for a weekday occurring in the previous or coming month.
193
194 .. versionadded:: 3.7
195
196
197 .. attribute:: cssclasses_weekday_head
198
199 A list of CSS classes used for weekday names in the header row.
200 The default is the same as :attr:`cssclasses`.
201
202 .. versionadded:: 3.7
203
204
205 .. attribute:: cssclass_month_head
206
207 The month's head CSS class (used by :meth:`formatmonthname`).
208 The default value is ``"month"``.
209
210 .. versionadded:: 3.7
211
212
213 .. attribute:: cssclass_month
214
215 The CSS class for the whole month's table (used by :meth:`formatmonth`).
216 The default value is ``"month"``.
217
218 .. versionadded:: 3.7
219
220
221 .. attribute:: cssclass_year
222
223 The CSS class for the whole year's table of tables (used by
224 :meth:`formatyear`). The default value is ``"year"``.
225
226 .. versionadded:: 3.7
227
228
229 .. attribute:: cssclass_year_head
230
231 The CSS class for the table head for the whole year (used by
232 :meth:`formatyear`). The default value is ``"year"``.
233
234 .. versionadded:: 3.7
235
236
237 Note that although the naming for the above described class attributes is
238 singular (e.g. ``cssclass_month`` ``cssclass_noday``), one can replace the
239 single CSS class with a space separated list of CSS classes, for example::
240
241 "text-bold text-red"
242
243 Here is an example how :class:`!HTMLCalendar` can be customized::
244
245 class CustomHTMLCal(calendar.HTMLCalendar):
246 cssclasses = [style + " text-nowrap" for style in
247 calendar.HTMLCalendar.cssclasses]
248 cssclass_month_head = "text-center month-head"
249 cssclass_month = "text-center month"
250 cssclass_year = "text-italic lead"
251
252
Georg Brandl0d8f0732009-04-05 22:20:44 +0000253.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255 This subclass of :class:`TextCalendar` can be passed a locale name in the
Georg Brandl7004bd12010-10-19 18:54:25 +0000256 constructor and will return month and weekday names in the specified locale.
257 If this locale includes an encoding all strings containing month and weekday
258 names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Georg Brandl0d8f0732009-04-05 22:20:44 +0000261.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263 This subclass of :class:`HTMLCalendar` can be passed a locale name in the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000264 constructor and will return month and weekday names in the specified
265 locale. If this locale includes an encoding all strings containing month and
266 weekday names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
Georg Brandl7004bd12010-10-19 18:54:25 +0000268.. note::
269
270 The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
271 classes temporarily change the current locale to the given *locale*. Because
272 the current locale is a process-wide setting, they are not thread-safe.
273
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275For simple text calendars this module provides the following functions.
276
Georg Brandl116aa622007-08-15 14:28:22 +0000277.. function:: setfirstweekday(weekday)
278
279 Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The
280 values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`,
281 :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for
282 convenience. For example, to set the first weekday to Sunday::
283
284 import calendar
285 calendar.setfirstweekday(calendar.SUNDAY)
286
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288.. function:: firstweekday()
289
290 Returns the current setting for the weekday to start each week.
291
Georg Brandl116aa622007-08-15 14:28:22 +0000292
293.. function:: isleap(year)
294
295 Returns :const:`True` if *year* is a leap year, otherwise :const:`False`.
296
297
298.. function:: leapdays(y1, y2)
299
300 Returns the number of leap years in the range from *y1* to *y2* (exclusive),
301 where *y1* and *y2* are years.
302
Georg Brandl55ac8f02007-09-01 13:51:09 +0000303 This function works for ranges spanning a century change.
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305
306.. function:: weekday(year, month, day)
307
308 Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
309 *month* (``1``--``12``), *day* (``1``--``31``).
310
311
312.. function:: weekheader(n)
313
314 Return a header containing abbreviated weekday names. *n* specifies the width in
315 characters for one weekday.
316
317
318.. function:: monthrange(year, month)
319
320 Returns weekday of first day of the month and number of days in month, for the
321 specified *year* and *month*.
322
323
324.. function:: monthcalendar(year, month)
325
326 Returns a matrix representing a month's calendar. Each row represents a week;
327 days outside of the month a represented by zeros. Each week begins with Monday
328 unless set by :func:`setfirstweekday`.
329
330
Georg Brandl0d8f0732009-04-05 22:20:44 +0000331.. function:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333 Prints a month's calendar as returned by :func:`month`.
334
335
Georg Brandl0d8f0732009-04-05 22:20:44 +0000336.. function:: month(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338 Returns a month's calendar in a multi-line string using the :meth:`formatmonth`
339 of the :class:`TextCalendar` class.
340
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Georg Brandl0d8f0732009-04-05 22:20:44 +0000342.. function:: prcal(year, w=0, l=0, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000343
344 Prints the calendar for an entire year as returned by :func:`calendar`.
345
346
Georg Brandl0d8f0732009-04-05 22:20:44 +0000347.. function:: calendar(year, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000348
Georg Brandl0d8f0732009-04-05 22:20:44 +0000349 Returns a 3-column calendar for an entire year as a multi-line string using
350 the :meth:`formatyear` of the :class:`TextCalendar` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000351
Georg Brandl116aa622007-08-15 14:28:22 +0000352
353.. function:: timegm(tuple)
354
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300355 An unrelated but handy function that takes a time tuple such as returned by
356 the :func:`~time.gmtime` function in the :mod:`time` module, and returns the
357 corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX
358 encoding. In fact, :func:`time.gmtime` and :func:`timegm` are each others'
359 inverse.
Georg Brandl116aa622007-08-15 14:28:22 +0000360
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362The :mod:`calendar` module exports the following data attributes:
363
Georg Brandl116aa622007-08-15 14:28:22 +0000364.. data:: day_name
365
366 An array that represents the days of the week in the current locale.
367
368
369.. data:: day_abbr
370
371 An array that represents the abbreviated days of the week in the current locale.
372
373
374.. data:: month_name
375
376 An array that represents the months of the year in the current locale. This
377 follows normal convention of January being month number 1, so it has a length of
378 13 and ``month_name[0]`` is the empty string.
379
380
381.. data:: month_abbr
382
383 An array that represents the abbreviated months of the year in the current
384 locale. This follows normal convention of January being month number 1, so it
385 has a length of 13 and ``month_abbr[0]`` is the empty string.
386
387
388.. seealso::
389
390 Module :mod:`datetime`
391 Object-oriented interface to dates and times with similar functionality to the
392 :mod:`time` module.
393
394 Module :mod:`time`
395 Low-level time related functions.