Skip to content

Instantly share code, notes, and snippets.

@dlsym
Created August 8, 2012 11:48
Show Gist options
  • Save dlsym/3294474 to your computer and use it in GitHub Desktop.
Save dlsym/3294474 to your computer and use it in GitHub Desktop.
Add time for bash
#!/bin/bash
#
# Add two "H:M:S" times
# (c) 2012 @dlsym
#
#
# Time to seconds
function t2s {
div=3600; ts=0
for t in $(echo $1 | tr ":" "\n"); do
ts=$(($ts + $t*$div)); div=$(($div / 60))
done
echo $ts
}
# Seconds to time
function s2t {
H=$((($1/3600)%24)); M=$((($1/60)%60)); S=$(($1%60))
printf "%02d:%02d:%02d" $H $M $S
}
# Add time
function addt {
t1=$(t2s $1); t2=$(t2s $2);
s2t $(($t1+$t2))
}
addt "12:30" "01:00"; echo
addt "23:30" "01:10"; echo
addt "10:30" "11:32:10"; echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment