helm/bin/til
Henrik Lissner c4be365696 Global bash/zsh aliases + new programs
+ add common.sh and common aliases
+ add bin/{since,til}
+ add bin/project-root
+ extract btl alias into bin/transmission-list
2015-12-21 01:30:42 -05:00

59 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -e
(( $# > 0 )) || exit 1
# 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
# `date` on OSX does not have the --date parameter
date=date
if [[ "$OSTYPE" == darwin* ]]; then
date=gdate
else
echo "Unsupported system"
exit 1
fi
# If given a file, give time since last modification
(( $# == 1 )) && [[ -f "$*" ]] && arg=reference || arg=date
args="$*"
if [[ "$1" == "--" ]]; then
t="$2"
else
now=$(${date} +%s)
then=$(${date} --$arg="$*" +%s)
# Show full target date
echo $(${date} --date="@$then")
t=$(( $now - $then ))
fi
# Bash doesn't support floating point arithmetic like zsh does, therefore:
bc <<EOF
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"
EOF