mirror of
https://github.com/notwa/rc
synced 2024-11-09 18:39:03 -08:00
52 lines
1.2 KiB
Bash
Executable file
52 lines
1.2 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
# compat: +ash +bash +dash +hush -ksh +mksh +oksh +osh +posh +yash +zsh
|
|
|
|
now() { ### @-
|
|
### print a date-time (UTC) in a sortable format.
|
|
### this takes a date or file as an argument,
|
|
### else it defaults to the current time.
|
|
### ```
|
|
### $ now
|
|
### 2019-05-27_35083906
|
|
### $ now ~/sh/monitor
|
|
### 2017-03-14_82387259
|
|
### $ now '@1234567890'
|
|
### 2009-02-13_84690000
|
|
### ```
|
|
[ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
|
|
|
|
local dt= fmt='+%F_%T_%N'
|
|
if [ -z "$1" ]; then
|
|
dt="$(exec /usr/bin/env date -u "$fmt")"
|
|
elif [ -e "$1" ]; then
|
|
dt="$(exec /usr/bin/env date -u -r "$1" "$fmt")"
|
|
else
|
|
dt="$(exec /usr/bin/env date -u -d "$1" "$fmt")"
|
|
fi
|
|
|
|
local F= T= N=
|
|
F=${dt%%_*}
|
|
T=${dt#*_}
|
|
T=${T%_*}
|
|
N=${dt##*_}
|
|
local H= M= S=
|
|
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=
|
|
ms=$(( (H*60*60+M*60+S)*1000+N/1000000 ))
|
|
ms=$(printf '%08i' $ms)
|
|
echo "${F}_${ms}"
|
|
}
|
|
|
|
[ -n "${preload+-}" ] || now "$@"
|