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?
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
.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With