Whats an easy way to convert 00:20:40.28 to seconds with a bash script? (Split seconds can be cut out, it’s not essential.)
views:
140answers:
4
+1
A:
I haven't tested this but, I think this is how you'd split the string. Followed by multiplying by the appropriate amounts for hours and minutes.
mytime=’00:20:40.28′
part1=${mytime%%:*}; rest=${mytime#*:}
part2=${rest%%:*}; rest=${rest#*:}
part3=${rest%%:*};
netricate
2010-02-02 03:37:56
+1
A:
with the shell,
#!/bin/bash
d="00:20:40.28"
IFS=":"
set -- $d
hr=$(($1*3600))
min=$(($2*60))
sec=${3%.*}
echo "total secs: $((hr+min+sec))"
ghostdog74
2010-02-02 03:41:21
+2
A:
Try this:
T='00:20:40.28'
SavedIFS="$IFS"
IFS=":."
Time=($T)
Seconds=$((${Time[0]}*3600 + ${Time[1]}*60 + ${Time[2]})).${Time[3]}
IFS="$SavedIFS"
echo $Seconds
($<string>) splits <string> based on the splitter (IFS
).
${<array>[<index>]} returns the element of the <array> at the <index>.
$((<arithmetic expression>)) performs the arithmetic expression.
Hope this helps.
NawaMan
2010-02-02 03:42:05
This fails when ${Time[2]} contains a decimal point.
dreamlax
2010-02-02 03:44:06
`IFS=":."` and `Seconds=$(( ... ))${Time[3]}`
Dennis Williamson
2010-02-02 04:15:56
Thanks both of you, I miss that.
NawaMan
2010-02-02 05:12:59