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