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.
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:
to_seconds() {
local epoch=$(date --utc -d @0 +%F)
date --utc -d "$epoch $1" +%s.%09N
}
Running it with the example from the question:
$ to_seconds 00:20:40.29
1240.290000000
Note that --utc
, @
, %s
and %N
are all GNU extensions not necessarily supported by other implementations.