Last active
December 22, 2021 23:40
-
-
Save thcipriani/1a0fb65a3ed8c7add571fc368873f1af to your computer and use it in GitHub Desktop.
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
#!/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