2

How can I get (some kind of a extremely precise) time stamp?

My application needs to get and store (accurate) server time, including hours, minutes, seconds and milliseconds.

How can I get this (extremely precise) time stamp?

sonnuforevis
  • 1,311
  • 6
  • 22
  • 38

2 Answers2

1

Use clock_gettime(3). For more info look here.

Sven
  • 384
  • 1
  • 12
  • 1
    Looks like that doesn't exist on OSX (bah!) but there's a relatively easy reimplementation in terms of Mach calls. The answers to [this SO question](http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x) seem reasonably extensive. – jade Dec 16 '11 at 19:01
0

As you do not seem to need nanosecond precision, the correct and portable way of doing this is not to play with mach_absolute_time(), but by using gettimeofday():

#include <sys/time.h>

struct timeval tv;
gettimeofday(&tv, NULL);

You can then access the seconds since 1970-01-01 00:00 UTC in tv.tv_sec and the microseconds in tv.tv_usec.

rockpiper
  • 575
  • 5
  • 10