Jitesh Dani
Jitesh Dani

Reputation: 385

How can I get the current time in milliseconds using C?

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.

Upvotes: 3

Views: 2699

Answers (2)

Luca
Luca

Reputation: 11961

I use ftime for time tracking (link text)

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

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

Upvotes: 5

Related Questions