简体   繁体   English

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

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

I am trying to read the packets from a pcap file using ac program and print the time-stamp of each packet. 我正在尝试使用ac程序从pcap文件中读取数据包,并打印每个数据包的时间戳。

I am using the below line of code to print the timestamp: 我正在使用下面的代码行来打印时间戳:

printf("%s,",ctime((const time_t*)&header->ts.tv_sec));

And my output is as below: 我的输出如下:

Mon Jan 14 09:48:18 2013

How do I get it as YYYY-MM-DD HH:MM:SS like the one shown below? 如何获得YYYY-MM-DD HH:MM:SS,如下所示?

2016-02-16 13:14:33.224487

I am new to c programming and have no clue what i am doing. 我是C编程新手,不知道我在做什么。 Please help. 请帮忙。 Thanks! 谢谢!

You might like to have a look at localtime() and strftime() . 您可能想看看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 */

(include <time.h> on top) (在顶部包括<time.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM