【问题标题】:Clock cycle of time_t and getting time problemtime_t 的时钟周期和获取时间问题
【发布时间】:2020-01-20 04:01:21
【问题描述】:

我试图在我的简单 while 循环中获取当前的原始时间(人类不可读的时间)。我搜索了 time_t 时钟周期,但找不到任何有关它的信息。

第一期:有人知道C++中time_t的时钟周期吗?

第二个问题:当我打印时间 ltime 时,它​​看起来一直都是一样的。

time_t ltime;
while (count <= 1000) {
  time( & ltime); //I expect this to get current time in every cycle
  cout << "Current time " << ltime << endl;
  count++;
}

我已经检查了this 链接,但它没有提供有关时钟周期的信息。

【问题讨论】:

  • 当然可以:“将时间返回为...”。所以time_t的分辨率是1秒。
  • @rustyx 现在我明白了,谢谢,你对第二期有任何想法
  • 第二期:运行时间可能不到 1 秒。只有 1000 次迭代。

标签: c++ time-t


【解决方案1】:

使用std::chrono::high_resolution_clock::now()

编辑:

使用std::system_clock::now()std::chrono::system_clock::to_time_t()

【讨论】:

  • 在实际的长代码中我把它放在uint32_t映射中,所以它的类型不合适所以我选择了time_t
  • 谢谢uint32_t time = static_cast&lt;uint32_t&gt;(std::chrono::duration_cast&lt;std::chrono::seconds&gt;(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
  • 你不应该用 32 位来表示时间。考虑 2038 年的问题。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2011-05-23
  • 2018-06-02
  • 2017-09-24
相关资源
最近更新 更多