0%

python日历

python日历

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
import numpy as np 
def getYear():
year = input("Please enter year: ")
return int(year)

def firstDay(year):
k = leapYear(year)
n = (year - 1900) * 365 + k
return (n + 1) % 7

def printCalendar(year, w):
print("============" + str(year) + "============")
first = w
for month in range(12):
heading(month)
first = oneMonth(year, month, first)


def leapYear(year):
count = 0
for y in range(1900, year):
if y%4 == 0 and (y%100 != 0 or y%400 == 0):
count += 1
return count

def heading(month):
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"]
print(" %s " % (months[month]))
print(" Mon Tue Wed Thu Fri Sat Sun")

def oneMonth(year, month, first):
d = days(year, month)
frame = layout(first, d)
printMonth(frame)
return (first + d) % 7


def days(year, month):
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
d = month_days[month]
if (month == 1) and (year%4 == 0 and (year%100 != 0 or year%400 == 0)):
d += 1
return d

def layout(first, d):
frame = 42 * [""] # 6 weeks: cell( (6 + 31)/7 )=6
if (first == 0):
first = 7
j = first - 1
for i in range(1, d + 1): # from 1 to d
frame[j] = i
j += 1
return frame

def printMonth(frame):
for i in range(len(frame)):
print("%4s" % frame[i], end='', flush=True)
if (i + 1)%7 == 0:
print("\n", end='', flush=True)


def main():
year = getYear()
w = firstDay(year)
printCalendar(year, w)

# if __name__ == "main":
main()