2020-04-12 13:09:31 -07:00
|
|
|
#!/usr/bin/env cached-nix-shell
|
|
|
|
#! nix-shell -i bash -p bc
|
2015-12-20 20:39:59 -08:00
|
|
|
set -e
|
2016-11-14 17:39:57 -08:00
|
|
|
(( $# > 0 )) || exit
|
2015-12-20 20:39:59 -08:00
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
# Displays the time since $1 in a human readable format (e.g. 4h 25m 55s). If
|
|
|
|
# given a file, the modified time is used. If $1 == "--", then this script acts
|
|
|
|
# as a converter, converting $2 into human a readable format, e.g. since -- 72
|
|
|
|
# #=> 1m 12s
|
2015-12-20 20:39:59 -08:00
|
|
|
|
|
|
|
# TODO: A help fallback
|
2017-12-05 22:39:10 -08:00
|
|
|
# TODO: Options parsing?
|
2015-12-20 20:39:59 -08:00
|
|
|
|
|
|
|
# If given a file, give time since last modification
|
2017-12-05 22:39:10 -08:00
|
|
|
(( $# == 1 )) && [[ -f $1 ]] && arg=reference || arg=date
|
2015-12-20 20:39:59 -08:00
|
|
|
|
2016-11-14 17:39:57 -08:00
|
|
|
if [[ $1 == -- ]]; then
|
2017-12-05 22:39:10 -08:00
|
|
|
t="$2"
|
2015-12-20 20:39:59 -08:00
|
|
|
else
|
2020-04-12 13:09:31 -07:00
|
|
|
now=$(date +%s)
|
|
|
|
then=$(date --$arg="$*" +%s)
|
2015-12-20 20:39:59 -08:00
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
t=$(( now - then ))
|
2015-12-20 20:39:59 -08:00
|
|
|
fi
|
|
|
|
|
2020-04-12 13:09:31 -07:00
|
|
|
# Bash doesn't support floating point arithmetic like zsh does, so use bc to do
|
|
|
|
# our arithmetic.
|
2015-12-20 20:39:59 -08:00
|
|
|
bc <<EOF
|
2017-12-05 22:39:10 -08:00
|
|
|
define abs(i) {
|
|
|
|
if (i < 0) return (-i)
|
|
|
|
return (i)
|
|
|
|
}
|
|
|
|
|
|
|
|
t = abs($t)
|
|
|
|
|
|
|
|
weeks = t / 60 / 60 / 24 / 7
|
|
|
|
days = t / 60 / 60 / 24 % 7
|
|
|
|
hours = t / 60 / 60 % 24
|
|
|
|
minutes = t / 60 % 60
|
|
|
|
seconds = t % 60
|
|
|
|
|
|
|
|
if (weeks > 0) print weeks, "w "
|
|
|
|
if (days > 0) print days, "d "
|
|
|
|
if (hours > 0) print hours, "h "
|
|
|
|
if (minutes > 0) print minutes, "m "
|
|
|
|
print seconds, "s"
|
2015-12-20 20:39:59 -08:00
|
|
|
EOF
|