Skip to content

Instantly share code, notes, and snippets.

@hfs
Created July 11, 2012 21:31
Show Gist options
  • Save hfs/3093718 to your computer and use it in GitHub Desktop.
Save hfs/3093718 to your computer and use it in GitHub Desktop.
Print a time interval given in seconds in human readable form: 2d 13h 7m 0s
human_time() {
seconds=$1
d=$(( $seconds / 86400 ))
h=$(( $seconds / 3600 % 24 ))
m=$(( $seconds / 60 % 60 ))
seconds=$(( $seconds % 60 ))
for unit in d h m; do
eval value=\$$unit
if [ $value -gt 0 ]; then
printing=true
fi
if [ "$printing" ]; then
echo -n "$value$unit "
fi
done
echo "${seconds}s"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment