如何打印从 pcap 文件读取的数据包的时间戳?

How to print timestamp of a packet read from pcap file?

我正在尝试使用 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() and 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>