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

How can I get the current time in milliseconds in C? I do the 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, it struct tmdoes not have a millisecond term in it.

+3
source share
2 answers
  • On Unix, use gettimeofday () to get the response in microseconds and scale to milliseconds.
  • Or use POSIX clock_gettime () to get the response in nanoseconds and scale to milliseconds.
+5
source

I use ftime to track time ( link text )

0
source

Source: https://habr.com/ru/post/1716727/


All Articles