On Linux, I can find the current time in milliseconds using the command:
date +%s%N
but on FreeBSD I only get
[13:38 ]#date +%s%N 1299148740N
How can I get time in milliseconds (or nanoseconds) in FreeBSD?
Use gettimeofday() , for example:
gettimeofday()
#include <stdio.h> #include <sys/time.h> int main(void) { struct timeval time_now; gettimeofday(&time_now,NULL); printf ("%ld secs, %ld usecs\n",time_now.tv_sec,time_now.tv_usec); return 0; }
The BSD date command does not support milliseconds. If you want to use date with millisecond support, install the GNU coreutils package.
date
coreutils
I came across this on OS X whose date comes from BSD. The solution was brew install coreutils and ln -sf /usr/local/bin/gdate $HOME/bin , and make sure $HOME/bin ln -sf /usr/local/bin/gdate $HOME/bin first in the PATH .
brew install coreutils
ln -sf /usr/local/bin/gdate $HOME/bin
$HOME/bin
PATH
Try using tai64n from daemontools
tai64n
daemontools
$ echo | tai64n | tai64nlocal 2011-03-03 09:45:37.833010500 $ ps | tai64n | tai64nlocal 2011-03-03 09:52:30.817146500 PID TTY TIME CMD 2011-03-03 09:52:30.817150500 7154 pts/1 00:00:07 bash 2011-03-03 09:52:30.817157500 20099 pts/1 00:00:00 ps 2011-03-03 09:52:30.817159500 20100 pts/1 00:00:00 tai64n 2011-03-03 09:52:30.817162500 20101 pts/1 00:00:00 tai64nlocal
Here is one liner for any recent Perl:
perl -MTime::HiRes=gettimeofday -MPOSIX=strftime -e '($s,$us) = gettimeofday(); printf "%d.%06d\n", $s, $us' 1487594425.611120
(modified with this answer to a similar question)
Another way to get milliseconds is to use Python:
time_ms=$( python3 -c'import time; print(time.time())' ) echo $time_ms
Outputs:
1570807434.2774572
Note that Python 2.x returns only hundredths of a second. May need a little refinement if your OS uses only Python 2.x to get more detail.