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