gpt4 book ai didi

c - TimeVal 结构秒和微秒

转载 作者:行者123 更新时间:2023-12-04 09:43:43 25 4
gpt4 key购买 nike

我如何知道从 1970 年 1 月 1 日 00:00:00 到现在使用 timeval 之间的秒数和微秒数?谢谢。

struct timeval {
long tv_sec; /*seconds since 1/1/1970*/
long tv_usec; /*microseconds since tv_sec*/
};

最佳答案

您调用gettimeofday()

struct timeval tv;
gettimeofday(&tv, NULL);
tv.tv_sec /* seconds */
tv.tv_usec /* microseconds */

然而 gettimeofday()已过时,手册推荐 clock_gettime(2)反而:
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
tp.tv_sec /* seconds */
tp.tv_usec /* nanoseconds divide by 1000 to get microseconds*/

关于c - TimeVal 结构秒和微秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13661753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com