2015-12-12 06:39:45 -08:00
|
|
|
#!/usr/bin/env bash
|
2021-07-29 00:37:35 -07:00
|
|
|
# YES_ZSH
|
2015-12-12 06:39:45 -08:00
|
|
|
|
|
|
|
now() {
|
2021-07-29 00:37:35 -07:00
|
|
|
[ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
|
|
|
|
local dt
|
2015-12-12 06:39:45 -08:00
|
|
|
local fmt='+%F_%T_%N'
|
|
|
|
if [ -z "$1" ]; then
|
2019-05-27 02:46:16 -07:00
|
|
|
dt="$(date -u "$fmt")"
|
2015-12-12 06:39:45 -08:00
|
|
|
elif [ -e "$1" ]; then
|
2019-05-27 02:46:16 -07:00
|
|
|
dt="$(date -u -r "$1" "$fmt")"
|
2015-12-12 06:39:45 -08:00
|
|
|
else
|
2019-05-27 02:46:16 -07:00
|
|
|
dt="$(date -u -d "$1" "$fmt")"
|
2015-12-12 06:39:45 -08:00
|
|
|
fi
|
|
|
|
|
2021-07-29 00:37:35 -07:00
|
|
|
local F T N
|
2015-12-12 06:39:45 -08:00
|
|
|
F=${dt%%_*}
|
|
|
|
T=${dt#*_}
|
|
|
|
T=${T%_*}
|
|
|
|
N=${dt##*_}
|
2021-07-29 00:37:35 -07:00
|
|
|
local H M S
|
2015-12-12 06:39:45 -08:00
|
|
|
H=${T%%:*}
|
|
|
|
M=${T#*:}
|
|
|
|
M=${M%:*}
|
|
|
|
S=${T##*:}
|
|
|
|
|
|
|
|
# don't interpret numbers as octal
|
|
|
|
F=${F#0*}
|
|
|
|
T=${T#0*}
|
|
|
|
N=${N#0*}
|
|
|
|
H=${H#0*}
|
|
|
|
M=${M#0*}
|
|
|
|
S=${S#0*}
|
|
|
|
|
|
|
|
local ms=
|
|
|
|
let 'ms=(H*60*60+M*60+S)*1000+N/1000000'
|
|
|
|
ms=$(printf '%08i' $ms)
|
|
|
|
echo "${F}_${ms}"
|
|
|
|
}
|
|
|
|
|
2021-07-29 00:37:35 -07:00
|
|
|
[ "${SOURCING:-0}" -gt 0 ] || now "$@"
|