Skip to content

Instantly share code, notes, and snippets.

@ze-
Created September 18, 2014 00:28
Show Gist options
  • Save ze-/0ad16f27c769da01fec7 to your computer and use it in GitHub Desktop.
Save ze-/0ad16f27c769da01fec7 to your computer and use it in GitHub Desktop.
Shows the duration between 2 dates/times.
#!/usr/bin/env bash
[[ $# -lt 2 ]] && printf "$(basename $0) <startdate> <enddate>\n" >/dev/stderr && exit 1
shopt -s extglob
gfs=$(($(date -d "$2" +%s)-$(date -d "$1" +%s)))
duration=" $(($gfs/3600/24/365))y $(printf "%3id %2ih %2im %2is" $(($gfs/3600/24%365)) $(($gfs/3600%24)) $(($gfs/60%60)) $(($gfs%60)))"
printf "${duration/+(+( )0[ydhm])}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment