3

How might I get the current time in milliseconds in C? I am doing following to get the time in seconds:

struct tm ptm;

now = time(NULL);

localtime_r(&now,ptm);

myTime= (ptm->tm_hour * 3600) + (ptm->tm_min * 60) + (ptm->tm_sec);

Looking at time.h, struct tm does not have the millisecond member in it.

zengr
  • 38,346
  • 37
  • 130
  • 192
Jitesh Dani
  • 385
  • 3
  • 10
  • 17
  • 1
    See also http://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi-c http://stackoverflow.com/questions/173409/how-can-i-find-the-execution-time-of-a-section-of-my-program-in-c – John Carter Sep 02 '09 at 22:05

2 Answers2

5
  • On Unix, use gettimeofday() to get the answer in microseconds and scale to milliseconds.
  • Or use POSIX clock_gettime() to get the answer in nanoseconds and scale to milliseconds.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

I use ftime for time tracking (link text)

Luca
  • 11,646
  • 11
  • 70
  • 125