/Doc/library/calendar.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 330 lines · 185 code · 145 blank · 0 comment · 0 complexity · a71882057c0529ebefe486a710326bed MD5 · raw file

  1. :mod:`calendar` --- General calendar-related functions
  2. ======================================================
  3. .. module:: calendar
  4. :synopsis: Functions for working with calendars, including some emulation of the Unix cal
  5. program.
  6. .. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
  7. This module allows you to output calendars like the Unix :program:`cal` program,
  8. and provides additional useful functions related to the calendar. By default,
  9. these calendars have Monday as the first day of the week, and Sunday as the last
  10. (the European convention). Use :func:`setfirstweekday` to set the first day of
  11. the week to Sunday (6) or to any other weekday. Parameters that specify dates
  12. are given as integers. For related
  13. functionality, see also the :mod:`datetime` and :mod:`time` modules.
  14. Most of these functions and classses rely on the :mod:`datetime` module which
  15. uses an idealized calendar, the current Gregorian calendar indefinitely extended
  16. in both directions. This matches the definition of the "proleptic Gregorian"
  17. calendar in Dershowitz and Reingold's book "Calendrical Calculations", where
  18. it's the base calendar for all computations.
  19. .. class:: Calendar([firstweekday])
  20. Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
  21. first day of the week. ``0`` is Monday (the default), ``6`` is Sunday.
  22. A :class:`Calendar` object provides several methods that can be used for
  23. preparing the calendar data for formatting. This class doesn't do any formatting
  24. itself. This is the job of subclasses.
  25. .. versionadded:: 2.5
  26. :class:`Calendar` instances have the following methods:
  27. .. method:: iterweekdays()
  28. Return an iterator for the week day numbers that will be used for one
  29. week. The first value from the iterator will be the same as the value of
  30. the :attr:`firstweekday` property.
  31. .. method:: itermonthdates(year, month)
  32. Return an iterator for the month *month* (1-12) in the year *year*. This
  33. iterator will return all days (as :class:`datetime.date` objects) for the
  34. month and all days before the start of the month or after the end of the
  35. month that are required to get a complete week.
  36. .. method:: itermonthdays2(year, month)
  37. Return an iterator for the month *month* in the year *year* similar to
  38. :meth:`itermonthdates`. Days returned will be tuples consisting of a day
  39. number and a week day number.
  40. .. method:: itermonthdays(year, month)
  41. Return an iterator for the month *month* in the year *year* similar to
  42. :meth:`itermonthdates`. Days returned will simply be day numbers.
  43. .. method:: monthdatescalendar(year, month)
  44. Return a list of the weeks in the month *month* of the *year* as full
  45. weeks. Weeks are lists of seven :class:`datetime.date` objects.
  46. .. method:: monthdays2calendar(year, month)
  47. Return a list of the weeks in the month *month* of the *year* as full
  48. weeks. Weeks are lists of seven tuples of day numbers and weekday
  49. numbers.
  50. .. method:: monthdayscalendar(year, month)
  51. Return a list of the weeks in the month *month* of the *year* as full
  52. weeks. Weeks are lists of seven day numbers.
  53. .. method:: yeardatescalendar(year[, width])
  54. Return the data for the specified year ready for formatting. The return
  55. value is a list of month rows. Each month row contains up to *width*
  56. months (defaulting to 3). Each month contains between 4 and 6 weeks and
  57. each week contains 1--7 days. Days are :class:`datetime.date` objects.
  58. .. method:: yeardays2calendar(year[, width])
  59. Return the data for the specified year ready for formatting (similar to
  60. :meth:`yeardatescalendar`). Entries in the week lists are tuples of day
  61. numbers and weekday numbers. Day numbers outside this month are zero.
  62. .. method:: yeardayscalendar(year[, width])
  63. Return the data for the specified year ready for formatting (similar to
  64. :meth:`yeardatescalendar`). Entries in the week lists are day numbers. Day
  65. numbers outside this month are zero.
  66. .. class:: TextCalendar([firstweekday])
  67. This class can be used to generate plain text calendars.
  68. .. versionadded:: 2.5
  69. :class:`TextCalendar` instances have the following methods:
  70. .. method:: formatmonth(theyear, themonth[, w[, l]])
  71. Return a month's calendar in a multi-line string. If *w* is provided, it
  72. specifies the width of the date columns, which are centered. If *l* is
  73. given, it specifies the number of lines that each week will use. Depends
  74. on the first weekday as specified in the constructor or set by the
  75. :meth:`setfirstweekday` method.
  76. .. method:: prmonth(theyear, themonth[, w[, l]])
  77. Print a month's calendar as returned by :meth:`formatmonth`.
  78. .. method:: formatyear(theyear, themonth[, w[, l[, c[, m]]]])
  79. Return a *m*-column calendar for an entire year as a multi-line string.
  80. Optional parameters *w*, *l*, and *c* are for date column width, lines per
  81. week, and number of spaces between month columns, respectively. Depends on
  82. the first weekday as specified in the constructor or set by the
  83. :meth:`setfirstweekday` method. The earliest year for which a calendar
  84. can be generated is platform-dependent.
  85. .. method:: pryear(theyear[, w[, l[, c[, m]]]])
  86. Print the calendar for an entire year as returned by :meth:`formatyear`.
  87. .. class:: HTMLCalendar([firstweekday])
  88. This class can be used to generate HTML calendars.
  89. .. versionadded:: 2.5
  90. :class:`HTMLCalendar` instances have the following methods:
  91. .. method:: formatmonth(theyear, themonth[, withyear])
  92. Return a month's calendar as an HTML table. If *withyear* is true the year
  93. will be included in the header, otherwise just the month name will be
  94. used.
  95. .. method:: formatyear(theyear, themonth[, width])
  96. Return a year's calendar as an HTML table. *width* (defaulting to 3)
  97. specifies the number of months per row.
  98. .. method:: formatyearpage(theyear[, width[, css[, encoding]]])
  99. Return a year's calendar as a complete HTML page. *width* (defaulting to
  100. 3) specifies the number of months per row. *css* is the name for the
  101. cascading style sheet to be used. :const:`None` can be passed if no style
  102. sheet should be used. *encoding* specifies the encoding to be used for the
  103. output (defaulting to the system default encoding).
  104. .. class:: LocaleTextCalendar([firstweekday[, locale]])
  105. This subclass of :class:`TextCalendar` can be passed a locale name in the
  106. constructor and will return month and weekday names in the specified
  107. locale. If this locale includes an encoding all strings containing month and
  108. weekday names will be returned as unicode.
  109. .. versionadded:: 2.5
  110. .. class:: LocaleHTMLCalendar([firstweekday[, locale]])
  111. This subclass of :class:`HTMLCalendar` can be passed a locale name in the
  112. constructor and will return month and weekday names in the specified
  113. locale. If this locale includes an encoding all strings containing month and
  114. weekday names will be returned as unicode.
  115. .. versionadded:: 2.5
  116. For simple text calendars this module provides the following functions.
  117. .. function:: setfirstweekday(weekday)
  118. Sets the weekday (``0`` is Monday, ``6`` is Sunday) to start each week. The
  119. values :const:`MONDAY`, :const:`TUESDAY`, :const:`WEDNESDAY`, :const:`THURSDAY`,
  120. :const:`FRIDAY`, :const:`SATURDAY`, and :const:`SUNDAY` are provided for
  121. convenience. For example, to set the first weekday to Sunday::
  122. import calendar
  123. calendar.setfirstweekday(calendar.SUNDAY)
  124. .. versionadded:: 2.0
  125. .. function:: firstweekday()
  126. Returns the current setting for the weekday to start each week.
  127. .. versionadded:: 2.0
  128. .. function:: isleap(year)
  129. Returns :const:`True` if *year* is a leap year, otherwise :const:`False`.
  130. .. function:: leapdays(y1, y2)
  131. Returns the number of leap years in the range from *y1* to *y2* (exclusive),
  132. where *y1* and *y2* are years.
  133. .. versionchanged:: 2.0
  134. This function didn't work for ranges spanning a century change in Python
  135. 1.5.2.
  136. .. function:: weekday(year, month, day)
  137. Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
  138. *month* (``1``--``12``), *day* (``1``--``31``).
  139. .. function:: weekheader(n)
  140. Return a header containing abbreviated weekday names. *n* specifies the width in
  141. characters for one weekday.
  142. .. function:: monthrange(year, month)
  143. Returns weekday of first day of the month and number of days in month, for the
  144. specified *year* and *month*.
  145. .. function:: monthcalendar(year, month)
  146. Returns a matrix representing a month's calendar. Each row represents a week;
  147. days outside of the month a represented by zeros. Each week begins with Monday
  148. unless set by :func:`setfirstweekday`.
  149. .. function:: prmonth(theyear, themonth[, w[, l]])
  150. Prints a month's calendar as returned by :func:`month`.
  151. .. function:: month(theyear, themonth[, w[, l]])
  152. Returns a month's calendar in a multi-line string using the :meth:`formatmonth`
  153. of the :class:`TextCalendar` class.
  154. .. versionadded:: 2.0
  155. .. function:: prcal(year[, w[, l[c]]])
  156. Prints the calendar for an entire year as returned by :func:`calendar`.
  157. .. function:: calendar(year[, w[, l[c]]])
  158. Returns a 3-column calendar for an entire year as a multi-line string using the
  159. :meth:`formatyear` of the :class:`TextCalendar` class.
  160. .. versionadded:: 2.0
  161. .. function:: timegm(tuple)
  162. An unrelated but handy function that takes a time tuple such as returned by the
  163. :func:`gmtime` function in the :mod:`time` module, and returns the corresponding
  164. Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In
  165. fact, :func:`time.gmtime` and :func:`timegm` are each others' inverse.
  166. .. versionadded:: 2.0
  167. The :mod:`calendar` module exports the following data attributes:
  168. .. data:: day_name
  169. An array that represents the days of the week in the current locale.
  170. .. data:: day_abbr
  171. An array that represents the abbreviated days of the week in the current locale.
  172. .. data:: month_name
  173. An array that represents the months of the year in the current locale. This
  174. follows normal convention of January being month number 1, so it has a length of
  175. 13 and ``month_name[0]`` is the empty string.
  176. .. data:: month_abbr
  177. An array that represents the abbreviated months of the year in the current
  178. locale. This follows normal convention of January being month number 1, so it
  179. has a length of 13 and ``month_abbr[0]`` is the empty string.
  180. .. seealso::
  181. Module :mod:`datetime`
  182. Object-oriented interface to dates and times with similar functionality to the
  183. :mod:`time` module.
  184. Module :mod:`time`
  185. Low-level time related functions.