Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A script for calculating the difference between two dates in years, months, and days.
- # Function definitions:
- # Function to ask user for dates and assign them to variables.
- def input_dates():
- print "What is the start date?"
- # gets user input for start date in 3 separate variables
- start_month = raw_input("Month: ")
- start_day = raw_input("Day: ")
- start_year = raw_input("Year: ")
- print "\nThat's a dope ass date. Now what's the end date?"
- # gets user input for end date in 3 separate variables
- end_month = raw_input("Month: ")
- end_day = raw_input("Day: ")
- end_year = raw_input("Year: ")
- start_date = [start_month, start_day, start_year] # stores start date variables in a single list
- end_date = [end_month, end_day, end_year] # stores end date variables in a single list
- return (start_date, end_date) # returns start date and end date as lists
- # Function to find the difference between two dates
- def date_difference(date1, date2):
- # assign variables to start year, end year, and their difference
- year1 = int(date1[2])
- year2 = int(date2[2])
- year_diff = year2 - year1
- # assign variables for days
- day1 = int(date1[1])
- day2 = int(date2[1])
- # assign a list variable containing all months to reference via index
- months = ["January", "February", "March", "April", "May", "June", "July",
- "August", "September", "October", "November", "December"]
- # assign string variable to user month inputs
- month1 = date1[0] # defines start month as first string in start date list
- month2 = date2[0] # defines end month as first string in end date list
- # iterates through months list until matching month is found for start month
- for month in months:
- if month != month1:
- continue
- month_num1 = int(months.index(month)) # assigns integer value to month after match is found
- # iterates through months list until matching month is found for end month
- for month in months:
- if month != month2:
- continue
- month_num2 = int(months.index(month)) # assigns integer value to month after match is found
- # print month_num1 # check loop
- # print month_num2 # check loop
- # finds month difference.
- if month_num2 < month_num1: # accounts for case when end month precedes start month in end year
- month_diff = 12 - (month_num1 - month_num2)
- year_diff -= 1 # subtracts one year from total if end month precedes start month in end year
- elif month_num2 > month_num1:
- month_diff = month_num2 - month_num1
- else: # special cases for when the start month and end month are the same
- if day2 >= day1:
- month_diff = 0
- else:
- month_diff = 11
- year_diff -= 1
- # assigns a value for the number of days in the month prior to the end month. necessary to calculate day difference
- if month_num2 in (0,1,3,5,7,8,10):
- month_len = 31
- elif month_num2 in (4,6,9,11):
- month_len = 30
- else: # checks if leap year for February days
- if (year2 % 4 == 0 and year2 % 100 != 0) or year2 % 400 == 0: # this is the necessary condition for a leap year to exist
- month_len = 29
- else:
- month_len = 28
- # finds day difference. accounts for preceding day numbers in later months e.g. june 20 to march 2
- if day2 < day1:
- day_diff = month_len + day2 - day1
- else:
- day_diff = day2 - day1
- # prints difference in years, months, and days.
- print "\nWow. Thats %d years, %d months, and %d days!\n" % (year_diff, month_diff, day_diff)
- # returns values to be used for total days calculation
- return (year_diff, day_diff, year1, year2, day1, day2, month_num1, month_num2, months)
- # used to check calculations and variable values for debugging
- """ # check variable values
- print month_num1
- print month_num2
- print month_len
- print year_diff
- print day1
- print day2
- print month_diff
- print day_diff """
- # Function to calculate total number of days. Will take all return values from date_difference function
- def total_day_count(years, days, year1, year2, day1, day2, month1, month2, months):
- year_days = years * 365 # calculates number of days contained in full years
- # finds number of days in month prior to end month, adds remaining number of days from start date to total days
- if month2 in (0,1,3,5,7,8,10):
- month_end_len = 31
- elif month2 in (4,6,9,11):
- month_end_len = 30
- else: # checks if leap year for February days
- if (year2 % 4 == 0 and year2 % 100 != 0) or year2 % 400 == 0:
- month_end_len = 29
- else:
- month_end_len = 28
- # print month2 # check variable
- # print month_end_len # check variable
- total_days = year_days + (month_end_len - day1) + day2 # starts day total with complete year days plus days in start and end months
- # print total_days # check calculation
- # the next two loops calculate the number of days in all the months between start and end month. adds days to total at every loop iteration
- # first case: when the end month precedes start month in end year e.g. june 2014 to april 2015
- if month2 < month1:
- for month in range(month1+1, 12): # loops over all months between start month and end of year, excluding start month
- if month in (0,2,4,6,7,9,11):
- month_len = 31
- total_days += month_len
- elif month in (3,5,8,10):
- month_len = 30
- total_days += month_len
- else: # if the month is February, it checks for a leap year then assigns the month length
- if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:
- month_len = 29
- total_days += month_len
- else:
- month_len = 28
- total_days += month_len
- for month in range(0, month2): # loops over all month between beginning of year and end month, excluding end month
- if month in (0,2,4,6,7,9,11):
- month_len = 31
- total_days += month_len
- elif month in (3,5,8,10):
- month_len = 30
- total_days += month_len
- else: # same February calculation as above
- if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:
- month_len = 29
- total_days += month_len
- else:
- month_len = 28
- total_days += month_len
- # second case: when end month is after start month in later year e.g. june 2014 to august 2015
- if month2 > month1: # loops over all months between start month and end month. excluding start and end months
- for month in range(month1+1, month2):
- if month in (0,2,4,6,7,9,11):
- month_len = 31
- total_days += month_len
- elif month in (3,5,8,10):
- month_len = 30
- total_days += month_len
- else:
- if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:
- month_len = 29
- total_days += month_len
- else:
- month_len = 28
- total_days += month_len
- # third case: when end month and start month are the same.
- if month2 == month1 and years == 0:
- total_days = 365 + (day2 - day1)
- if month2 == month1 and years != 0:
- total_days = (years * 365) + (365 + (day2 - day1))
- # adds days for leap years that are present in the year span.
- for year in range(year1, year2): # the end year isn't included because February would already be accounted for in the month days calculation above
- if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
- total_days += 1
- # print year # check loop
- print "Damn son. That's %d days!\n" % total_days # prints total days
- return (total_days) # returns total days for conversion
- # Function to convert total days to other units of time and print them
- def convert(days):
- hours = days * 24
- minutes = hours * 60
- seconds = minutes * 60
- weeks = days / 7
- extra_days = days % 7
- hexsecs = float(seconds * 1.32)
- print "If you wanna get wild, that's equal to:\n"
- print "%d hours" % hours
- print "%d minutes" % minutes
- print "%d seconds" % seconds
- print "%d weeks and %d days" % (weeks, extra_days)
- print "And %d hexadecimal seconds! (wtf is that anyway?)\n" % hexsecs
- # end function definitions
- # start script
- print "\nOh shit. You have to go to China on a secret mission?! How long are you gonna be there?\n"
- # calls functions and assigns variables to return values
- (start, end) = input_dates()
- (years, days, year1, year2, day1, day2, month1, month2, months) = date_difference(start, end)
- total_days = total_day_count(years, days, year1, year2, day1, day2, month1, month2, months)
- convert(total_days)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement