-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.py
64 lines (53 loc) · 1.67 KB
/
Test.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
def dayNum(m, d, y):
dayFormula = 31*(m-1) + d
febVar = (4*m + 23)//10
if m<3:
return dayFormula
elif m>2:
if y%4 == 0: #Possible leap year
if y%100==0 and y%400 !=0:
return dayFormula-febVar #Not a leap year
else:
return dayFormula-febVar + 1 #A leap year
else:
return dayFormula-febVar
def verifyDate(dateStr):
day = eval(dateStr[0])
month = eval(dateStr[1])
year = eval(dateStr[2])
month31 = [1, 3 , 5, 7, 8, 10, 12]
month30 = [4, 6, 9, 11]
if month <=12 and month >=1:
#For month = 2 or febuary
if month == 2:
if year%4==0:
if year%100==0 and year%400!=0 and day==29:
return "INVALID DATE"
elif day<=29 and day>=1:
return dayNum(month, day, year)
else:
return "INVALID DATE"
elif day<=28:
return dayNum(month, day, year)
else:
return "INVALID DATE"
# If month = 31 days
elif month in month31:
if day<=31 and day>=1:
return dayNum(month, day, year)
else:
return "INVALID DATE"
# If month = 30 days
elif month in month30:
if day<=30 and day>=1:
return dayNum(month, day, year)
else:
return "INVALID DATE"
else: # Error with month
return "INVALID DATE"
def main():
print("Accepts a date in d/m/y format and outputs if it is valid and the day's number.")
dateStr = input("Please enter date(d/m/y): ")
dateList = dateStr.split("/")
print("That day is {0}.".format(verifyDate(dateList)))
main()