将C#DateTime对象转换为libpcap捕获文件时间戳

Emi*_*elt 1 c# datetime packet-sniffers

我坚持将一个DateTime对象转换为C#中的libpcap捕获文件格式的时间戳(也由wireshark,文件格式definitiom使用).我无法将对象转换为的时间戳是数据包(记录)标头中的时间戳(guint32 ts_sec和guint32 ts_usec).

Ree*_*sey 6

你可以这样做:

DateTime dateToConvert = DateTime.Now;
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date - origin;

// Seconds since 1970
uint ts_sec = Math.Floor(diff.TotalSeconds);
// Microsecond offset
uint ts_usec = 1000000 * (diff.TotalSeconds - ts_sec);
Run Code Online (Sandbox Code Playgroud)