Last active
July 7, 2016 22:25
-
-
Save millzpaugh/115ad339bb79fdc1e82490428316bdee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
days_each_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} | |
class Datetime(object): | |
def __init__(self, year, month, day): | |
self.day = day | |
self.month = month | |
self.year = year | |
class Package(object): | |
def __init__(self,date_shipped, date_delivered, include_startend_dates): | |
self.date_shipped = date_shipped | |
self.date_delivered = date_delivered | |
self.include_startend_dates = include_startend_dates | |
@property | |
# adding 2 days includes the ship date and delivery date | |
def origin_dates(self): | |
if self.include_startend_dates: | |
return 2 | |
else: | |
return 0 | |
@property | |
def number_of_years(self): | |
return self.date_delivered.year - self.date_shipped.year | |
def calculate_years(self): | |
if self.number_of_years == 1 and self.date_shipped.month > self.date_delivered.month: | |
return 0 | |
else: | |
return self.number_of_years * 365 | |
def calculate_months(self): | |
months = range(self.date_shipped.month, self.date_delivered.month +1) | |
if self.date_shipped.month > self.date_delivered.month: | |
months = [k for k in days_each_month.keys() if k >= self.date_shipped.month or k <= self.date_delivered.month] | |
days = 0 | |
for month in months: | |
days_in_month = days_each_month[month] | |
days += days_in_month | |
return days | |
def number_of_days_to_deliver(self): | |
days_in_years = self.calculate_years() | |
origin_dates = self.origin_dates | |
if self.number_of_years == 0 and self.date_shipped.month > self.date_delivered.month: | |
return 'Your end date cannot occur before your start date.' | |
else: | |
total_days_in_months_shipped = self.calculate_months() | |
days_in_end_month = days_each_month[self.date_shipped.month] - self.date_delivered.day | |
days_in_less_than_calendar_year = total_days_in_months_shipped - (self.date_shipped.day + days_in_end_month) | |
return days_in_years + days_in_less_than_calendar_year + origin_dates | |
############################################################## | |
# TESTS | |
############################################################## | |
date_shipped_one = Datetime(year=2013, month=5, day=10) | |
date_delivered_one = Datetime(year=2014, month=5, day=10) | |
date_shipped_two = Datetime(year=2013, month=5, day=10) | |
date_delivered_two = Datetime(year=2016, month=5, day=10) | |
date_shipped_three = Datetime(year=2013, month=12, day=10) | |
date_delivered_three = Datetime(year=2014, month=5, day=10) | |
p_one = Package(date_shipped=date_shipped_one, date_delivered=date_delivered_one, include_startend_dates=True) | |
one_year = p_one.number_of_days_to_deliver() | |
p_two = Package(date_shipped=date_shipped_two, date_delivered=date_delivered_two, include_startend_dates=True) | |
multiple_years = p_two.number_of_days_to_deliver() | |
p_three = Package(date_shipped=date_shipped_three, date_delivered=date_delivered_three, include_startend_dates=True) | |
off_calendar_half_year = p_three.number_of_days_to_deliver() | |
assert(one_year == 367) | |
assert(multiple_years == 1097) | |
assert(off_calendar_half_year == 153) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment