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