【发布时间】:2012-11-29 10:16:18
【问题描述】:
我正在尝试将std::time_point 保存到std::stream 中并读回。一个问题是在某处使用标准函数会“损失”一个小时。即,我阅读的时间比我写作的时间晚 1 小时。我怀疑我需要在某个地方设置夏令时。我编写了一个小程序,将时间打印到std::stringstream 并读回。
#include <iomanip>
#include <iostream>
#include <sstream>
#include <chrono>
#include <ctime>
using std::chrono::system_clock;
namespace chrono = std::chrono;
void test();
int main(int argc, char** argv)
{
std::stringstream ss;
auto start = system_clock::now();
std::time_t ts = system_clock::to_time_t(start);
std::tm time_out = *std::localtime(&ts);
ss << std::put_time(&time_out, "%Y-%m-%d %H:%M:%S %Z") << '\n';
std::cout << ss.str() << std::endl;
std::tm time_in;
ss >> std::get_time(&time_in, "%Y-%m-%d %H:%M:%S %Z");
std::cout << "Are time dsts equal? : " <<
(time_out.tm_isdst == time_in.tm_isdst) << '\n';
std::time_t rawTime = std::mktime(&time_in);
auto end = std::chrono::system_clock::from_time_t(rawTime);
std::cout << "Are time points equal? : " << (start == end) << '\n';
// print the trouble makers
std::time_t start_time = system_clock::to_time_t(start);
std::time_t end_time = system_clock::to_time_t(end);
std::cout << "times: \n"
<< '\t' << std::put_time(std::localtime(&start_time), "%c %z") << '\n'
<< '\t' << std::put_time(std::localtime(&end_time), "%c %z") << '\n';
// this is a source of strange behaviour...
// std::cout << "Difference: "
// << chrono::duration_cast<chrono::seconds>(start - end).count()
// << std::endl;
return 0;
}
最奇怪的是程序打印了以下内容:
Are time dsts equal? : 1
Are time points equal? : 0
times:
Tue Dec 11 19:26:24 2012 +0000
Tue Dec 11 19:26:24 2012 +0000
当我在程序末尾取消注释 3 行(打印时间点之间的差异)时,结果是:
Are time dsts equal? : 0
Are time points equal? : 0
times:
Tue Dec 11 19:29:40 2012 +0000
Tue Dec 11 18:29:40 2012 +0000
Difference: 3600
请注意,dst(夏令时)突然不相等,时间也不相等。
我在带有 XCode46-DP2 的 Mac OS X 10.8.2 上使用 libc++。我使用的 clang++ 版本是 Apple clang version 4.1 和 clang version 3.2 (trunk 167239)
我想我的问题是: A)关于 1 小时的差异,这是我的库中的错误还是我没有正确使用标准函数? (后者不会让我感到惊讶...)
B) 当我取消注释程序末尾的三行时,代码发生了什么?这对我来说似乎是一个错误。有人愿意在他们的平台上试用吗?
【问题讨论】:
-
对于 chrono/io 你可能喜欢使用 boost chrono_io boost.org/doc/libs/1_52_0/doc/html/chrono/…
-
@JohanLundberg:这两个都是我的代码,所以切换不太可能解决问题。正在调查...
标签: c++ time c++11 timezone chrono