【发布时间】:2014-10-08 21:48:30
【问题描述】:
我试图在 00:00:00 这样的字符串中显示高分辨率时间点之间的差异。我的问题是我打印的时间是+1小时。
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
using namespace std;
using namespace chrono;
int main(int argc, char **argv) {
auto start = high_resolution_clock::now();
this_thread::sleep_for(seconds(1));
auto stop = high_resolution_clock::now();
auto result = stop - start;
time_t t = duration_cast<seconds>(result).count();
cout << ctime(&t) << endl;
return EXIT_SUCCESS;
}
打印出来
Thu Jan 1 01:00:01 1970
虽然我希望它打印出来
Thu Jan 1 00:00:01 1970
以下打印件 1:
cout << (int) duration_cast<seconds>(result).count() << endl;
也许它与时区有关?任何帮助表示赞赏!
【问题讨论】:
-
好久没用
ctime了,不过the documentation好像表示时间是用localtime换算的。 -
localtime 返回一个结构体 tm*。我可以编辑时间,但我认为它应该是正确的,而不是像现在这样增加 1 小时。
-
如果您不想使用时区,请使用不使用时区的函数执行转换,可能是
gmtime或任何其他替代方法。跨度> -
@RetiredNinja 哇,gmtime 救了我!我尝试并获得了预期的输出,非常感谢。用它写一个答案并且不接受它。
-
嘿@fonZ,我更新了我的答案。你应该可以通过这个走得更远。