"""
Days_till.py Assignment 3, CIS 210, Fall 2012
Authors: Audrey Stroh, with help from Caitlin Hennessy
Credits: Caitlin Hennessy was my programming partner and we bounced ideas off
of eachother, but our programs look DRASTICALLY different.
10/11/2012
Determines how many days are between two given dates between
1/1/1800 and 12/31/2200
"""
DAYS_IN_MONTH = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
def is_date_valid(month, day, year):
"Determines whether a given date is valid or not."""
if month>12:
return False
if month!=2:
if DAYS_IN_MONTH[month]>=day:
return True
else:
return False
if month == 2:
if is_leap_year(year):
if day<=29:
return True
else:
return False
else:
if DAYS_IN_MONTH[month]>=day:
return True
def get_dates():
"""Gets user input for two dates"""
m1= int(input("Please enter the start month as an integer: "))
d1 = int(input("Please enter the day of the start month as an integer: "))
y1 = int(input("Please enter the start year as an integer: "))
if not is_date_valid(m1, d1, y1):
print("Sorry, that month and day don't look right.")
exit(1)
if y1<1800 or y1>2200:
print("Sorry, I need a valid date between 1/1/1800 and 12/31/2200: ")
exit(1)
m2 = int(input("Please enter the end month as an integer: "))
d2 = int(input("Please enter the day of the end month as an integer: "))
if m2>m1:
#the end month is in the same year as the start month. Check y1 to see if it is a leap year and if 2/29 is a valid date.
if not is_date_valid(m2,d2,y1) and (m2, d2) == (2, 29):
m2, d2 = 3, 1
if not is_date_valid(m2, d2, y1):
print("Sorry, that month and day don't look right.")
exit(1)
if m2<m1:
#end month is in the next year. Must check (y1+1) to see if it is a leap year.
if not is_date_valid(m2, d2, y1+1) and (m2, d2) == (2, 29):
m2, d2 = 3, 1
if not is_date_valid(m2, d2, y1+1):
print("Sorry, that month and day don't look right.")
exit(1)
if m2==m1 and d2>d1:
#in same month of same year
if not is_date_valid(m2, d2, y1) and (m2, d2) == (2, 29):
m2, d2 = 3, 1
if not is_date_valid(m2, d2, y1):
print("Sorry, that month and day don't look right.")
exit(1)
if m2==m1 and d2<d1:
#in next year
if not is_date_valid(m2, d2, y1+1) and (m2, d2) == (2, 29):
m2, d2 = 3, 1
if not is_date_valid(m2, d2, y1+1):
print("Sorry, that month and day don't look right.")
exit(1)
return m1, d1, y1, m2, d2
def is_leap_year(year):
"""Determines whether or not a given year is a leap year."""
year=int(year)
if year%400==0:
return True
if year%100==0:
return False
if year%4==0:
return True
else:
return False
def days_left_in_month(month, day, year):
"""Calculates the remaining number of days in a given month."""
if is_leap_year(year) and month==2:
total=29
else:
total = DAYS_IN_MONTH[month]
days_left = total-day
return(days_left)
def get_difference():
"""Calculates how many days between two dates"""
m1, d1, y1, m2, d2 = get_dates()
total=days_left_in_month(m1, d1, y1)
cur_month=m1+1
if cur_month==13:
cur_month=1
if (m1==m2 and d2>=d1):
total=d2-d1
else:
total=days_left_in_month(m1, d1, y1)
cur_month=m1+1
cur_year=y1
month_reset=0
if cur_month==13:
cur_month=1
cur_year += 1
while cur_month != m2:
if (cur_month==2) and (is_leap_year(cur_year)):
total += 29
cur_month+=1
else:
total += DAYS_IN_MONTH[cur_month]
cur_month += 1
if cur_month==13:
cur_month=1
cur_year += 1
total += d2
print(total, "days to go")
get_difference()
No comments:
Post a Comment