Re: Is there a POSIX compliant way of turning a "HH:MM:SS" formatted string to seconds? ...
OK, the Math is right, but the assumptions made by date aren't smart. I "overtested" your one liner with the kinds of input you would grab using jq from youtube .info.json files and to my amazement, when you only have two semicolon separated values, the bash date utility assumes the first chunk to be the hours and the second the minutes!:
_HHMMSS="19:09:00"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo "// __ \$_HHMMSS|$_HHMMSS|, seconds=$seconds"
_HHMMSS="19:08:00"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo "// __ \$_HHMMSS|$_HHMMSS|, seconds=$seconds"
_HHMMSS="00:19:09"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo "// __ \$_HHMMSS|$_HHMMSS|, seconds=$seconds"
_HHMMSS="00:19:08"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo "// __ \$_HHMMSS|$_HHMMSS|, seconds=$seconds"
_HHMMSS="19:09"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo "// __ \$_HHMMSS|$_HHMMSS|, seconds=$seconds"
_HHMMSS="19:08"
seconds=$( TZ=GMT date --date="1 january 1970 $_HHMMSS" "+%s" )
echo "// __ \$_HHMMSS|$_HHMMSS|, seconds=$seconds"
// __ $_HHMMSS|19:09:00|, seconds=68940
// __ $_HHMMSS|19:08:00|, seconds=68880
// __ $_HHMMSS|00:19:09|, seconds=1149
// __ $_HHMMSS|00:19:08|, seconds=1148
// __ $_HHMMSS|19:09|, seconds=68940
// __ $_HHMMSS|19:08|, seconds=68880
$
~
I will have to go over, learn from Greg's reply when I get the chance. I still think that there should be a way to make what I need straight forward using date's own formatting.
lbrtchx
Reply to: