Last active
September 30, 2023 08:38
-
-
Save dandrake/06ba0c10adb2e5e3ef020b260efe8aac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Print a "continuous" calendar -- as a ribbon / candy bar: | |
https://davidseah.com/node/compact-calendar/ | |
https://docs.python.org/3/library/calendar.html | |
Example output: | |
>>> calendar.setfirstweekday(1) | |
>>> print(domonths(2023, 9, 6)) | |
Mo Tu We Th Fr Sa Su | |
1 2 3 September 2023 | |
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 1 October 2023 | |
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 1 2 3 4 5 November 2023 | |
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 1 2 3 December 2023 | |
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 | |
1 2 3 4 5 6 7 January 2024 | |
8 9 10 11 12 13 14 | |
15 16 17 18 19 20 21 | |
22 23 24 25 26 27 28 | |
29 30 31 1 2 3 4 February 2024 | |
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 1 2 3 March 2024 | |
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 | |
""" | |
import calendar | |
def joinmonths(first, second): | |
firstlines = first.splitlines() | |
secondlines = second.splitlines()[2:] | |
monthname = ' ' + second.splitlines()[0].strip() | |
if len(firstlines[-1]) < 20: | |
# first month doesn't end on last day of week | |
separator = ' ' | |
else: | |
separator = '\n ' | |
join = firstlines[-1].rstrip() + separator + secondlines[0].lstrip() + monthname | |
return firstlines[:-1] + [join] + secondlines[1:] | |
def domonths(year, start_month, num_months): | |
thismonth = calendar.TextCalendar().formatmonth(year, start_month) | |
# save off the first month and year: | |
monthname = ' ' + thismonth.splitlines()[0].strip() | |
# concatenate the rest of the months on: | |
month = start_month | |
for i in range(start_month + 1, start_month + num_months + 1): | |
month += 1 | |
if month > 12: | |
month = 1 | |
year += 1 | |
nextmonth = calendar.TextCalendar().formatmonth(year, month) | |
thismonth = '\n'.join(joinmonths(thismonth, nextmonth)) | |
# put first month name back in and return | |
ret = thismonth.splitlines()[1:] | |
ret[1] += monthname | |
return '\n'.join(ret) | |
def doyear(year): | |
return domonths(year, 1, 12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment