Is there a standard way to convert a struct timeva

2019-04-04 23:59发布

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 timespec works the same way except that instead of microseconds it's offset (tv_nsec) is stored in nanosecond units.

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

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-05 00:17

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

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

查看更多
地球回转人心会变
3楼-- · 2019-04-05 00:25

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

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

查看更多
登录 后发表回答