Issue
How can I sum a time in HH:MM:SS with another one time in second in shell or python? For example, suppose that the time is 07:12:54 and I need to add 2000.234 seconds, how can I calculate the output time in HH:MM:SS after the sum? And how will be if I wanna decrease the seconds of the HH:MM:SS time?
thanks,
Solution
The easiest way should be using datetime with timedelta:
import datetime
time = datetime.datetime.strptime("07:12:54", "%H:%M:%S") # suppose that the time is 07:12:54
print(time.strftime ("%H:%M:%S"))
delta = datetime.timedelta(seconds=2000.234) # I need to add 2000.234 seconds
print(delta)
new_time = time + delta # calculate the output time in HH:MM:SS
print(new_time.strftime ("%H:%M:%S"))
new_time2 = time - delta # decrease the seconds of the HH:MM:SS time
print(new_time2.strftime ("%H:%M:%S"))
resulting in the output:
07:12:54
0:33:20.234000
07:46:14
06:39:33
Answered By - Frank J. Answer Checked By - Pedro (WPSolving Volunteer)