-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalpr.py
85 lines (70 loc) · 2.01 KB
/
calpr.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Print the calendar for a month
(without using the Python 'calendar' module).
Limitations: Treats February as always having 28 days.
Author: Austin Dayton, Mark Poliquin
Consulted with Mark on printing full and partial weeks. Much help on printing weeks from
GTF (name unknown at time of submission, in office hours with Anna on 1/15).
"""
import sys # For command-line arguments
import datetime # To determine what day of week a month
# begins on.
# Note: For this project, module calendar
# is not permitted. It basically has a function
# to do the whole assignment in one line.
USAGE = """
Usage: python3 calpr.py 12 2013
where 12 can be replaced by any month 1..12
and 2013 can be replaced by any year 1..2100
"""
if len(sys.argv) != 3:
print(USAGE)
exit(1)
month = int(sys.argv[1])
year = int(sys.argv[2])
MONTHLEN = [ 0, # No month zero
31, # 1. January
28, # 2. February (ignoring leap years)
31, # 3. March
30, # 4. April
31, # 5. May
30, # 6. June
31, # 7. July
31, # 8. August
30, # 9. September
31, #10. October
30, #11. November
31, #12. December
]
# What day of the week does year,month begin on?
a_date = datetime.date(year, month, 1)
starts_weekday = a_date.weekday()
## a_date.weekday() gives 0=Monday, 1=Tuesday, etc.
## Roll to start week on Sunday
starts_weekday = (1 + starts_weekday) % 7
month_day = 1 ## Next day to print
last_day = MONTHLEN[month] ## Last day to print
print(" Su Mo Tu We Th Fr Sa")
###
### The first (perhaps partial) week
###
for i in range(7):
if i < starts_weekday :
print(" ", end="")
else:
# Logic for printing one day, moving to next
print(format(month_day, "3d"), end="")
month_day += 1
print() # Newline
###print full weeks
while month_day <= (last_day-7):
for i in range(7):
# Logic for printing one day, moving to next
print(format(month_day, "3d"), end="")
month_day += 1
print()
##while days remain, print each day
while month_day <= last_day:
print(format(month_day, "3d"), end="")
month_day += 1
print()