Question: I am writing a program that tracks running performance using Python. It calculates the runner's pace given the calculated elapsed_time and the distance. The issue I am facing is in the calculation of the difference between the runner's pace and their predicted pace. This difference can be positive or negative, as you can run faster than predicted or slower than predicted. However, in the example code (below) I get the result diff = 23:59:54. The result I want is diff = -00:00:06. Any ideas on what to do? I had a vague idea that datetime.timedelta might help. But as far as I can see there is no way to format a timedelta as a string...? import time def time_to_secs(t): hour, minute, seconds = t.split(':') adjustedtime = (int(hour)*3600) + (int(minute)*60) + int(seconds) return adjustedtime def time_to_string(t): ty_res = time.gmtime(t) result = time.strftime("%H:%M:%S",ty_res) return result predicted_pace = '00:04:10' distance = 10.0 elapsed_time = '00:42:42' pace = time_to_string((time_to_secs(elapsed_time)/distance)) diff = time_to_string(time_to_secs(predicted_pace) - time_to_secs(pace)) print(f'Your total time over {distance} km was {elapsed_time} with a pace of {pace} per km') print(f'Your predicted pace was {predicted_pace} per km') print(f'Difference between predicted and actual pace was {diff}') 

Answer: I think this arises from the way Python's time.strftime() function handles times. When you provide a negative number of seconds to time.strftime(), it doesn't format it as a negative time. Instead, it calculates a time that many seconds before the epoch, which is not what you want it seems. You need a custom solution to handle this. To calculate the difference between the predicted pace and the actual pace and handle negative values properly, you can modify the time_to_string function to account for negative timedelta values. Here is an example that might be helpful: import time def time_to_secs(t): hour, minute, seconds = t.split(':') adjustedtime = (int(hour) * 3600) + (int(minute) * 60) + int(seconds) return adjustedtime def time_to_string(t): negative = False if t < 0: negative = True t = -t # Make the time positive for calculation ty_res = time.gmtime(t) result = time.strftime("%H:%M:%S", ty_res) if negative: result = "-" + result return result predicted_pace = '00:04:10' distance = 10.0 elapsed_time = '00:42:42' pace = time_to_string((time_to_secs(elapsed_time) / distance)) diff = time_to_string(time_to_secs(predicted_pace) - time_to_secs(pace)) print(f'Your total time over {distance} km was {elapsed_time} with a pace of {pace} per km') print(f'Your predicted pace was {predicted_pace} per km') print(f'Difference between predicted and actual pace was {diff}')  This code does the following:  It first calculates the total seconds for both the predicted pace and the actual pace It then computes the difference in seconds. The code then determines if the difference is negative (meaning the runner was faster than predicted). If the time is negative, it then converts the absolute value of the delta into hours, minutes, and seconds. Finally, the code then formats the result as a string, adding a negative sign if necessary.  Hope this helps.