ctimeval

warning: format '%06d' expects type 'int', but argument 5 has type '__suseconds_t'


I am using this code:

struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[64], buf[64];

gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof buf, "%s.%06d", tmbuf, tv.tv_usec);

from this SO answer:

https://stackoverflow.com/a/2409054/997112

to print a struct timeval in to a readable format. However, I get this compiler warning:

warning: format '%06d' expects type 'int', but argument 5 has type '__suseconds_t'

Could somebody please help?


Solution

  • The tv_usec member of struct tv structure is a __suseconds_t datatype, which is a typedef to long. You can display it with %06ld instead of %06d.

    And it seems like a cast to long would be better, at least for portability issues.