0

I'm writing a datalogging program in C on the Intel Edison. The program writes rows of tab-separated data to a log file at irregular but frequent intervals. I'd like to record the current time, or at least the time elapsed since the start along with the data, with at least .01 second precision.

I've been searching for a function that can help me with this, but everything I've found can only give me 1 second precision.

How can I achieve what I'd like to do?

0andriy
  • 4,183
  • 1
  • 24
  • 37

1 Answers1

1

Assuming you'll be running on Linux, you can use the gettimeofday function, which can return the system time with microsecond resolution. You can run this as often as you need to get timestamps.

Edit:

As Jonathan mentioned in his comment, you can also use clock_gettime as gettimeofday is deprecated, although the latter is more portable.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    You could also use `clock_gettime()` on Linux, which has the additional merit of not being deprecated (POSIX has marked `gettimeofday()` obsolescent). Or `ftime()`, which is also deprecated. Etc. Using `gettimeofday()` actually gives maximum portability; it is available on Mac OS X and BSD, unlike `clock_gettime()`. – Jonathan Leffler Jul 25 '15 at 03:45
  • I ended up going with clock_gettime() and it was exactly what I needed. Thanks! – James Vaughan Jul 25 '15 at 05:09