Skip to content

Instantly share code, notes, and snippets.

@psycho23
Last active July 23, 2017 03:46
Show Gist options
  • Save psycho23/6e5c59e110675895a9ca0e9535928e03 to your computer and use it in GitHub Desktop.
Save psycho23/6e5c59e110675895a9ca0e9535928e03 to your computer and use it in GitHub Desktop.
timediff.sh possible solutions
PROBLEM by AW
I need someone to create a command that does this for me `timediff 13:54:05
14:06:42 #outputs: 00:12:37` If you put that command on github I 94% promise to
star the repository or gist it is in for at least 3 days :0)
SOLUTION1 (untested) by PH
timediff () { date +%H:%M:%S -d "1970-01-01 + $(( $(date +%s -d "1970-01-01 $timeb") - $(date +%s -d "1970-01-01 $timea") )) seconds" ; }
SOLUTION2VERSION1 by KU
date +%T -d @$(( $( date +%s -d "$1" ) - $( date +%s -d "$2" )))
SOLUTION2VERSION2 by KU
TZ=Z date +%T -d @$(( $( date +%s -d "$1" ) - $( date +%s -d "$2" )))
SOLUTION2VERSION3 by KU (11 minutes, 21 seconds later)
t2s() { local IFS=:; local -a a=($1) ; a=("${a[@]//#/10#}") ; echo $(( a[0]*3600 +a[1]*60 + a[2] )) ; } ; TZ=Z printf '%(%T)T\n' $(( $( t2s $2 ) - $( t2s $1 ) ))
SOLUTION3VERSION1 by AW
echo -e "$(date +%T -d '13:54:05')\n$(date +%T -d '14:06:42')" | awk -F: 'BEGIN{OFS=":"} (NR == 1){first_one=$1; first_two=$2; first_three=$3;} (NR == 2){printf "%02d:%02d:%02d\n", $1-first_one, $2-first_two, $3-first_three; }' | perl -ne 'chomp;@all=split /:/;if($all[1] =~ /^-/){$all[0]-=1;$all[1]+=60;}CORE::say join(":", @all);'
SOLUTION3VERSION2 by AW
echo -e "$(date +%T -d '14:17:44')\n$(date +%T -d '14:29:05')" | awk -F: 'BEGIN{OFS=":"} (NR == 1){first_one=$1; first_two=$2; first_three=$3;} (NR == 2){printf "%02d:%02d:%02d\n", $1-first_one, $2-first_two, $3-first_three; }' | perl -ne 'chomp;@all=split /:/;if($all[1] =~ /^-/){$all[0]-=1;$all[1]+=60;}if($all[2] =~ /^-/){$all[1]-=1;$all[2]+=60;}CORE::say join(":", @all);'
Analysis:
SOLUTION3 is not a function, yet. It would take 2 minutes to turn it into one.
SOLUTION2 personal questions:
a=("${a[@]//#/10#}")
1) Why is this happening in the array? Why is there a 10?
This will take 12 minutes to figure out.
SOLUTION1 is apparently untested because the author stated "something like..."
before the solution code. Therefore it would take 10 minutes to dismantle to
test each individual part + put it together and run it.
CONCLUSION:
How can there be a conclusion without a hypothesis?
Plus I didn't even care about this problem! I'm trolling lol.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment