helm/bin/since
Henrik Lissner 20b8c2c1ad
Use zsh for bin/since
Why depend on bc for this, when we know zsh will always be available on
my nix systems.
2020-05-12 17:45:58 -04:00

39 lines
1 KiB
Bash
Executable file

#!/usr/bin/env zsh
set -e
(( $# > 0 )) || exit
# 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
# TODO: A help fallback
# TODO: Options parsing?
# If given a file, give time since last modification
(( $# == 1 )) && [[ -f $1 ]] && arg=reference || arg=date
if [[ $1 == -- ]]; then
t="$2"
else
now=$(date +%s)
then=$(date --$arg="$*" +%s)
t=$(( now - then ))
fi
# Bash doesn't support floating point arithmetic like zsh does, so use bc to do
# our arithmetic.
zmodload zsh/mathfunc
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 ))
(( weeks > 0 )) && echo -n "${weeks}w "
(( days > 0 )) && echo -n "${days}d "
(( hours > 0 )) && echo -n "${hours}h "
(( minutes > 0 )) && echo -n "${minutes}m "
echo -n "${seconds}s"