Skip to content

Instantly share code, notes, and snippets.

@sabrehagen
Last active November 14, 2019 02:15
Show Gist options
  • Save sabrehagen/e0af311b729a92fe6af2e8417abd25a6 to your computer and use it in GitHub Desktop.
Save sabrehagen/e0af311b729a92fe6af2e8417abd25a6 to your computer and use it in GitHub Desktop.
#!/bin/bash
minutes=60
hours=$(( 60 * minutes ))
days=$(( 24 * hours ))
diff=$(( ${2:-$(date +%s)} - $1 ))
if (( diff < 60 )); then
ago="${diff} seconds"
elif (( diff < 120 )); then
ago="1 minute"
elif (( diff < 1 * hours )); then
ago="$(( (diff + minutes / 2) / minutes )) minutes"
elif (( diff < 1 * hours + 30 * minutes )); then
ago="1 hour"
elif (( diff < 1 * days )); then
ago="$(( (diff + hours / 2) / hours )) hours"
elif (( diff < 2 * days)); then
ago="1 day"
else
ago="$(( (diff + days / 2) / days)) days"
fi
echo "$ago ago"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment