[英]std::chrono::system_clock vs std::chrono::high_resolution_clock behavior
Consider the following code snippet:考虑以下代码片段:
#include <chrono>
#include <cassert>
int main()
{
auto result1 = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
auto result2 = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch());
assert((result2.count() - result1.count()) < 10);
}
I am expecting that the difference in counts between the two values should be minimal (ideally less than a second).我期望两个值之间的计数差异应该是最小的(最好小于一秒)。 But with VS2015, the difference in count is of the order of Billions of seconds.
但是在 VS2015 中,计数的差异是数十亿秒的数量级。 How is this possible?
这怎么可能?
The reason it asserts for you is because high_resolution_clock
is allowed to (and often does) have a different epoch than system_clock
.它为您断言的原因是因为
high_resolution_clock
被允许(并且通常确实)具有与system_clock
不同的纪元。
It is the de facto standard (not specified but portable) that system_clock
's epoch is measuring time since 1970-01-01 00:00:00 UTC, neglecting leap seconds 1 . system_clock
的纪元是从 1970-01-01 00:00:00 UTC 开始测量时间的事实标准(未指定但可移植),忽略闰秒1 。
There is no de facto standard for high_resolution_clock
. high_resolution_clock
没有事实上的标准。 On gcc high_resolution_clock
is a typedef for system_clock
, and so on gcc platforms, you'll notice perfect synchronization.在 gcc
high_resolution_clock
是system_clock
的 typedef,在 gcc 平台上,你会注意到完美的同步。 On VS and libc++ high_resolution_clock
is a typedef for steady_clock
.在VS)和libc(++
high_resolution_clock
是一个typedef为steady_clock
。
For me, the epoch of steady_clock
is whenever the computer booted up.对我来说,
steady_clock
的时代是计算机启动时。
Here is a video tutorial for <chrono>
.这是
<chrono>
的视频教程。 It covers lots of issues, including this one, and is about an hour long.它涵盖了很多问题,包括这个问题,大约一个小时。
1 The de facto standard for system_clock
has been made official for C++20 . 1
system_clock
事实标准已成为 C++20 的官方标准。
Objects of type
system_clock
represent wall clock time from the system-wide realtime clock.system_clock
类型的对象表示来自系统范围实时时钟的挂钟时间。 Objects of typesys_time<Duration>
measure time since 1970-01-01 00:00:00 UTC excluding leap seconds.sys_time<Duration>
类型的对象测量自 1970-01-01 00:00:00 UTC 以来的时间,不包括闰秒。 This measure is commonly referred to as Unix time .此度量通常称为Unix 时间。 This measure facilitates an efficient mapping between
sys_time
and calendar types ([time.cal]).此度量有助于
sys_time
和日历类型 ([time.cal]) 之间的有效映射。 [ Example:sys_seconds{sys_days{1970y/January/1}}.time_since_epoch()
is0s
.[示例:
sys_seconds{sys_days{1970y/January/1}}.time_since_epoch()
是0s
。
sys_seconds{sys_days{2000y/January/1}}.time_since_epoch()
is946'684'800s
, which is10'957 * 86'400s
.sys_seconds{sys_days{2000y/January/1}}.time_since_epoch()
是946'684'800s
,即10'957 * 86'400s
。 — end example ]— 结束示例]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.