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

发布于 2024-08-09 00:27:12 字数 294 浏览 3 评论 0原文

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

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

问题是:是否有一种标准方法可以在这两者之间进行转换?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-08-16 00:27:13

看看这个文档,我想乘以tv_usec 乘以 1000 就足以获得 tv_nsec

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

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.

悲欢浪云 2024-08-16 00:27:13

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

TIMEVAL_TO_TIMESPEC(X, Y)

TIMESPEC_TO_TIMEVAL(X, Y)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文