Tell us what’s happening:
Can’t understand why in some test the result exced the 12 hour and in other cases the result restart the 12 hour to 00:00. For example, for an initial time of 11:49PM and an additional time of 00:05 the expected result is 12:04AM (instead of 00:04AM). In other test the initial time is 8:16PM with an additional time of 266:02 (19 days and 10:02) and in this case the expected result is 6:18AM instead of 18:18AM. Can someone help me?
Your code so far
DAYS_ON_WEEK = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
def add_time(start, duration, day = ''):
# Get the time parameters
time_splitted = start.split(' ')
ampm = time_splitted[1]
start_time = time_splitted[0].split(':')
start_hour = int(start_time[0])
start_minutes = int(start_time[1])
# Duration parameters
duration_splitted = duration.split(':')
duration_hours = int(duration_splitted[0])
duration_minutes = int(duration_splitted[1])
hours_left = 0
days = 0
if duration_hours >= 24:
days = duration_hours // 24
hours_left = duration_hours % 24
else:
hours_left = duration_hours
final_hour = start_hour + hours_left
final_minutes = start_minutes + duration_minutes
if final_minutes >= 60:
final_hour += 1
final_minutes -= 60
if final_hour >= 24:
days += 1
final_hour -= 24
ampm_changed = False
if final_hour >= 12:
if ampm == 'PM':
ampm_changed = True
ampm = 'AM'
else:
ampm = 'PM'
new_time = str(final_hour) + ":" + ('0' + str(final_minutes) if final_minutes < 10 else str(final_minutes)) + " " + ampm
final_day = 0
if day != '':
day_index = DAYS_ON_WEEK.index(day.lower())
final_day = (day_index + days) % 7
new_time += ', ' + DAYS_ON_WEEK[final_day][0].upper() + DAYS_ON_WEEK[final_day][1:]
if days == 0 and ampm_changed and ampm == 'AM':
new_time += ' (next day)'
elif days == 1:
new_time += ' (next day)'
elif days > 1:
new_time += ' (' + str(days) + ' days later)'
return new_time
print(add_time('11:59 PM', '24:05', 'Wednesday'))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project