unix输出时间的几种方式之clock_gettime函数(纳秒级)
程序员文章站
2024-01-21 22:32:34
...
#include <time.h>
#include <stdio.h>
int main(int argc, char** argv)
{
struct timespec ts;
struct tm *tmp;
char time[64], buff[64];
clock_gettime(CLOCK_REALTIME, &ts);
tmp = localtime(&ts.tv_sec);
strftime(time, 64, "%F,%T", tmp);
snprintf(buff, sizeof(buff), "%s.%ld\n",time, ts.tv_nsec);
printf("%s", buff);
return 0;
}
编译
gcc time.cpp -o time
结果