Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
| 19 | Most of these functions and classses rely on the :mod:`datetime` module which |
| 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 | |
| 25 | |
| 26 | .. class:: Calendar([firstweekday]) |
| 27 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 36 | :class:`Calendar` instances have the following methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 38 | .. method:: iterweekdays() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 40 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | |
| 44 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 45 | .. method:: itermonthdates(year, month) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 47 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
| 52 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 53 | .. method:: itermonthdays2(year, month) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 55 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 60 | .. method:: itermonthdays(year, month) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 62 | Return an iterator for the month *month* in the year *year* similar to |
| 63 | :meth:`itermonthdates`. Days returned will simply be day numbers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
| 65 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 66 | .. method:: monthdatescalendar(year, month) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 68 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 72 | .. method:: monthdays2calendar(year, month) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +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 tuples of day numbers and weekday |
| 76 | numbers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
| 78 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 79 | .. method:: monthdayscalendar(year, month) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 81 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 85 | .. method:: yeardatescalendar(year[, width]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 87 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 93 | .. method:: yeardays2calendar(year[, width]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 95 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 100 | .. method:: yeardayscalendar(year[, width]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 102 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | |
| 107 | .. class:: TextCalendar([firstweekday]) |
| 108 | |
| 109 | This class can be used to generate plain text calendars. |
| 110 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 112 | :class:`TextCalendar` instances have the following methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 114 | .. method:: formatmonth(theyear, themonth[, w[, l]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 116 | Return a month's calendar in a multi-line string. If *w* is provided, it |
| 117 | specifies the width of the date columns, which are centered. If *l* is |
| 118 | given, it specifies the number of lines that each week will use. Depends |
| 119 | on the first weekday as specified in the constructor or set by the |
| 120 | :meth:`setfirstweekday` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | |
| 122 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 123 | .. method:: prmonth(theyear, themonth[, w[, l]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 125 | Print a month's calendar as returned by :meth:`formatmonth`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | |
| 127 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 128 | .. method:: formatyear(theyear, themonth[, w[, l[, c[, m]]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 130 | Return a *m*-column calendar for an entire year as a multi-line string. |
| 131 | Optional parameters *w*, *l*, and *c* are for date column width, lines per |
| 132 | week, and number of spaces between month columns, respectively. Depends on |
| 133 | the first weekday as specified in the constructor or set by the |
| 134 | :meth:`setfirstweekday` method. The earliest year for which a calendar |
| 135 | can be generated is platform-dependent. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | |
| 137 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 138 | .. method:: pryear(theyear[, w[, l[, c[, m]]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 140 | Print the calendar for an entire year as returned by :meth:`formatyear`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | .. class:: HTMLCalendar([firstweekday]) |
| 144 | |
| 145 | This class can be used to generate HTML calendars. |
| 146 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 148 | :class:`HTMLCalendar` instances have the following methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 150 | .. method:: formatmonth(theyear, themonth[, withyear]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 152 | Return a month's calendar as an HTML table. If *withyear* is true the year |
| 153 | will be included in the header, otherwise just the month name will be |
| 154 | used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
| 156 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 157 | .. method:: formatyear(theyear, themonth[, width]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 159 | Return a year's calendar as an HTML table. *width* (defaulting to 3) |
| 160 | specifies the number of months per row. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
| 162 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 163 | .. method:: formatyearpage(theyear[, width[, css[, encoding]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 165 | Return a year's calendar as a complete HTML page. *width* (defaulting to |
| 166 | 3) specifies the number of months per row. *css* is the name for the |
| 167 | cascading style sheet to be used. :const:`None` can be passed if no style |
| 168 | sheet should be used. *encoding* specifies the encoding to be used for the |
| 169 | output (defaulting to the system default encoding). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | .. class:: LocaleTextCalendar([firstweekday[, locale]]) |
| 173 | |
| 174 | This subclass of :class:`TextCalendar` can be passed a locale name in the |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 175 | constructor and will return month and weekday names in the specified |
| 176 | locale. If this locale includes an encoding all strings containing month and |
| 177 | weekday names will be returned as unicode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
| 180 | .. class:: LocaleHTMLCalendar([firstweekday[, locale]]) |
| 181 | |
| 182 | This subclass of :class:`HTMLCalendar` can be passed a locale name in the |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 183 | constructor and will return month and weekday names in the specified |
| 184 | locale. If this locale includes an encoding all strings containing month and |
| 185 | weekday names will be returned as unicode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
| 188 | For simple text calendars this module provides the following functions. |
| 189 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | .. function:: setfirstweekday(weekday) |
| 191 | |
| 192 | Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The |
| 193 | values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`, |
| 194 | :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for |
| 195 | convenience. For example, to set the first weekday to Sunday:: |
| 196 | |
| 197 | import calendar |
| 198 | calendar.setfirstweekday(calendar.SUNDAY) |
| 199 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | |
| 201 | .. function:: firstweekday() |
| 202 | |
| 203 | Returns the current setting for the weekday to start each week. |
| 204 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | |
| 206 | .. function:: isleap(year) |
| 207 | |
| 208 | Returns :const:`True` if *year* is a leap year, otherwise :const:`False`. |
| 209 | |
| 210 | |
| 211 | .. function:: leapdays(y1, y2) |
| 212 | |
| 213 | Returns the number of leap years in the range from *y1* to *y2* (exclusive), |
| 214 | where *y1* and *y2* are years. |
| 215 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 216 | This function works for ranges spanning a century change. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | .. function:: weekday(year, month, day) |
| 220 | |
| 221 | Returns the day of the week (``0`` is Monday) for *year* (``1970``--...), |
| 222 | *month* (``1``--``12``), *day* (``1``--``31``). |
| 223 | |
| 224 | |
| 225 | .. function:: weekheader(n) |
| 226 | |
| 227 | Return a header containing abbreviated weekday names. *n* specifies the width in |
| 228 | characters for one weekday. |
| 229 | |
| 230 | |
| 231 | .. function:: monthrange(year, month) |
| 232 | |
| 233 | Returns weekday of first day of the month and number of days in month, for the |
| 234 | specified *year* and *month*. |
| 235 | |
| 236 | |
| 237 | .. function:: monthcalendar(year, month) |
| 238 | |
| 239 | Returns a matrix representing a month's calendar. Each row represents a week; |
| 240 | days outside of the month a represented by zeros. Each week begins with Monday |
| 241 | unless set by :func:`setfirstweekday`. |
| 242 | |
| 243 | |
| 244 | .. function:: prmonth(theyear, themonth[, w[, l]]) |
| 245 | |
| 246 | Prints a month's calendar as returned by :func:`month`. |
| 247 | |
| 248 | |
| 249 | .. function:: month(theyear, themonth[, w[, l]]) |
| 250 | |
| 251 | Returns a month's calendar in a multi-line string using the :meth:`formatmonth` |
| 252 | of the :class:`TextCalendar` class. |
| 253 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
| 255 | .. function:: prcal(year[, w[, l[c]]]) |
| 256 | |
| 257 | Prints the calendar for an entire year as returned by :func:`calendar`. |
| 258 | |
| 259 | |
| 260 | .. function:: calendar(year[, w[, l[c]]]) |
| 261 | |
| 262 | Returns a 3-column calendar for an entire year as a multi-line string using the |
| 263 | :meth:`formatyear` of the :class:`TextCalendar` class. |
| 264 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
| 266 | .. function:: timegm(tuple) |
| 267 | |
| 268 | An unrelated but handy function that takes a time tuple such as returned by the |
| 269 | :func:`gmtime` function in the :mod:`time` module, and returns the corresponding |
| 270 | Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In |
| 271 | fact, :func:`time.gmtime` and :func:`timegm` are each others' inverse. |
| 272 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | |
| 274 | The :mod:`calendar` module exports the following data attributes: |
| 275 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | .. data:: day_name |
| 277 | |
| 278 | An array that represents the days of the week in the current locale. |
| 279 | |
| 280 | |
| 281 | .. data:: day_abbr |
| 282 | |
| 283 | An array that represents the abbreviated days of the week in the current locale. |
| 284 | |
| 285 | |
| 286 | .. data:: month_name |
| 287 | |
| 288 | An array that represents the months of the year in the current locale. This |
| 289 | follows normal convention of January being month number 1, so it has a length of |
| 290 | 13 and ``month_name[0]`` is the empty string. |
| 291 | |
| 292 | |
| 293 | .. data:: month_abbr |
| 294 | |
| 295 | An array that represents the abbreviated months of the year in the current |
| 296 | locale. This follows normal convention of January being month number 1, so it |
| 297 | has a length of 13 and ``month_abbr[0]`` is the empty string. |
| 298 | |
| 299 | |
| 300 | .. seealso:: |
| 301 | |
| 302 | Module :mod:`datetime` |
| 303 | Object-oriented interface to dates and times with similar functionality to the |
| 304 | :mod:`time` module. |
| 305 | |
| 306 | Module :mod:`time` |
| 307 | Low-level time related functions. |
| 308 | |