Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 23176  / 2 Years ago, tue, june 7, 2022, 2:54:31

As far as I know, bash variables do not have a certain type.



My problem is, I need to convert a time stamp (always in format: hh:mm:ss) to a single integer representing seconds.



So I cut the hh, mm and ss parts into separate strings and use expr to calculate the seconds integer like:



TIME=expr $HH * 3600 + $MM * 60 + $SS


But when e.g. $HH=00 then expr won't work. Is it possible to convert strings like 00 or 01 to integers?


More From » bash

 Answers
1

Try awk. For example:



echo "00:20:40" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'


So, if you have:



time="00:20:40"


then:



seconds=$(echo $time | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')


and echo $seconds will print 1240.


[#27391] Wednesday, June 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mance

Total Points: 198
Total Questions: 105
Total Answers: 128

Location: South Georgia
Member since Mon, Aug 16, 2021
3 Years ago
;