Is there a standard way to convert a struct timeval into a struct timespec?

我只是一个虾纸丫 提交于 2019-12-05 15:23:09

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1618433/is-there-a-standard-way-to-convert-a-struct-timeval-into-a-struct-timespec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!