calendar — General calendar-related functions — Python 3.16.0a2 documentation
Navigation index modules | next | previous | Python » 3.16.0a2 documentation » The Python standard library » Data Types » calendar — General calendar-related functions | Theme | 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 Calendar object. firstweekday is an integer specifying the first day of the week. MONDAY is 0 (the default), SUNDAY is 6 . A Calendar object 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. Calendar instances 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() and getfirstweekday() respectively. getfirstweekday ( ) ¶ Return an int for the current first weekday (0–6). Identical to reading the firstweekday property. setfirstweekday ( firstweekday ) ¶ Set the first weekday to firstweekday , passed as an int (0–6).
Identical to setting the firstweekday property. 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 firstweekday property. itermonthdates ( year , month ) ¶ Return an iterator for the month month (1–12) in the year year . This iterator will return all days (as datetime.date objects) 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 the datetime.date range.
Days returned will simply be day of the month numbers. For the days outside of the specified month, the day number is 0 . itermonthdays2 ( year , month ) ¶ Return an iterator for the month month in the year year similar to itermonthdates() , but not restricted by the datetime.date range. 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 the datetime.date range.
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 the datetime.date range. 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.date objects. 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.date objects. 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. TextCalendar instances 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 0 is Monday and 6 is 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. HTMLCalendar instances 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 mon
The rest of this page is on the live site.