In case anyone is using other shells than bash
, ksh93
and zsh
have a floating point $SECONDS
variable if you do a typeset -F SECONDS
which can be handy to measure time with accuracy:
$ typeset -F SECONDS=0
$ do-something
something done
$ echo "$SECONDS seconds have elapsed"
18.3994340000 seconds have elapsed
Since version 4.3.13 (2011) zsh
has a $EPOCHREALTIME
special floating point variable in the zsh/datetime
module:
$ zmodload zsh/datetime
$ echo $EPOCHREALTIME
1364401642.2725396156
$ printf '%d\n' $((EPOCHREALTIME*1000))
1364401755993
Note that that's derived from the two integers (for seconds and nanoseconds) returned by clock_gettime()
. On most systems, that's more precision than a single C double
floating point number can hold, so you'll lose precision when you use it in arithmetic expressions (except for dates in the first few months of 1970).
$ t=$EPOCHREALTIME
$ echo $t $((t))
1568473231.6078064442 1568473231.6078064
To compute high precision time differences (though I doubt you'd need more than millisecond precision), you may want to use the $epochtime
special array instead (which contains the seconds and nanoseconds as two separate elements).
Since version 5.7 (2018) the strftime
shell builtin also supports a %N
nanosecond format à la GNU date
and a %.
to specify the precision, so the number of milliseconds since the epoch can also be retrieved with:
zmodload zsh/datetime
strftime %s%3. $epochtime
(or stored in a variable with -s var
)