Question

How can we do this (get local time with milliseconds) without Boost? I have something like this:

time_of_day = time(NULL);

time_str_tm = localtime(&time_of_day);

printf("\n%02i:%02i:%02i\n", time_str_tm->tm_hour, time_str_tm->tm_min, time_str_tm->tm_sec);

but the tm structure has only seconds at last...

any suggestions?

Was it helpful?

Solution

You need gettimeofday(2)

struct timeval time_now;
gettimeofday(&time_now, NULL);
time_str_tm = gmtime(&time_now.tv_sec);

printf("\n%02i:%02i:%02i:%06i\n"
   , time_str_tm->tm_hour
   , time_str_tm->tm_min
   , time_str_tm->tm_sec
   , time_now.tv_usec);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top