私はこのコードを使用しています:
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);
このSOの回答から:
https://stackoverflow.com/a/2409054/997112
struct timeval を読み取り可能な形式で出力します。ただし、次のコンパイラ警告が表示されます。
warning: format '%06d' expects type 'int', but argument 5 has type '__suseconds_t'
誰か助けてくれませんか?