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
  • 37,540
  • 37
  • 125
  • 190
Jitesh Dani
  • 375
  • 3
  • 8
  • 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
  • 698,132
  • 130
  • 858
  • 1,229
0

I use ftime for time tracking (link text)

Luca
  • 11,229
  • 10
  • 67
  • 122