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.

1

2 Answers 2

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.
0

I use ftime for time tracking (link text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.