2020-05-15 calendar函数 python复刻

import sys
3
4 from datetime import datetime
5
6
7 def is_leap(year):
8 return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
9
10
11 def days_of_month(year, month):
12 days = [None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
13 total = days[month]
14 return total + 1 if month == 2 and is_leap(year) else total
15
16
17 def main():
18 if len(sys.argv) == 3:
19 month, year = map(int, sys.argv[1:])
20 else:
21 curr_date = datetime.now()
22 year, month = curr_date.year, curr_date.month
23 y, m = (year, month) if month > 2 else (year - 1, month + 12)
24 c, y = y // 100, y % 100
25 w = y + y // 4 + c // 4 - 2 * c + 26 * (m + 1) // 10
26 w %= 7
27 month_names = [
28 '', 'January', 'Feburary', 'March', 'April', 'May', 'June',
29 'July', 'August', 'September', 'October', 'November', 'December'
30 ]
31 print(f'{month_names[month]} {year}'.center(20))
32 print('Su Mo Tu We Th Fr Sa')
33 print(' ' * w * 3, end='')
34 days = days_of_month(year, month)
35 for day in range(1, days + 1):
36 print(f'{day}'.rjust(2), end=' ')
37 w += 1
38 if w == 7:
39 w = 0
40 print()

你可能感兴趣的:(2020-05-15 calendar函数 python复刻)