如果未设置 clock_settime,clock_gettime() 是否有效?
Does clock_gettime() work if clock_settime has not been set?
如果 clock_settime() 未设置时钟,clock_gettime() 是否有效?我有一种情况,重复调用 clock_gettime() returns 相同的时间值(秒)。我似乎记得我需要先用 clock_settime() 设置时间。 clock_gettime() 和 time() 也使用相同的时间源吗?
clock_gettime(clockid_t clk_id, struct timespec *tp) - All implementations support the system-wide real-time clock, which is identified by CLOCK_REALTIME. Its time represents seconds and nanoseconds SINCE THE EPOCH. When its time is changed, timers for a relative interval are unaffected, but timers for an absolute point in time are affected.
time() - returns the time as the number of seconds SINCE THE EPOCH, 1970-01-01 00:00:00 +0000 (UTC).
代码是 C,平台是 Ubuntu18.04。 C++ 被标记在这个问题中,因为据我所知,C++ 不提供获取高精度时间的方法(尽管 C++20 提供),我将创建我的代码的 C++ 版本。
clock_settime
设置整台电脑的时间。只有系统管理工具(例如 GNOME Control Center) and time synchronization daemons (e.g. ntpd)才需要调用它。它需要超级用户权限;大多数程序无法成功调用它。
如果从未在特定计算机上调用 clock_settime
(或执行相同工作的旧 API 之一),则 clock_gettime
返回的值很可能是错误 与当前时间的某些权威来源相比。但是,无论时间是否准确,时钟都应该仍然是运行。我不知道 API 上 Linux 或其他 stop 系统时钟。
然而,根据您使用的时钟 ID,连续两次调用 clock_gettime
获得相同的值可能是正常的(不是错误或问题)。例如,如果两次调用之间的间隔短于时钟分辨率,则可能会发生这种情况,如 clock_getres
所报告的那样。
如果 clock_settime() 未设置时钟,clock_gettime() 是否有效?我有一种情况,重复调用 clock_gettime() returns 相同的时间值(秒)。我似乎记得我需要先用 clock_settime() 设置时间。 clock_gettime() 和 time() 也使用相同的时间源吗?
clock_gettime(clockid_t clk_id, struct timespec *tp) - All implementations support the system-wide real-time clock, which is identified by CLOCK_REALTIME. Its time represents seconds and nanoseconds SINCE THE EPOCH. When its time is changed, timers for a relative interval are unaffected, but timers for an absolute point in time are affected.
time() - returns the time as the number of seconds SINCE THE EPOCH, 1970-01-01 00:00:00 +0000 (UTC).
代码是 C,平台是 Ubuntu18.04。 C++ 被标记在这个问题中,因为据我所知,C++ 不提供获取高精度时间的方法(尽管 C++20 提供),我将创建我的代码的 C++ 版本。
clock_settime
设置整台电脑的时间。只有系统管理工具(例如 GNOME Control Center) and time synchronization daemons (e.g. ntpd)才需要调用它。它需要超级用户权限;大多数程序无法成功调用它。
如果从未在特定计算机上调用 clock_settime
(或执行相同工作的旧 API 之一),则 clock_gettime
返回的值很可能是错误 与当前时间的某些权威来源相比。但是,无论时间是否准确,时钟都应该仍然是运行。我不知道 API 上 Linux 或其他 stop 系统时钟。
然而,根据您使用的时钟 ID,连续两次调用 clock_gettime
获得相同的值可能是正常的(不是错误或问题)。例如,如果两次调用之间的间隔短于时钟分辨率,则可能会发生这种情况,如 clock_getres
所报告的那样。