I am currently typing in time to be converted into seconds as HH:MM:SS
with this:
$(date -d "1970-01-01 $element Z" %s)
I would like to be able to supply the time as HHMMSS without the :
colons. But have the same outcome. Converted into seconds.
Input:
010000 = 60*60*1 = 3600s
004032 = 60*40 32 = 2432s
CodePudding user response:
If you have $element
containing a six-digit string representing a time HHMMSS, you can convert it directly:
(( s = 10#${element:0:2}*3600 10#${element:2:2}*60 10#${element:4} ))
This uses parameter expansion to extract the substrings.
You need to prefix them with 10#
so bash doesn't treat them as octal if they begin with zero.
CodePudding user response:
Use parameter expansion to extract the substrings:
seconds=$(date -d "1970-01-01 ${element:0:2}:${element:2:2}:${element:4} Z" %s)
# 2 chars from 2 chars from rest of the
# position 0 position 2 string from
# position 4