将CLOCK_BOOTTIME转换为CLOCK_REALTIME的函数是什么?

lf5gs5x2  于 2023-05-06  发布在  其他
关注(0)|答案(1)|浏览(114)

我写了一个class to listen for process events via a PF_NETLINK socket connection
recv()函数会收到一条消息,其中包含一个时间戳,表示自内核上次 Boot 以来的纳秒数。
将这样的时间戳转换为真实的的函数是什么?
我唯一能想到的是使用clock_gettime()函数读取两个值并生成偏移量。但我想确定的是,现在还没有一个图书馆为我们做这件事。

struct timespec realtime;
struct timespec boottime;

clock_gettime(CLOCK_REALTIME, &realtime);
clock_gettime(CLOCK_BOOTTIME, &boottime);

// the following is pseudo code since timespec is a struct
struct timespec offset = realtime - boottime;

struct timespec event_timestamp = ...; // value from PF_NETLINK event

struct timespec event_realtime = event_timesamp + offset;

这看起来很难看,这就是为什么我希望有一个C库函数为我们做所有这些。有吗?如果有,那是什么?

gxwragnw

gxwragnw1#

没有函数,它不可能存在- CLOCK_REALTIME是任意的,可以由用户设置,并且是非单调的。实时时钟设置的历史记录不存储在任何数据库中。如果你不知道在某个时刻的真实的时间,你就没有办法知道。

相关问题