-
Notifications
You must be signed in to change notification settings - Fork 26
/
datediff.py
103 lines (84 loc) · 2.64 KB
/
datediff.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# cf. book 7.7 exercises 11-13
def yearLength(year):
if year % 400 == 0:
return 366
elif year % 100 == 0:
return 365
elif year % 4 == 0:
return 366
else:
return 365
def monthLength(year,month):
if month == 2 and yearLength(year) == 366:
return 29
elif month == 2:
return 28
elif month in [4,6,9,11]:
return 30
else:
return 31
def dateDays(year,month,day):
days = 0
for y in range(year):
days = days + yearLength(y)
for m in range(1,month):
days = days + monthLength(year,m)
return days + day
def dateDiff(year1,month1,day1,year2,month2,day2):
diff = dateDays(year2,month2,day2) - dateDays(year1,month1,day1)
return diff
#### new stuff added for Lecture 6
def checkDate(year,month,day):
return (0 < month <= 12) and (0 < day <= monthLength(year,month))
def inputDate(prompt):
date = (input(prompt + ", format år,månad,dag: ")).split(',')
if len(date) == 3 and date[0].isdigit() and date[1].isdigit() and date[2].isdigit():
year,month,day = int(date[0]),int(date[1]),int(date[2])
if checkDate(year,month,day):
return year,month,day
else:
print("ogiltigt datum")
return inputDate(prompt)
else:
print("försök igen, tre tal")
return inputDate(prompt)
#### end new stuff
### old versions step by step, not used in the actual main()
def inputDate1(date):
year,month,day = eval(input("Ange " + date + " i format år,månad,dag: "))
return year,month,day
def inputDate2(prompt):
year,month,day = input(prompt+": ").split(',')
return int(year),int(month),int(day)
def inputDate3(prompt):
date = (input(prompt + ": ")).split(',')
if len(date) == 3:
return int(date[0]),int(date[1]),int(date[2])
else:
print("ogiltigt datum")
return inputDate3(prompt)
def inputDate4(prompt):
date = (input(prompt + ": ")).split(',')
if (len(date) == 3
and date[0].isdigit()
and date[1].isdigit()
and date[2].isdigit()):
return int(date[0]),int(date[1]),int(date[2])
else:
print("ogiltigt datum")
return inputDate4(prompt)
##########
def main():
try:
year1,month1,day1 = inputDate("startdatum")
year2,month2,day2 = inputDate("slutdatum")
print("Skillnaden är", dateDiff(year1,month1,day1,year2,month2,day2))
except KeyboardInterrupt:
print()
print("om du vill lämna programmet, tryck ^D")
main()
except:
print()
print("Ok, hejdå")
if __name__ == '__main__':
main()