# https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator
############ time_calculator.py ###################
def add_time(start, duration, startday=None):
start = start.split(' ')
duration = duration.split(' ')
a = start[0].split(':')
b = duration[0].split(':')
l = int(a[1]) + int(b[1])
k = int(a[0]) + int(b[0]) + int((l // 60))
d = k // 24
e = k % 12
if e == 0:
e = 12
if startday != None:
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
index = days.index(startday)
c = list(days[index:] + days)[:7]
if d == 0 and k >= 12 and start[1] == 'PM':
return f'{e}:{l} AM {c[1]} (next day)'
if d == 0 and k >= 12 and start[1] == 'PM':
return f'{e}:{l} AM (next day)'
if l > 60 and a[0] == '11' and start[1] == 'PM':
start[1] = 'AM'
d = d + 1
elif l > 60 and a[0] == '11' and start[1] == 'AM':
start[1] = 'PM'
elif (k // 12) % 2 == 1 and start[1] == 'PM':
start[1] = 'AM'
d = d + 1
if l > 60:
l = l % 60
g = str(l)
if l < 10:
l = str('0'+ g)
if startday != None:
if d == 0:
return f'{e}:{l} {start[1]} {c[0]}'
elif d == 1:
return f'{e}:{l} {start[1]} {c[1]} (next day)'
elif d > 1:
return f'{e}:{l} {start[1]} {c[(d % 7)]} ({d} days later)'
else:
if d == 0:
return f'{e}:{l} {start[1]}'
elif d == 1:
return f'{e}:{l} {start[1]} (next day)'
elif d > 1:
return f'{e}:{l} {start[1]} ({d} days later)'
print(add_time("3:30 PM", "2:12", "Monday"))
# a start time in the 12-hour clock format (ending in AM or PM)
# a duration time that indicates the number of hours and minutes
# (optional) a starting day of the week, case insensitive
# If the result will be the next day, it should show (next day) after the time.
# If the result will be more than one day later, it should show (n days later) after the time, where "n" is the number of days later.
# If the function is given the optional starting day of the week parameter, then the output should display the day of the week of the result.
# The day of the week in the output should appear after the time and before the number of days later.
from time_calculator import add_time
from unittest import main
print(add_time("11:06 PM", "2:02"))
1:8 AM (next day)
add_time("3:00 PM", "3:10")
# Returns: 6:10 PM
add_time("11:30 AM", "2:32", "Monday")
# Returns: 2:02 PM, Monday
add_time("11:43 AM", "00:20")
# Returns: 12:03 PM
add_time("10:10 PM", "3:30")
# Returns: 1:40 AM (next day)
add_time("11:43 PM", "24:20", "Tuesday")
# Returns: 12:03 AM, Thursday (2 days later)
add_time("6:30 PM", "205:12")
# Returns: 7:42 AM (9 days later)
'7:42 AM (9 days later)'