calendar — General calendar-related functions¶
Source code: Lib/calendar.py
This module allows you to output calendars like the Unix cal program,
and provides additional useful functions related to the calendar. By default,
these calendars have Monday as the first day of the week, and Sunday as the last
(the European convention). Use setfirstweekday() to set the first day of
the week to Sunday (6) or to any other weekday. Parameters that specify dates
are given as integers. For related
functionality, see also the datetime and time modules.
The functions and classes defined in this module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book “Calendrical Calculations”, where it’s the base calendar for all computations. Zero and negative years are interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is 2 BC, and so on.
- class calendar.Calendar(firstweekday=0)¶
Creates a
Calendarobject. firstweekday is an integer specifying the first day of the week.MONDAYis0(the default),SUNDAYis6.A
Calendarobject provides several methods that can be used for preparing the calendar data for formatting. This class doesn’t do any formatting itself. This is the job of subclasses.Calendarinstances have the following methods and attributes:- firstweekday¶
The first weekday as an integer (0–6).
This property can also be set and read using
setfirstweekday()andgetfirstweekday()respectively.
- getfirstweekday()¶
Return an
intfor the current first weekday (0–6).Identical to reading the
firstweekdayproperty.
- setfirstweekday(firstweekday)¶
Set the first weekday to firstweekday, passed as an
int(0–6).Identical to setting the
firstweekdayproperty.
- iterweekdays()¶
Return an iterator for the weekday numbers that will be used for one week. The first value from the iterator will be the same as the value of the
firstweekdayproperty.
- itermonthdates(year, month)¶
Return an iterator for the month month (1–12) in the year year. This iterator will return all days (as
datetime.dateobjects) for the month and all days before the start of the month or after the end of the month that are required to get a complete week.
- itermonthdays(year, month)¶
Return an iterator for the month month in the year year similar to
itermonthdates(), but not restricted by thedatetime.daterange. Days returned will simply be day of the month numbers. For the days outside of the specified month, the day number is0.
- itermonthdays2(year, month)¶
Return an iterator for the month month in the year year similar to
itermonthdates(), but not restricted by thedatetime.daterange. Days returned will be tuples consisting of a day of the month number and a weekday number.
- itermonthdays3(year, month)¶
Return an iterator for the month month in the year year similar to
itermonthdates(), but not restricted by thedatetime.daterange. Days returned will be tuples consisting of a year, a month and a day of the month numbers.Added in version 3.7.
- itermonthdays4(year, month)¶
Return an iterator for the month month in the year year similar to
itermonthdates(), but not restricted by thedatetime.daterange. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers.Added in version 3.7.
- monthdatescalendar(year, month)¶
Return a list of the weeks in the month month of the year as full weeks. Weeks are lists of seven
datetime.dateobjects.
- monthdays2calendar(year, month)¶
Return a list of the weeks in the month month of the year as full weeks. Weeks are lists of seven tuples of day numbers and weekday numbers.
- monthdayscalendar(year, month)¶
Return a list of the weeks in the month month of the year as full weeks. Weeks are lists of seven day numbers.
- yeardatescalendar(year, width=3)¶
Return the data for the specified year ready for formatting. The return value is a list of month rows. Each month row contains up to width months (defaulting to 3). Each month contains between 4 and 6 weeks and each week contains 1–7 days. Days are
datetime.dateobjects.
- yeardays2calendar(year, width=3)¶
Return the data for the specified year ready for formatting (similar to
yeardatescalendar()). Entries in the week lists are tuples of day numbers and weekday numbers. Day numbers outside this month are zero.
- yeardayscalendar(year, width=3)¶
Return the data for the specified year ready for formatting (similar to
yeardatescalendar()). Entries in the week lists are day numbers. Day numbers outside this month are zero.
- class calendar.TextCalendar(firstweekday=0)¶
This class can be used to generate plain text calendars.
TextCalendarinstances have the following methods:- prweek(theweek, width)¶
Print a week’s calendar as returned by
formatweek()and without a final newline.
- formatday(day, weekday, width)¶
Return a string representing a single day formatted with the given width. If day is
0, return a string of spaces of the specified width, representing an empty day. The weekday parameter is unused.
- formatweek(theweek, width)¶
Return a single week in a string with no newline. width specifies the width of the date columns, which are centered. Depends on the first weekday as specified in the constructor or set by the
setfirstweekday()method.
- formatweekday(day, width)¶
Return a string representing the name of a single weekday formatted to the specified width. The day parameter is an integer representing the day of the week, where
0is Monday and6is Sunday.
- formatweekheader(width)¶
Return a string containing the header row of weekday names, formatted with the given width for each column. The names depend on the locale settings and are padded to the specified width.
- formatmonth(theyear, themonth, w=0, l=0)¶
Return a month’s calendar in a multi-line string. If w is provided, it specifies the width of the date columns, which are centered. If l is given, it specifies the number of lines that each week will use. Depends on the first weekday as specified in the constructor or set by the
setfirstweekday()method.
- formatmonthname(theyear, themonth, width, withyear=True)¶
Return a string representing the month’s name centered within the specified width. If withyear is
True, include the year in the output. The theyear and themonth parameters specify the year and month for the name to be formatted respectively.
- prmonth(theyear, themonth, w=0, l=0)¶
Print a month’s calendar as returned by
formatmonth().
- formatyear(theyear, w=2, l=1, c=6, m=3)¶
Return a m-column calendar for an entire year as a multi-line string. Optional parameters w, l, and c are for date column width, lines per week, and number of spaces between month columns, respectively. Depends on the first weekday as specified in the constructor or set by the
setfirstweekday()method. The earliest year for which a calendar can be generated is platform-dependent.
- pryear(theyear, w=2, l=1, c=6, m=3)¶
Print the calendar for an entire year as returned by
formatyear().
- class calendar.HTMLCalendar(firstweekday=0)¶
This class can be used to generate HTML calendars.
HTMLCalendarinstances have the following methods:- formatmonth(theyear, themonth, withyear=True)¶
Return a month’s calendar as an HTML table. If withyear is true the year will be included in the header, otherwise just the month name will be used.
- formatyear(theyear, width=3)¶
Return a year’s calendar as an HTML table. width (defaulting to 3) specifies the number of months per row.
- formatyearpage(theyear, width=3, css='calendar.css', encoding=None)¶
Return a year’s calendar as a complete HTML page. width (defaulting to 3) specifies the number of months per row. css is the name for the cascading style sheet to be used.
Nonecan be passed if no style sheet should be used. encoding specifies the encoding to be used for the output (defaulting to'utf-8').
- formatmonthname(theyear, themonth, withyear=True)¶
Return a month name as an HTML table row. If withyear is true the year will be included in the row, otherwise just the month name will be used.
HTMLCalendarhas the following attributes you can override to customize the CSS classes used by the calendar:- cssclasses¶
A list of CSS classes used for each weekday. The default class list is:
cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
more styles can be added for each day:
cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
Note that the length of this list must be seven items.
- cssclass_noday¶
The CSS class for a weekday occurring in the previous or coming month.
Added in version 3.7.
- cssclasses_weekday_head¶
A list of CSS classes used for weekday names in the header row. The default is the same as
cssclasses.Added in version 3.7.
- cssclass_month_head¶
The month’s head CSS class (used by
formatmonthname()). The default value is"month".Added in version 3.7.
- cssclass_month¶
The CSS class for the whole month’s table (used by
formatmonth()). The default value is