早晨!
我正在尝试从 timeval 结构中获取纳秒时间戳(uint64_t)。
struct timeval t;
// initialized so that ....
print("t sec: %ld, micro sec: %ld", t.tv_sec, t.tv_usec);
// .... prints "t sec: 4, micro sec: 130728"
long int sec = (long int) t.tv_sec;
// just did that because I thought that using time_t in calculations may cause errors
// print("%ld", sec) -> 4, so this seems to work as expected
uint64_t t_int = (sec * 1000000 + t.tv_usec) * 1000; // should be nanoseconds
print("t_int (uint64_t) %llu", t_int);
// prints "t_int (uint64_t) 18446744073545312320"
我的 timevals 绝对应该适合 uint64_t。此外,从 time_t 转换为 long int 有效,所以我所做的只是用两个 long int 进行简单计算,将结果放入 uint64_t,所以我不确定为什么我得到一个明显错误的值。有任何想法吗?这只是一种错误的格式化方式(使用 %llu)吗?