If you need to use bash, you need to convert the timestamp to an integer value:
$ time="12:34:56.789" $ IFS=":." read -rhms ms <<<"$time" $ echo $h $m $s $ms 12 34 56 789 $ milliseconds=$(( (h*3600 + m*60 + s)*1000 + ms )) $ echo $milliseconds 45296789
You can then subtract to get a few milliseconds representing diff.
Convert to seconds with some arithmetic and string:
$ seconds="$((milliseconds / 1000)).$((milliseconds % 1000))" $ echo $seconds 45296.789
To access the correct gniourf_gniourf point (in an awkward way):
$ time="12:34:56.7" $ IFS=":." read -rhms ms <<<"$time" $ ms="${ms}000"; ms=${ms:0:3} $ echo $h $m $s $ms 12 34 56 700
Also, strings with invalid octal digits such as "08" and "09" will cause errors in bash arithmetic, so explicitly declare them to be base 10
milliseconds=$(( (10#$h*3600 + 10#$m*60 + 10#$s)*1000 + 10#$ms ))
glenn jackman
source share