-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL10Q31_FinishDaysBetweenDates.py
90 lines (80 loc) · 2.88 KB
/
L10Q31_FinishDaysBetweenDates.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
# Credit goes to Websten from forums
#
# Use Dave's suggestions to finish your daysBetweenDates
# procedure. It will need to take into account leap years
# in addition to the correct number of days in each month.
def isLeapYear(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return True
else:
return False
return False
def daysInMonth(year, month):
if month == 1 or month == 3 or month == 5 or month == 7 or month ==8 or month == 10 or month ==12:
return 31
else:
if month == 2:
if isLeapYear(year):
return 29
else:
return 28
else:
return 30
def nextDay(year, month, day):
if day < daysInMonth(year,month):
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
def dateIsBefore(year1, month1, day1, year2, month2, day2):
"""Returns True if year1-month1-day1 is before year2-month2-day2. Otherwise, returns False."""
if year1 < year2:
return True
if year1 == year2:
if month1 < month2:
return True
if month1 == month2:
return day1 < day2
return False
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
"""Returns the number of days between year1/month1/day1
and year2/month2/day2. Assumes inputs are valid dates
in Gregorian calendar."""
# program defensively! Add an assertion if the input is not valid!
assert not dateIsBefore(year2, month2, day2, year1, month1, day1)
days = 0
while dateIsBefore(year1, month1, day1, year2, month2, day2):
year1, month1, day1 = nextDay(year1, month1, day1)
days += 1
return days
def test():
assert daysBetweenDates(2013,1,1,2013,1,1) == 0
assert daysBetweenDates(2013,1,1,2013,1,2) == 1
assert nextDay(2013, 1, 1) == (2013, 1, 2)
assert nextDay(2013, 4, 30) == (2013, 5, 1)
assert nextDay(2012, 12, 31) == (2013, 1, 1)
assert nextDay(2013, 2, 28) == (2013, 3, 1)
assert nextDay(2013, 9, 30) == (2013, 10, 1)
assert nextDay(2012, 2, 28) == (2012, 2, 29)
assert nextDay(2013, 9, 30) == (2013, 10, 1)
assert daysBetweenDates(2013, 1,1,2014,1,1) == 365
assert daysBetweenDates(2012, 1,1,2013,1,1) == 366
print 'tests finished'
test_cases = [((2012, 1, 1, 2012, 2, 28), 58),
((2012, 1, 1, 2012, 3, 1), 60),
((2011, 6, 30, 2012, 6, 30), 366),
((2011, 1, 1, 2012, 8, 8), 585),
((1900, 1, 1, 1999, 12, 31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()