# Time calculator. I need some help . i have finished all the conditions except test period change at 12

def add_time(start, duration, days=False ):
days_of_the_week = {‘monday’:0, ‘tuesday’:1, ‘wednesday’:2, ‘thursday’:3, ‘friday’:4, ‘saturday’:5, ‘sunday’:6}
days_array = [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]
print(’\n’)
print(‘new conversion’)
start_time_tuple = start.partition(’:’)
print (start_time_tuple)
start_time = start_time_tuple[2].partition(’ ‘)
print(start_time)
time_of_day = start_time[2]
print (time_of_day, type(time_of_day))
start_hours = int(start_time_tuple[0])
print (f’start_hours = {start_hours}’)
start_minutes = int(start_time[0])
print (f’start_minutes = {start_minutes}’)
am_or_pm = {“AM”:“PM”, “PM”:“AM”}

``````duration_time = duration.partition(':')
print (duration_time)
duration_hours = int(duration_time[0])
print (f'duration_hours = {duration_hours}')
duration_minutes = int(duration_time[2])
if duration_minutes == 00:
duration_minutes = 00
print(f'duration_minutes = {duration_minutes}')

day_amount = int(duration_hours/24)
print (f'day_amount = {day_amount}')
if time_of_day == "AM" and start_minutes == 11:
time_of_day = "PM"

end_minutes = start_minutes + duration_minutes
if end_minutes == 0 :
end_minutes = 0
if end_minutes >= 60:
end_minutes = end_minutes % 60
print(f'end_minutes = {end_minutes}')
start_hours = start_hours + 1
print(f'start_hours = {start_hours}')

else:
print(f'end_minutes = {end_minutes}')
#am_or_pm_turns = int((start_hours + duration_hours)/ 12)
if end_minutes < 9:
end_minutes = "0" + str(end_minutes)
print(f'end_minutes = {end_minutes}')
else:
print(f'end_minutes = {end_minutes}')

end_hours = start_hours + duration_hours
#print (f'end_hours = {end_hours}')
if end_hours > 12:
end_hours = end_hours % 12
print (f'end_hours = {end_hours}')
#if end_hours == 12:
#if time_of_day == "AM":
# time_of_day = "PM"
# elif time_of_day == "PM":
#  time_of_day = "AM"
if end_hours == 0:
end_hours = 12
#if time_of_day == "AM" and start_hours == 11 :
# time_of_day = "PM"
#elif end_hours < 9:
#end_hours = "0" + str(end_hours)
#elif end_hours < 9:
#end_hours = "0" + str(end_hours)
if time_of_day == "PM" and start_hours + (duration_hours % 12) >= 12:
day_amount = day_amount + 1
if  time_of_day == "PM":
time_of_day = "AM"
print(f'time_of_day = {time_of_day}')
elif time_of_day == "AM" and start_hours + (duration_hours % 12) > 12:
time_of_day = "PM"
if duration_hours == 24:
day_amount = day_amount + 1
elif time_of_day == "AM" and start_hours == 11:
time_of_day = "PM"
#if days is False:
#while end_hours == 12 and time_of_day == "AM":
#if time_of_day == "AM":
#  time_of_day = "PM"
# break
#elif days is True:
print('yes')
if end_hours == "12" and end_minutes == "05":
time_of_day == "PM"
#if time_of_day == "AM"  and start_time == 11:
# time_of_day == "PM"

print(time_of_day)
print (f'end_hours = {end_hours}')
print(f'end_minutes = {end_minutes}')
print(day_amount)

#time_of_day = am_or_pm[time_of_day] if am_or_pm_turns % 2 == 1 else time_of_day

return_time = str(end_hours) + ":" + str(end_minutes) + " " + time_of_day

print(days)
if days != 0:
days = days.lower()
index = int((days_of_the_week[days]) + day_amount) % 7
new_day = days_array[index]
return_time  += "," + " " + new_day
if day_amount == 1:
return_time += " " + "(next day)"
elif day_amount> 1:
return_time += " " + "(" + str(day_amount) + " " + "days later)"
print (return_time)
return return_time``````

i have tried to change the time once it reaches 12 but nothing seems to work.

I handled this by converting to 24 hour time for all calculations and converting it back to 12 hour time for the output.

Welcome to the forums @mugumya.

This one works…

This one doesn’t. Compare the final comparison operators.

In the vein of @fuzzyray’s response, you can make a lot of simplifications if you add the times and then work from large units to small units (weeks, then days, then hours, etc.).

Thanks so much. I hadnt thought about that.
It worked.

Thanks alot. I appreciate it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.