From ColdSphinX, 2 Years ago, written in Bash.
Embed
  1. #/bin/bash
  2.  
  3. [[ -n $1 ]] && goal="$(date +$1)" || goal="tomorrow 00:00"
  4. LANG=C date -d "$goal" >/dev/null || exit 1
  5.  
  6. function countdown_negative() {
  7.   echo "$(basename $0): invalid countdown '${remainingsec}'" >&2
  8.   exit 2
  9. }
  10.  
  11. remainingsec=$(($(date -d "$goal" +%s) - $(date +%s)))
  12. [ $remainingsec -lt 0 ] && countdown_negative
  13.  
  14. H=$(($remainingsec / 3600))
  15. remH=$(($H * 3600))
  16. [ $H -lt 10 ] && H="0$H"
  17.  
  18. m=$(($(($remainingsec - $remH)) / 60))
  19. remm=$(($m * 60))
  20. [ $m -lt 10 ] && m="0$m"
  21.  
  22. s=$(($remainingsec - $remH - $remm))
  23. [ $s -lt 10 ] && s="0$s"
  24.  
  25. echo "$H:$m:$s"
  26.