0

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,

gwsmelo
  • 13
  • 4
  • For Python see https://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python – slothrop Aug 11 '23 at 20:18
  • What have you tried? Please show it and explain how its output differs from what you expected. – Michael Ruth Aug 11 '23 at 20:24
  • Can you clarify if you are doing this in [tag:python] or [tag:shell]? – SiKing Aug 11 '23 at 20:27
  • I am processing a set of signals with more than 100 inside a for loop on shell. I have two variables: one presenting travel time in seconds (t_wave_travel_time_sec=`echo "scale=2; $epic_dist / 1.5" | bc`) and another one with the origin time (origin_time=`awk ' NR==n {print $3}' n=$n $filename `) in hh:mm:ss. I need to calculate the "t_wave_arrival_time" that will be the origin time + t_wave_travel_time_sec. – gwsmelo Aug 11 '23 at 20:37
  • But I am stuck and do not know how to do because some functions like datetime on python look like to need the information separate hh mm ss, however, they are only separated by the ':' on my case (ex: 07:17:46, 07:26:35, 08:24:15....). – gwsmelo Aug 11 '23 at 20:37
  • I asked on python because if someone now a way on python I can create a code on python and include it inside of the shell, similar I have done to calculate distance: epic_dist=`python3 distance.py $slat $slon $epiclat $epiclon` – gwsmelo Aug 11 '23 at 20:39

1 Answers1

0

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
Frank J.
  • 16
  • 1
  • 2
  • Ok. I created the code and tried to execute code, including some time examples as input: python3.9 arrival_time.py 12:22:22 2304.34 – gwsmelo Aug 11 '23 at 20:52
  • import datetime import sys time = datetime.datetime.strptime("float(sys.argv[1])", "%H:%M:%S") # suppose that the time is 07:12:54 print(time.strftime ("%H:%M:%S")) delta = datetime.timedelta(seconds=float(sys.argv[2])) # 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")) – gwsmelo Aug 11 '23 at 20:52
  • However, it did not work. – gwsmelo Aug 11 '23 at 20:52
  • time = datetime.datetime.strptime("float(sys.argv[1])", "%H:%M:%S") # suppose that the time is 07:12:54 File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 349, in _strptime raise ValueError("time data %r does not match format %r" % ValueError: time data 'float(sys.argv[1])' does not match format '%H:%M:%S' – gwsmelo Aug 11 '23 at 20:53
  • `"float(sys.argv[1])"` is a literal string. You likely want `sys.argv[1]`: no double quotes, and no conversion to float, since it's a string that you're going to parse. – slothrop Aug 11 '23 at 21:26
  • I got it. Thanks! – gwsmelo Aug 11 '23 at 21:52