我正在尝试使用 c 程序从 pcap 文件中读取数据包并打印每个数据包的时间戳。
我正在使用下面的代码行来打印时间戳:
printf("%s,",ctime((const time_t*)&header->ts.tv_sec));
我的输出如下:
Mon Jan 14 09:48:18 2013
我如何获得如下所示的 YYYY-MM-DD HH:MM:SS?
2016-02-16 13:14:33.224487
我是c编程的新手,不知道我在做什么。请帮忙。谢谢!
你可能想看看localtime()
和strftime()
。
#define MYDATE_STR_MAX (<large enough> + 1)
...
struct tm lt = localtime(header->ts.tv_sec);
char st[MYDATE_STR_MAX];
strftime(st, MYDATE_STR_MAX, <format specifier as per man-page here>, lt);
/* use st here */
(包括顶部的<time.h>
)