| 40 | |
| 41 | |
| 42 | def getCalendarFor(year, month): |
| 43 | calText = '' # calText will contain the string of our calendar. |
| 44 | |
| 45 | # Put the month and year at the top of the calendar: |
| 46 | calText += (' ' * 34) + MONTHS[month - 1] + ' ' + str(year) + '\n' |
| 47 | |
| 48 | # Add the days of the week labels to the calendar: |
| 49 | # (!) Try changing this to abbreviations: SUN, MON, TUE, etc. |
| 50 | calText += '...Sunday.....Monday....Tuesday...Wednesday...Thursday....Friday....Saturday..\n' |
| 51 | |
| 52 | # The horizontal line string that separate weeks: |
| 53 | weekSeparator = ('+----------' * 7) + '+\n' |
| 54 | |
| 55 | # The blank rows have ten spaces in between the | day separators: |
| 56 | blankRow = ('| ' * 7) + '|\n' |
| 57 | |
| 58 | # Get the first date in the month. (The datetime module handles all |
| 59 | # the complicated calendar stuff for us here.) |
| 60 | currentDate = datetime.date(year, month, 1) |
| 61 | |
| 62 | # Roll back currentDate until it is Sunday. (weekday() returns 6 |
| 63 | # for Sunday, not 0.) |
| 64 | while currentDate.weekday() != 6: |
| 65 | currentDate -= datetime.timedelta(days=1) |
| 66 | |
| 67 | while True: # Loop over each week in the month. |
| 68 | calText += weekSeparator |
| 69 | |
| 70 | # dayNumberRow is the row with the day number labels: |
| 71 | dayNumberRow = '' |
| 72 | for i in range(7): |
| 73 | dayNumberLabel = str(currentDate.day).rjust(2) |
| 74 | dayNumberRow += '|' + dayNumberLabel + (' ' * 8) |
| 75 | currentDate += datetime.timedelta(days=1) # Go to next day. |
| 76 | dayNumberRow += '|\n' # Add the vertical line after Saturday. |
| 77 | |
| 78 | # Add the day number row and 3 blank rows to the calendar text. |
| 79 | calText += dayNumberRow |
| 80 | for i in range(3): # (!) Try changing the 4 to a 5 or 10. |
| 81 | calText += blankRow |
| 82 | |
| 83 | # Check if we're done with the month: |
| 84 | if currentDate.month != month: |
| 85 | break |
| 86 | |
| 87 | # Add the horizontal line at the very bottom of the calendar. |
| 88 | calText += weekSeparator |
| 89 | return calText |
| 90 | |
| 91 | |
| 92 | calText = getCalendarFor(year, month) |