CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day31-35/code/mycal.py
Views: 729
1
#!/usr/bin/python3
2
from datetime import datetime
3
4
import sys
5
6
7
def is_leap(year):
8
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
9
10
11
def main():
12
if len(sys.argv) == 3:
13
month = int(sys.argv[1])
14
year = int(sys.argv[2])
15
else:
16
now = datetime.now()
17
date = now.date
18
month = now.month
19
year = now.year
20
m, y = (month, year) if month >= 3 else (month + 12, year - 1)
21
c, y = y // 100, y % 100
22
w = (y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10) % 7
23
month_words = [
24
'January', 'February', 'March', 'April', 'May', 'June',
25
'July', 'August', 'September', 'October', 'November', 'December'
26
]
27
print(f'{month_words[month - 1]} {year}'.center(20))
28
print('Su Mo Tu We Th Fr Sa')
29
print(' ' * 3 * w, end='')
30
days = [
31
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
32
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
33
][is_leap(year)][month - 1]
34
for day in range(1, days + 1):
35
print(str(day).rjust(2), end=' ')
36
w += 1
37
if w == 7:
38
print()
39
w = 0
40
print()
41
42
43
if __name__ == '__main__':
44
main()
45
46