【发布时间】:2021-08-24 11:19:07
【问题描述】:
我正在尝试编写一个函数,它将返回自 1970 年以来的当前时间(以微秒为单位)。在进行调试时,我注意到返回的数字太小了。例如:269104616249。我还添加了 static_assert 来检查返回的值类型是 int64_t,它大到足以容纳 292471 年(以微秒为单位)。所以这里不应该出现整数溢出。
我究竟做错了什么?
这是我的代码:
int64_t NowInMicroseconds() {
static_assert(std::is_same<decltype(duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count()), int64_t>::value);
return duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
}
int64_t result = NowInMicroseconds();
【问题讨论】:
-
我检查了 int64_t 的最大值是 9223372036854775807,足以容纳 292471 年的微秒。
-
我收到
1629804220239351here。我们可以得到minimal reproducible example吗? -
@NathanOliver 这不是也只有 5 年吗?
-
最小的可重现示例只是调用函数 NowInMicroseconds
-
@user32434999 我得到了大约 51 年的价值。看起来你可能会偏离 10 倍