What's an easy way to convert 00:20:40.28
(HH:MM:SS) to seconds with a Bash script?
Split seconds can be cut out, it’s not essential.
What's an easy way to convert 00:20:40.28
(HH:MM:SS) to seconds with a Bash script?
Split seconds can be cut out, it’s not essential.
If you are processing a time from
ps
, mind you that the format2-18:01
is also possible for 2 days, 19 hours, 1 minute. In that case you'll want to checkout: Parse ps' "etime" output and convert it into secondsThis would work even if you don't specify hours or minutes:
echo "04:20:40" | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc
with the shell,
If you don't know what exactly do you have - SS, MM:SS or HH:MM:SS, like after
youtube-dl --get-duration
, then awk magic could be useful:With GNU date, you can perform the conversion if the duration is less than 24 hours, by treating it as a time of day on the epoch:
Running it with the example from the question:
Note that
--utc
,@
,%s
and%N
are all GNU extensions not necessarily supported by other implementations.