有没有将struct timeval转换为struct timespec的标准方法?

时间:2021-02-18 20:48:01

struct timeval represents and instant in time with two members, tv_sec (seconds) and tv_usec (microseconds). In this representation, tv_usec is not by itself an absolute time it is a sub second offset off of tv_sec.

struct timeval用两个成员表示并及时,tv_sec(秒)和tv_usec(微秒)。在此表示中,tv_usec本身不是绝对时间,它是tv_sec的子秒偏移量。

struct timespec works the same way except that instead of microseconds it's offset (tv_nsec) is stored in nanosecond units.

struct timespec的工作方式相同,只是它的偏移量(tv_nsec)以纳秒单位存储,而不是微秒。

The question is: Is there a standard way to convert between these two?

问题是:有没有一种标准的方法来转换这两者?

2 个解决方案

#1


14  

Looking at this doc, I would think multiplying tv_usec by 1000 is sufficient to get tv_nsec.

看看这个文档,我认为将tv_usec乘以1000足以获得tv_nsec。

More important, I suspect is the source of the different structures: they could be filled by different clocks.

更重要的是,我怀疑是不同结构的来源:它们可以由不同的时钟填充。

#2


12  

In sys/time.h there are two macros that do what you want:

在sys / time.h中有两个宏可以执行您想要的操作:

TIMEVAL_TO_TIMESPEC(X, Y)

and

TIMESPEC_TO_TIMEVAL(X, Y)

See the docs here: http://www.daemon-systems.org/man/TIMEVAL_TO_TIMESPEC.3.html

请参阅此处的文档:http://www.daemon-systems.org/man/TIMEVAL_TO_TIMESPEC.3.html

#1


14  

Looking at this doc, I would think multiplying tv_usec by 1000 is sufficient to get tv_nsec.

看看这个文档,我认为将tv_usec乘以1000足以获得tv_nsec。

More important, I suspect is the source of the different structures: they could be filled by different clocks.

更重要的是,我怀疑是不同结构的来源:它们可以由不同的时钟填充。

#2


12  

In sys/time.h there are two macros that do what you want:

在sys / time.h中有两个宏可以执行您想要的操作:

TIMEVAL_TO_TIMESPEC(X, Y)

and

TIMESPEC_TO_TIMEVAL(X, Y)

See the docs here: http://www.daemon-systems.org/man/TIMEVAL_TO_TIMESPEC.3.html

请参阅此处的文档:http://www.daemon-systems.org/man/TIMEVAL_TO_TIMESPEC.3.html