【问题标题】:How to print timestamp of a packet read from pcap file?如何打印从 pcap 文件读取的数据包的时间戳?
【发布时间】:2017-10-15 16:39:23
【问题描述】:

我正在尝试使用 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 编程新手,不知道我在做什么。请帮忙。 谢谢!

【问题讨论】:

  • 2013 年,真的吗? ;-)

标签: c formatting timestamp packet libpcap


【解决方案1】:

您可能想看看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 */

(包括顶部的&lt;time.h&gt;

【讨论】:

  • 成功了。我用 strftime(buff,20,"%Y-%m-%d %H:%M:%S", localtime(&now));
  • strftime 缺少max 参数;)
  • @AnttiHaapala:哎哟...... - 已修复。
猜你喜欢
  • 2015-09-05
  • 1970-01-01
  • 2018-01-25
  • 2015-03-30
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2020-08-28
  • 2023-03-10
相关资源
最近更新 更多