I have a list of sub-times and a total time. I'm trying to iteratively subtract the sub-times from the total time until I have the remaining time difference after subtracting all the sub-times from the total
Issue is that I'm working with the Python datetime module which seems to be very finicky about formatting. The total time is initially in minutes, seconds, and milliseconds, but after subtracting a lot of the sub-times, it'll change. The sub-time may also be minutes, seconds, milliseconds or simply seconds and milliseconds depending on user input.
I've also been having issues converting the times to datetime objects since they are originally strings and don't adhere fully to datetime formatting.
How would I go about solving this problem?
def convert_to_timedelta(time_string):
try:
return datetime.strptime(time_string, '%H:%M:%S.%f') - datetime(1900, 1, 1)
except ValueError:
return datetime.strptime(time_string, '%M:%S.%f') - datetime(1900, 1, 1)
except:
return timedelta(seconds=float(time_string))
def calculate_remaining_time(total_time, sub_times):
total_time = convert_to_timedelta(total_time)
for sub_time in sub_times:
total_time -= convert_to_timedelta(sub_time)
return total_time