Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Usage: result=$( seconds_to_human_time $SECONDS )
seconds_to_human_time () {
local result= sign= suffix= seconds=
# Precisely one argument.
if [[ ${#@} != 1 ]] || [[ -z $1 ]]; then
echo 'Usage: result=$( seconds_to_human_time $SECONDS )' 1>&2
exit 1
fi
# Record whether it's positive or negative, choosing the right suffix.
if [[ $1 -lt 0 ]]; then
sign=-1
suffix='from now'
else
sign=1
suffix='ago'
fi
# Absolute seconds, discarding the sign (positive/negative).
seconds=$(( $sign * $1 ))
# Once a $result is set, all following clauses will be skipped.
# They're ordered largest to smallest (days, hours, minutes, seconds).
# If nothing else matches it'll default to N seconds.
# Days.
[[ -z $result ]] && [[ $(( $seconds / 86400 )) == 1 ]] && result="1 day"
[[ -z $result ]] && [[ $seconds -gt 86400 ]] && result="$(( $seconds / 86400 )) days"
# Hours.
[[ -z $result ]] && [[ $(( $seconds / 3600 )) == 1 ]] && result="1 hour"
[[ -z $result ]] && [[ $seconds -gt 3600 ]] && result="$(( $seconds / 3600 )) hours"
# Minutes.
[[ -z $result ]] && [[ $(( $seconds / 60 )) == 1 ]] && result="1 minute"
[[ -z $result ]] && [[ $seconds -gt 60 ]] && result="$(( $seconds / 60 )) minutes"
# Seconds.
[[ -z $result ]] && [[ $seconds == 1 ]] && result="1 second"
[[ -z $result ]] && [[ $seconds == 0 ]] && result="right now" suffix=""
[[ -z $result ]] && result="${seconds} seconds"
# Print the result.
echo $result $suffix
}
echo : Seconds
for i in 0 1 2 29 30 31; do
echo $i seconds == $( seconds_to_human_time $i )
done
echo : Minutes
for i in 59 60 61 89 90 91 119 120 121; do
echo $i seconds == $( seconds_to_human_time $i )
done
echo : Hours
for i in 3599 3600 3601 5399 5400 5401 7199 7200 7201 8999 9000 9001; do
echo $i seconds == $( seconds_to_human_time $i )
done
echo : Days
for i in 86399 86400 86401 129599 129600 129601 172799 172800 172801 215999 216000 216001; do
echo $i seconds == $( seconds_to_human_time $i )
done
echo : Negatives
for i in -2 -1 0 -61 -60 -59 -3601 -3600 -3599 -86401 -86400 -86399; do
echo $i seconds == $( seconds_to_human_time $i )
done
RESULT=<<EOF
: Seconds
0 seconds == right now
1 seconds == 1 second ago
2 seconds == 2 seconds ago
29 seconds == 29 seconds ago
30 seconds == 30 seconds ago
31 seconds == 31 seconds ago
: Minutes
59 seconds == 59 seconds ago
60 seconds == 1 minute ago
61 seconds == 1 minute ago
89 seconds == 1 minute ago
90 seconds == 1 minute ago
91 seconds == 1 minute ago
119 seconds == 1 minute ago
120 seconds == 2 minutes ago
121 seconds == 2 minutes ago
: Hours
3599 seconds == 59 minutes ago
3600 seconds == 1 hour ago
3601 seconds == 1 hour ago
5399 seconds == 1 hour ago
5400 seconds == 1 hour ago
5401 seconds == 1 hour ago
7199 seconds == 1 hour ago
7200 seconds == 2 hours ago
7201 seconds == 2 hours ago
8999 seconds == 2 hours ago
9000 seconds == 2 hours ago
9001 seconds == 2 hours ago
: Days
86399 seconds == 23 hours ago
86400 seconds == 1 day ago
86401 seconds == 1 day ago
129599 seconds == 1 day ago
129600 seconds == 1 day ago
129601 seconds == 1 day ago
172799 seconds == 1 day ago
172800 seconds == 2 days ago
172801 seconds == 2 days ago
215999 seconds == 2 days ago
216000 seconds == 2 days ago
216001 seconds == 2 days ago
: Negatives
-2 seconds == 2 seconds from now
-1 seconds == 1 second from now
0 seconds == right now
-61 seconds == 1 minute from now
-60 seconds == 1 minute from now
-59 seconds == 59 seconds from now
-3601 seconds == 1 hour from now
-3600 seconds == 1 hour from now
-3599 seconds == 59 minutes from now
-86401 seconds == 1 day from now
-86400 seconds == 1 day from now
-86399 seconds == 23 hours from now
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment