blob: 8fe93fd4de3fa48ce851d208d839ae63d51c0aca [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:: itermonthdays(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
Alexander Belopolskyfdd9b212017-10-24 13:17:10 -040059 :meth:`itermonthdates`, but not restricted by the :class:`datetime.date`
60 range. Days returned will simply be day of the month numbers. For the
61 days outside of the specified month, the day number is ``0``.
62
63
64 .. method:: itermonthdays2(year, month)
65
66 Return an iterator for the month *month* in the year *year* similar to
67 :meth:`itermonthdates`, but not restricted by the :class:`datetime.date`
68 range. Days returned will be tuples consisting of a day of the month
69 number and a week day number.
70
71
72 .. method:: itermonthdays3(year, month)
73
74 Return an iterator for the month *month* in the year *year* similar to
75 :meth:`itermonthdates`, but not restricted by the :class:`datetime.date`
76 range. Days returned will be tuples consisting of a year, a month and a day
77 of the month numbers.
78
79 .. versionadded:: 3.7
80
81
82 .. method:: itermonthdays4(year, month)
83
84 Return an iterator for the month *month* in the year *year* similar to
85 :meth:`itermonthdates`, but not restricted by the :class:`datetime.date`
86 range. Days returned will be tuples consisting of a year, a month, a day
87 of the month, and a day of the week numbers.
88
89 .. versionadded:: 3.7
Georg Brandl116aa622007-08-15 14:28:22 +000090
91
Benjamin Petersone41251e2008-04-25 01:59:09 +000092 .. method:: monthdatescalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000093
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 Return a list of the weeks in the month *month* of the *year* as full
95 weeks. Weeks are lists of seven :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +000096
97
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 .. method:: monthdays2calendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +000099
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 Return a list of the weeks in the month *month* of the *year* as full
101 weeks. Weeks are lists of seven tuples of day numbers and weekday
102 numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 .. method:: monthdayscalendar(year, month)
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 Return a list of the weeks in the month *month* of the *year* as full
108 weeks. Weeks are lists of seven day numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
Georg Brandl0d8f0732009-04-05 22:20:44 +0000111 .. method:: yeardatescalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 Return the data for the specified year ready for formatting. The return
114 value is a list of month rows. Each month row contains up to *width*
115 months (defaulting to 3). Each month contains between 4 and 6 weeks and
116 each week contains 1--7 days. Days are :class:`datetime.date` objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
Georg Brandl0d8f0732009-04-05 22:20:44 +0000119 .. method:: yeardays2calendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 Return the data for the specified year ready for formatting (similar to
122 :meth:`yeardatescalendar`). Entries in the week lists are tuples of day
123 numbers and weekday numbers. Day numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
Georg Brandl0d8f0732009-04-05 22:20:44 +0000126 .. method:: yeardayscalendar(year, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 Return the data for the specified year ready for formatting (similar to
129 :meth:`yeardatescalendar`). Entries in the week lists are day numbers. Day
130 numbers outside this month are zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132
Georg Brandl0d8f0732009-04-05 22:20:44 +0000133.. class:: TextCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135 This class can be used to generate plain text calendars.
136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 :class:`TextCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Georg Brandl0d8f0732009-04-05 22:20:44 +0000139 .. method:: formatmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Benjamin Petersone41251e2008-04-25 01:59:09 +0000141 Return a month's calendar in a multi-line string. If *w* is provided, it
142 specifies the width of the date columns, which are centered. If *l* is
143 given, it specifies the number of lines that each week will use. Depends
144 on the first weekday as specified in the constructor or set by the
145 :meth:`setfirstweekday` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
Georg Brandl0d8f0732009-04-05 22:20:44 +0000148 .. method:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 Print a month's calendar as returned by :meth:`formatmonth`.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152
Benjamin Peterson20211002009-11-25 18:34:42 +0000153 .. method:: formatyear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Benjamin Petersone41251e2008-04-25 01:59:09 +0000155 Return a *m*-column calendar for an entire year as a multi-line string.
156 Optional parameters *w*, *l*, and *c* are for date column width, lines per
157 week, and number of spaces between month columns, respectively. Depends on
158 the first weekday as specified in the constructor or set by the
159 :meth:`setfirstweekday` method. The earliest year for which a calendar
160 can be generated is platform-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162
Georg Brandl0d8f0732009-04-05 22:20:44 +0000163 .. method:: pryear(theyear, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Benjamin Petersone41251e2008-04-25 01:59:09 +0000165 Print the calendar for an entire year as returned by :meth:`formatyear`.
Georg Brandl116aa622007-08-15 14:28:22 +0000166
167
Georg Brandl0d8f0732009-04-05 22:20:44 +0000168.. class:: HTMLCalendar(firstweekday=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170 This class can be used to generate HTML calendars.
171
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Oz N Tiram8b7a4cc2017-06-06 11:35:59 +0200173 :class:`!HTMLCalendar` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl0d8f0732009-04-05 22:20:44 +0000175 .. method:: formatmonth(theyear, themonth, withyear=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Benjamin Petersone41251e2008-04-25 01:59:09 +0000177 Return a month's calendar as an HTML table. If *withyear* is true the year
178 will be included in the header, otherwise just the month name will be
179 used.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181
Benjamin Peterson20211002009-11-25 18:34:42 +0000182 .. method:: formatyear(theyear, width=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000183
Benjamin Petersone41251e2008-04-25 01:59:09 +0000184 Return a year's calendar as an HTML table. *width* (defaulting to 3)
185 specifies the number of months per row.
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187
Georg Brandl0d8f0732009-04-05 22:20:44 +0000188 .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Benjamin Petersone41251e2008-04-25 01:59:09 +0000190 Return a year's calendar as a complete HTML page. *width* (defaulting to
191 3) specifies the number of months per row. *css* is the name for the
192 cascading style sheet to be used. :const:`None` can be passed if no style
193 sheet should be used. *encoding* specifies the encoding to be used for the
194 output (defaulting to the system default encoding).
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196
Oz N Tiram8b7a4cc2017-06-06 11:35:59 +0200197 :class:`!HTMLCalendar` has the following attributes you can override to
198 customize the CSS classes used by the calendar:
199
200 .. attribute:: cssclasses
201
202 A list of CSS classes used for each weekday. The default class list is::
203
204 cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
205
206 more styles can be added for each day::
207
208 cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
209
210 Note that the length of this list must be seven items.
211
212
213 .. attribute:: cssclass_noday
214
215 The CSS class for a weekday occurring in the previous or coming month.
216
217 .. versionadded:: 3.7
218
219
220 .. attribute:: cssclasses_weekday_head
221
222 A list of CSS classes used for weekday names in the header row.
223 The default is the same as :attr:`cssclasses`.
224
225 .. versionadded:: 3.7
226
227
228 .. attribute:: cssclass_month_head
229
230 The month's head CSS class (used by :meth:`formatmonthname`).
231 The default value is ``"month"``.
232
233 .. versionadded:: 3.7
234
235
236 .. attribute:: cssclass_month
237
238 The CSS class for the whole month's table (used by :meth:`formatmonth`).
239 The default value is ``"month"``.
240
241 .. versionadded:: 3.7
242
243
244 .. attribute:: cssclass_year
245
246 The CSS class for the whole year's table of tables (used by
247 :meth:`formatyear`). The default value is ``"year"``.
248
249 .. versionadded:: 3.7
250
251
252 .. attribute:: cssclass_year_head
253
254 The CSS class for the table head for the whole year (used by
255 :meth:`formatyear`). The default value is ``"year"``.
256
257 .. versionadded:: 3.7
258
259
260 Note that although the naming for the above described class attributes is
261 singular (e.g. ``cssclass_month`` ``cssclass_noday``), one can replace the
262 single CSS class with a space separated list of CSS classes, for example::
263
264 "text-bold text-red"
265
266 Here is an example how :class:`!HTMLCalendar` can be customized::
267
268 class CustomHTMLCal(calendar.HTMLCalendar):
269 cssclasses = [style + " text-nowrap" for style in
270 calendar.HTMLCalendar.cssclasses]
271 cssclass_month_head = "text-center month-head"
272 cssclass_month = "text-center month"
273 cssclass_year = "text-italic lead"
274
275
Georg Brandl0d8f0732009-04-05 22:20:44 +0000276.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000277
278 This subclass of :class:`TextCalendar` can be passed a locale name in the
Georg Brandl7004bd12010-10-19 18:54:25 +0000279 constructor and will return month and weekday names in the specified locale.
280 If this locale includes an encoding all strings containing month and weekday
281 names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Georg Brandl116aa622007-08-15 14:28:22 +0000283
Georg Brandl0d8f0732009-04-05 22:20:44 +0000284.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286 This subclass of :class:`HTMLCalendar` can be passed a locale name in the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000287 constructor and will return month and weekday names in the specified
288 locale. If this locale includes an encoding all strings containing month and
289 weekday names will be returned as unicode.
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Georg Brandl7004bd12010-10-19 18:54:25 +0000291.. note::
292
293 The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
294 classes temporarily change the current locale to the given *locale*. Because
295 the current locale is a process-wide setting, they are not thread-safe.
296
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298For simple text calendars this module provides the following functions.
299
Georg Brandl116aa622007-08-15 14:28:22 +0000300.. function:: setfirstweekday(weekday)
301
302 Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The
303 values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`,
304 :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for
305 convenience. For example, to set the first weekday to Sunday::
306
307 import calendar
308 calendar.setfirstweekday(calendar.SUNDAY)
309
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311.. function:: firstweekday()
312
313 Returns the current setting for the weekday to start each week.
314
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316.. function:: isleap(year)
317
318 Returns :const:`True` if *year* is a leap year, otherwise :const:`False`.
319
320
321.. function:: leapdays(y1, y2)
322
323 Returns the number of leap years in the range from *y1* to *y2* (exclusive),
324 where *y1* and *y2* are years.
325
Georg Brandl55ac8f02007-09-01 13:51:09 +0000326 This function works for ranges spanning a century change.
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328
329.. function:: weekday(year, month, day)
330
331 Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
332 *month* (``1``--``12``), *day* (``1``--``31``).
333
334
335.. function:: weekheader(n)
336
337 Return a header containing abbreviated weekday names. *n* specifies the width in
338 characters for one weekday.
339
340
341.. function:: monthrange(year, month)
342
343 Returns weekday of first day of the month and number of days in month, for the
344 specified *year* and *month*.
345
346
347.. function:: monthcalendar(year, month)
348
349 Returns a matrix representing a month's calendar. Each row represents a week;
350 days outside of the month a represented by zeros. Each week begins with Monday
351 unless set by :func:`setfirstweekday`.
352
353
Georg Brandl0d8f0732009-04-05 22:20:44 +0000354.. function:: prmonth(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 Prints a month's calendar as returned by :func:`month`.
357
358
Georg Brandl0d8f0732009-04-05 22:20:44 +0000359.. function:: month(theyear, themonth, w=0, l=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361 Returns a month's calendar in a multi-line string using the :meth:`formatmonth`
362 of the :class:`TextCalendar` class.
363
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Georg Brandl0d8f0732009-04-05 22:20:44 +0000365.. function:: prcal(year, w=0, l=0, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367 Prints the calendar for an entire year as returned by :func:`calendar`.
368
369
Georg Brandl0d8f0732009-04-05 22:20:44 +0000370.. function:: calendar(year, w=2, l=1, c=6, m=3)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Georg Brandl0d8f0732009-04-05 22:20:44 +0000372 Returns a 3-column calendar for an entire year as a multi-line string using
373 the :meth:`formatyear` of the :class:`TextCalendar` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376.. function:: timegm(tuple)
377
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300378 An unrelated but handy function that takes a time tuple such as returned by
379 the :func:`~time.gmtime` function in the :mod:`time` module, and returns the
380 corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX
381 encoding. In fact, :func:`time.gmtime` and :func:`timegm` are each others'
382 inverse.
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Georg Brandl116aa622007-08-15 14:28:22 +0000384
385The :mod:`calendar` module exports the following data attributes:
386
Georg Brandl116aa622007-08-15 14:28:22 +0000387.. data:: day_name
388
389 An array that represents the days of the week in the current locale.
390
391
392.. data:: day_abbr
393
394 An array that represents the abbreviated days of the week in the current locale.
395
396
397.. data:: month_name
398
399 An array that represents the months of the year in the current locale. This
400 follows normal convention of January being month number 1, so it has a length of
401 13 and ``month_name[0]`` is the empty string.
402
403
404.. data:: month_abbr
405
406 An array that represents the abbreviated months of the year in the current
407 locale. This follows normal convention of January being month number 1, so it
408 has a length of 13 and ``month_abbr[0]`` is the empty string.
409
410
411.. seealso::
412
413 Module :mod:`datetime`
414 Object-oriented interface to dates and times with similar functionality to the
415 :mod:`time` module.
416
417 Module :mod:`time`
418 Low-level time related functions.