Skip to content

Instantly share code, notes, and snippets.

@Rudde
Created May 10, 2016 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rudde/4b99049a176de35452a8ee61440aa78e to your computer and use it in GitHub Desktop.
Save Rudde/4b99049a176de35452a8ee61440aa78e to your computer and use it in GitHub Desktop.
Takes int or float seconds as input will output in format hh:mm:ss.ms where ms is rounded to 3 decimals
#!/bin/bash
function padnum {
NUM=$1
if [ $(echo "$NUM < 10" | bc) -ne 0 ]
then
echo 0$NUM
else
echo $NUM
fi
}
function sec2ts {
SEC=$1
HR=$(echo - | awk "{print $SEC / 3600}")
HR=${HR%.*}
SEC=$(echo - | awk "{print $SEC - ($HR * 3600)}")
MIN=$(echo - | awk "{print $SEC / 60}")
MIN=${MIN%.*}
SEC=$(echo - | awk "{print $SEC - ($MIN * 60)}")
SEC=$(printf "%0.3f" $SEC)
echo $(padnum $HR):$(padnum $MIN):$(padnum $SEC)
}
sec2ts $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment