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复刻
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...