class Date(object):
def __init__(self, month, day, year):
self._month = month
self._day = day
self._year = year
def leap_year(self):
if self._year%4 == 0:
if self._year%100 == 0:
if self._year%400 == 0:
return True # divisible by 4 and century and also divisible by 400
else:
return False # divisible by 4 and century and also not divisible by 400
else:
return True # divisible by 4 and not a century
else:
return False # not divisible by 4
def tomorrow(self):
if self._month in [1,3,5,7,8,10]:
if self._day < 31:
return Date(self._month,self._day+1,self._year)
else:
return Date(self._month+1,1,self._year)
elif self._month in [4,6,9,11]:
if self._day < 30:
return Date(self._month,self._day+1,self._year)
else:
return Date(self._month+1,1,self._year)
elif self._month == 12:
if self._day == 31:
return Date(1,1,self._year+1)
else:
return Date(self._month,self._day+1,self._year)
elif self._month == 2:
if self.leap_year():
if self._day == 29:
return Date(self._month+1,1,self._year)
else:
return Date(self._month,self._day+1,self._year)
else:
if self._day == 28:
return Date(self._month+1,1,self._year)
else:
return Date(self._month,self._day+1,self._year)
else:
return None
def add(self,ndays):
d = Date(self._month,self._day,self._year)
for i in range(ndays):
d = d.tomorrow()
return d
def __str__(self):
sMonths = "01January:02February:03March:04April:05May:06June:" + \
"07July:08August:09September:10October:11November:12December:"
m = "0"+str(self._month) if (self._month<10) else ""+str(self._month)
i = sMonths.find(m)
j = sMonths.find(":",i)
sMonth = sMonths[i+2:j]
return str(self._day) + " " + sMonth + ", " + str(self._year)
d1 = Date(10,28,1958)
d2 = d1.tomorrow()
print(d1)
print(d2)
d3 = d2.tomorrow().tomorrow().tomorrow()
print(d3)
e1 = Date(1,2,2400)
e1.leap_year()
print(d1.add(1000))
print(d1.add(100))
def main():
print("Please give me a valid date.")
m = int(input("Enter month (<= 12): "))
d = int(input("Enter day (<= 31) : "))
y = int(input("Enter year (> 0) : "))
day = Date(m,d,y)
print("\nThe date you entered is " + str(day))
while True:
menu()
option = input("Enter your option: ").lower()
if option == "a":
n = int(input("Enter number of days to add to the date: "))
print("\nThe date obtained after adding " + str(n) + " to " + str(day) + " is " + str(day.add(n)))
elif option == "s":
n = int(input("Enter number of days to subtract from the date: "))
print("\nThe date obtained after subtracting " + str(n) + " from " + str(day) + " is " + str(day.sub(n)))
elif option == "y":
print("\nThe date previous to " + str(day) + " is " + str(day.yesterday()))
elif option == "t":
print("\nThe date after " + str(day) + " is " + str(day.tomorrow()))
elif option == "n":
print("\nThe first day of the next month for " + str(day) + " is " + str(day.first_of_next_month()))
elif option == "p":
print("\nThe first day of the previous month for " + str(day) + " is " + str(day.first_of_previous_month()))
elif option == "b":
print("Please give me another valid date.")
m2 = int(input("Enter month (<= 12): "))
d2 = int(input("Enter day (<= 31) : "))
y2 = int(input("Enter year (> 0) : "))
day2 = Date(m2,d2,y2)
print("\nThe number of days between " + str(day) + " and " + str(day2) + " is " + str(day.days_between(day2)))
elif option == "c":
print("Please give me another valid date.")
m3 = int(input("Enter month (<= 12): "))
d3 = int(input("Enter day (<= 31) : "))
y3 = int(input("Enter year (> 0) : "))
day3 = Date(m3,d3,y3)
if day.before(day3):
print("\n" + str(day) + " is before " + str(day3))
elif day.after(day3):
print("\n" + str(day) + " is after " + str(day3))
else:
print("\n" + str(day) + " is same as " + str(day3))
elif option == "q":
break
def menu():
print("\n Welcome to Date Tester\n")
print("(a) Add number to the date. ")
print("(s) Subtract number from date. ")
print("(y) Yesterday. ")
print("(t) Tomorrow. ")
print("(n) First of next month. ")
print("(p) First of previous month. ")
print("(b) Days between. ")
print("(c) Compare Dates to see if one is before another. ")
print("(q) Quit. ")
print()
main()