This module allows you to output calendars like the Unix cal program, and provides additional useful functions related to the calendar.
-
iterweekdays( weekday) - Return an iterator for the week day numbers that will be used for one week. The first number from the iterator will be the same as the number returned by firstweekday().
for i in cal.iterweekdays(): print i
0
1
2
3
4
5
6
-
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.
for i in cal.itermonthdates(2008, 3): print i
2008-02-25
2008-02-26
2008-02-27
2008-02-28
2008-02-29
2008-03-01
2008-03-02
2008-03-03
2008-03-04
2008-03-05
...
-
itermonthdays2( year, month) - Return an iterator for the month month in the year year similar to itermonthdates(). Days returned will be tuples consisting of a day number and a week day number.
for i in cal.itermonthdays2(2008, 3): print i
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 5)
(2, 6)
(3, 0)
(4, 1)
(5, 2)
...
-
itermonthdays( year, month) - Return an iterator for the month month in the year year similar to itermonthdates(). Days returned will simply be day numbers.
for i in cal.itermonthdays(2008, 3): print i
0
0
0
0
0
1
2
3
4
5
[...]
-
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.
cal.monthdatescalendar(2008, 3)
[[datetime.date(2008, 2, 25), datetime.date(2008, 2, 26), datetime.date(2008,
2, 27), datetime.date(2008, 2, 28), datetime.date(2008, 2, 29),
datetime.date(2008, 3, 1), datetime.date(2008, 3, 2)], [datetime.date(2008, 3,
3), datetime.date(2008, 3, 4), datetime.date(2008, 3, 5), datetime.date(2008,
3, 6), datetime.date(2008, 3, 7), datetime.date(2008, 3, 8),
datetime.date(2008, 3, 9)], [datetime.date(2008, 3, 10), datetime.date(2008, 3,
11), datetime.date(2008, 3, 12), datetime.date(2008, 3, 13),
datetime.date(2008, 3, 14), datetime.date(2008, 3, 15), datetime.date(2008, 3,
16)], [datetime.date(2008, 3, 17), datetime.date(2008, 3, 18),
datetime.date(2008, 3, 19), datetime.date(2008, 3, 20), datetime.date(2008, 3,
21), datetime.date(2008, 3, 22), datetime.date(2008, 3, 23)],
[datetime.date(2008, 3, 24), datetime.date(2008, 3, 25), datetime.date(2008, 3,
26), datetime.date(2008, 3, 27), datetime.date(2008, 3, 28),
datetime.date(2008, 3, 29), datetime.date(2008, 3, 30)], [datetime.date(2008,
3, 31), datetime.date(2008, 4, 1), datetime.date(2008, 4, 2),
datetime.date(2008, 4, 3), datetime.date(2008, 4, 4), datetime.date(2008, 4,
5), datetime.date(2008, 4, 6)]]
-
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.
cal.monthdays2calendar(2008, 3)
[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)], [(3, 0), (4, 1),
(5, 2), (6, 3), (7, 4), (8, 5), (9, 6)], [(10, 0), (11, 1), (12, 2), (13,
3), (14, 4), (15, 5), (16, 6)], [(17, 0), (18, 1), (19, 2), (20, 3), (21,
4), (22, 5), (23, 6)], [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29,
5), (30, 6)], [(31, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]]
-
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.
cal.monthdayscalendar(2008,3)
[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15,
16], [17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30], [31, 0, 0,
0, 0, 0, 0]]
-
yeardatescalendar( year, month[, width]) - 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.
cal.yeardatescalendar(2008,3)
[[[[datetime.date(2007, 12, 31), datetime.date(2008, 1, 1),
datetime.date(2008, 1, 2), datetime.date(2008, 1, 3), [...]
-
yeardays2calendar( year, month[, width]) - 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.
cal.yeardays2calendar(2008,3)
[[[[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], [(7, 0), (8,
1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)], [(14, 0), (15, 1), (16,
2), (17, 3), (18, 4), (19, 5), (20, 6)], [(21, 0), (22, 1), (23, 2), (24,
3), (25, 4), (26, 5), (27, 6)], [...]
-
yeardayscalendar( year, month[, width]) - 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.
cal.yeardayscalendar(2008,3)
[[[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18,
19, 20], [21, 22, 23, 24, 25, 26, 27], [28, 29, 30, 31, 0, 0, 0]], [[0, 0,
0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18,
19, 20, 21, 22, 23, 24], [...]
TextCalendar instances have the following methods:
-
formatmonth( theyear, themonth[, w[, l]]) - 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 set by setfirstweekday().
print tc.formatmonth(2008,3)
March 2008
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30 32
-
prmonth( theyear, themonth[, w[, l]]) - Print a month's calendar as returned by formatmonth(). (see above).
-
formatyear( theyear, themonth[, w[, l[, c[, m]]]]) - 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 set by setfirstweekday(). The earliest year for which a calendar can be generated is platform-dependent.
print tc.formatyear(2008,3)
2008
January February March
Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6 1 2 3 1 2
7 8 9 10 11 12 13 4 5 6 7 8 9 10 3 4 5 6 7 8 9
14 15 16 17 18 19 20 11 12 13 14 15 16 17 10 11 12 13 14 15 16
21 22 23 24 25 26 27 18 19 20 21 22 23 24 17 18 19 20 21 22 23
28 29 30 31 25 26 27 28 29 24 25 26 27 28 29 30
31
[...]
-
pryear( theyear[, w[, l[, c[, m]]]]) - Print the calendar for an entire year as returned by formatyear(). (see above)
-
class HTMLCalendar( [firstweekday]) - This class can be used to generate HTML calendars.
New in version 2.5.
-
formatmonth( theyear, themonth[, withyear]) - 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.
hc.formatmonth(2008,3)
'<table border="0" cellpadding="0" cellspacing="0" class="month">\n<tr>
<th colspan="7" class="month">March 2008</th></tr>\n<tr>
<th class="mon">Mon</th><th class="tue">Tue</th>
<th class="wed">Wed</th><th class="thu">Thu</th>
<th class="fri">Fri</th><th class="sat">Sat</th>
<th class="sun">Sun</th></tr>\n<tr>
<td class="noday"> </td>
-
formatyear( theyear, themonth[, width]) - Return a year's calendar as an HTML table. width (defaulting to 3) specifies the number of months per row. (Same as text formatyear above but produces html table).
-
formatyearpage( theyear, themonth[, width[, css[, encoding]]]) - 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. None can be passed if no style sheet should be used. encoding specifies the encoding to be used for the output (defaulting to the system default encoding). (Same as text formatyear above but produces html table).
-
class LocaleTextCalendar( [firstweekday[, locale]]) - This subclass of TextCalendar can be passed a locale name in the constructor and will return month and weekday names in the specified locale. If this locale includes an encoding all strings containing month and weekday names will be returned as unicode. New in version 2.5.
-
class LocaleHTMLCalendar( [firstweekday[, locale]]) - This subclass of HTMLCalendar can be passed a locale name in the constructor and will return month and weekday names in the specified locale. If this locale includes an encoding all strings containing month and weekday names will be returned as unicode. New in version 2.5.
-
setfirstweekday( weekday) - Sets the weekday (0 is Monday, 6 is Sunday) to start each week. The values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are provided for convenience. For example, to set the first weekday to Sunday:
import calendar
firstweekday() # 0 [monday by default]
calendar.setfirstweekday(calendar.SUNDAY)
firstweekday() # 6
-
firstweekday( ) - Returns the current setting for the weekday to start each week. New in version 2.0.
firstweekday() # 0
-
isleap( year) - Returns True if year is a leap year, otherwise False.
isleap(2008) # True
-
leapdays( y1, y2) - Returns the number of leap years in the range [y1...y2), where y1 and y2 are years. Changed in version 2.0: This function didn't work for ranges spanning a century change in Python 1.5.2.
leapdays(2000,2008) # 2
-
weekday( year, month, day) - Returns the day of the week (0 is Monday) for year (1970-...), month (1-12), day (1-31).
weekday(2008,3,2) # 6
-
weekheader( n) - Return a header containing abbreviated weekday names. n specifies the width in characters for one weekday.
weekheader(5) # ' Mon Tue Wed Thu Fri Sat Sun '
-
monthrange( year, month) - Returns weekday of first day of the month and number of days in month, for the specified year and month.
monthrange(2008,3) # (5, 31)
-
monthcalendar( year, month) - Returns a matrix representing a month's calendar. Each row represents a week; days outside of the month a represented by zeros. Each week begins with Monday unless set by setfirstweekday().
monthcalendar(2008,3)
[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30], [31, 0, 0, 0, 0, 0, 0]]
-
prmonth( theyear, themonth[, w[, l]]) - Prints a month's calendar as returned by month().
-
month( theyear, themonth[, w[, l]]) - Returns a month's calendar in a multi-line string using the formatmonth of the TextCalendar class. New in version 2.0.
print month(2008,3)
March 2008
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
-
prcal( year[, w[, l[c]]]) - Prints the calendar for an entire year as returned by calendar().
prcal(2008)
2008
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 1 2
7 8 9 10 11 12 13 4 5 6 7 8 9 10 3 4 5 6 7 8 9
14 15 16 17 18 19 20 11 12 13 14 15 16 17 10 11 12 13 14 15 16
21 22 23 24 25 26 27 18 19 20 21 22 23 24 17 18 19 20 21 22 23
28 29 30 31 25 26 27 28 29 24 25 26 27 28 29 30
31
-
calendar( year[, w[, l[c]]]) - Returns a 3-column calendar for an entire year as a multi-line string using the formatyear of the TextCalendar class. New in version 2.0. (see above)
-
timegm( tuple) - An unrelated but handy function that takes a time tuple such as returned by the gmtime() function in the time module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In fact, time.gmtime() and timegm() are each others' inverse. New in version 2.0.
timegm(time.gmtime()) # 1207010863
index
