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