Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Last active December 22, 2021 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thcipriani/1a0fb65a3ed8c7add571fc368873f1af to your computer and use it in GitHub Desktop.
Save thcipriani/1a0fb65a3ed8c7add571fc368873f1af to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
output() {
printf "%s %s\n" "$@"
}
STARTDATE="$1"
ENDDATE="$2"
STARTSECONDS=$(date +%s -d"${STARTDATE}")
ENDSECONDS=$(date +%s -d"${ENDDATE}")
DIFFSECONDS=$(( ENDSECONDS - STARTSECONDS ))
UNIT="years"
UNITPRINT="year(s)"
if (( DIFFSECONDS < 60 )); then
output "$DIFFSECONDS" "second(s)"
exit 0
elif (( DIFFSECONDS < 3600 )); then
UNIT="minutes"
UNITPRINT="minute(s)"
elif (( DIFFSECONDS < 86400 )); then
UNIT="hours"
UNITPRINT="hour(s)"
elif (( DIFFSECONDS < 604800 )); then
UNIT="days"
UNITPRINT="day(s)"
elif (( DIFFSECONDS < 31536000 )); then
UNIT="weeks"
UNITPRINT="week(s)"
fi
DIFFTIME=$(units "${DIFFSECONDS}seconds" "${UNIT}" | awk '/*/ {print $2}')
output "$DIFFTIME" "${UNITPRINT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment