## This program calculates a new date given an old date and ## a number of days in the future import time months = ['January','February','March','April','May', 'June', \ 'July', 'August', 'September', 'October','November','December'] weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'] months_30 = ['April','June','September','November'] anchor_sunday = ['November', 14, 2010] anchor_year = 2010 def is_leap_year(year): ## let's assume that years divisible by 4 are leap years return((year % 4) == 0) def calculate_day_number(month, day, year): leap_days = 0 day_total = 0 month_number = months.index(month) if is_leap_year(year) and \ (((month_number == 1) and (day > 28)) \ or (month_number >= 2)): leap_days = 1 for month in months[:month_number]: if month == 'February': day_total = day_total + 28 + leap_days elif month in months_30: day_total = day_total + 30 else: day_total = day_total + 31 day_total = day_total+ day return(day_total) def year_days(year): if is_leap_year(year): return(366) else: return(365) anchor_day = calculate_day_number(anchor_sunday[0],anchor_sunday[1],anchor_sunday[2]) def get_day_difference(new_year, old_year, new_day, old_day): ## print('Get_day_differrence: ',new_year,old_year,new_day,old_day) ## Used for debugging import math days = 0 max_year = max(new_year,old_year) min_year = min(new_year,old_year) if old_year == new_year: ## if the same year, subtract days return (new_day - old_day) else: if max_year==new_year: max_day = new_day min_day = old_day else: max_day = old_day min_day = new_day if (is_leap_year(min_year) and (min_day < 59)): days = 366 - min_day else: days = 365 - min_day ## set days to the days left in the current year for year in range(min_year+1, max_year): ## add on the number of days in each year in between if is_leap_year(year): days = days + 366 days = days + 365 days = days + max_day ## add the days into the final year if new_day == max_day: return(days) else: return(0 - days) def remaining_days_after_year(year, days_left): if is_leap_year(year): return(days_left > 366) else: return(days_left>365) def days_in_month(month,year): if month=='February': if is_leap_year(year): return(29) else: return(28) elif month in months_30: return(30) else: return(31) def day_in_future(month, day, year, future): today = calculate_day_number(month,day, year) future_day = today + future ## anchor day is a known Sunday days_from_anchor = get_day_difference(year, \ anchor_year, \ today, \ anchor_day) today_day_of_week = days_from_anchor%7 future_day_of_week_number = (today_day_of_week+future)%7 future_day_of_week = weekdays[future_day_of_week_number] days_left = future_day new_year = year while remaining_days_after_year(year, days_left): ## remaining_days_after_year = boolean function ## that determines if there are any more days in ## year if is_leap_year(new_year): days_left=days_left-366 else: days_left=days_left-365 new_year=new_year+1 new_month = 'January' days_in_current_month = days_in_month(new_month,year) month_number = 0 while days_in_current_month < days_left: days_left = days_left-days_in_current_month month_number = (month_number + 1) new_month = months[month_number] days_in_current_month = days_in_month(new_month,year) return([new_year,new_month,days_left,future_day_of_week]) def main(): input_string = input('Please enter the current date \ in the form month/day/year: ') input_list = input_string.split('/') input_list[0] = int(input_list[0]) input_list[1] = int(input_list[1]) input_list[2] = int(input_list[2]) month = months[input_list[0]-1] ## Human month 1 = index 0 day = input_list[1] year = input_list[2] print('You entered', month, str(day)+',',year) future = int(input('How many days in the future would \ you like your appointment? ')) new_day_list = day_in_future(month,day,year,future) new_year=new_day_list[0] new_month=new_day_list[1] new_day=new_day_list[2] week_day=new_day_list[3] print('Your appointment will be on',week_day,\ new_month,str(new_day)+',',new_year) time.sleep(5) main()