我試圖在00:00:00這樣的字符串中顯示高分辨率時間點之間的差異。我的問題是,當我打印它時,時間爲+1小時。std chrono用ctime轉換char *時出現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',但[文檔](http://en.cppreference.com/w/cpp/chrono/c/ctime)似乎表明時間使用' localtime'。 – 2014-10-08 22:05:00
localtime返回一個struct tm *。我可以編輯時間,但我認爲它應該是正確的,而不是像現在這樣增加1小時。 – fonZ 2014-10-08 22:17:37
如果您不想使用時區,那麼使用不使用時區的函數(可能是'gmtime'或任何其他選項)執行轉換。 – 2014-10-08 22:22:44