Created
August 8, 2012 11:48
-
-
Save dlsym/3294474 to your computer and use it in GitHub Desktop.
Add time for bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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