Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get the time in milliseconds in FreeBSD?

Tags:

shell

freebsd

In Linux I can find the current time in milliseconds using the command:

date +%s%N

but on FreeBSD I get only

[13:38 ]#date +%s%N
1299148740N

How can I get the time in milliseconds (or nanoseconds) in FreeBSD?

like image 988
ilalex Avatar asked Mar 03 '11 10:03

ilalex


2 Answers

The BSD date command doesn't support milliseconds. If you want a date with millisecond support, install the GNU coreutils package.

I encountered this on OS X, whose date comes from BSD. The solution was to brew install coreutils and ln -sf /usr/local/bin/gdate $HOME/bin, and making sure that $HOME/bin comes first in PATH.

like image 39
Antti Kissaniemi Avatar answered Sep 28 '22 02:09

Antti Kissaniemi


Use gettimeofday(), for example:

#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;
}
like image 112
Eelvex Avatar answered Sep 28 '22 02:09

Eelvex