我正在尝试将 boost::chrono::system_clock::time_point 输出到文件流,这样它将显示本地时间,但与 UTC 时间不同,例如:
2015-05-08 11:49:07.890992700 -0400
基于documentation这应该可以通过使用带有本地时区的 time_fmt 操纵器来实现:
std::ofstream file("MyFile.txt");
boost::chrono::system_clock::time_point timePoint = boost::chrono::system_clock::now();
file << boost::chrono::time_fmt(boost::chrono::timezone::local)
<< timePoint;
然而,我得到的结果是:
2015-05-08 11:49:07.890992700 Eastern Daylight Time
基本上,我希望将“东部夏令时”时区字符串替换为与 UTC 的“-0400”时区差异。我猜结果可能取决于系统本地设置。无论系统设置如何,有没有办法实现这一目标?
最佳答案
据我所知,+HHMM 格式被称为 POSIX 时区。
FWIW 我猜你的意思是 system_clock::now()
,下面是在我的系统上运行的一些测试:
$ TZ='Asia/Kathmandu' ./test
2015-05-10 04:46:40.655817854 +0545
$ TZ='America/Detroit' ./test
2015-05-09 18:59:57.022323975 -0400
所以它似乎确实取决于平台和潜在的配置(但我不认为它取决于选项的语言环境设置)。
//#include <boost/chrono/chrono_io.hpp>
#include <boost/chrono/time_point.hpp>
#include <boost/chrono/io/time_point_io.hpp>
#include <boost/chrono/chrono.hpp>
#include <iostream>
int main() {
boost::chrono::system_clock::time_point timePoint = boost::chrono::system_clock::now();
std::cout
<< boost::chrono::time_fmt(boost::chrono::timezone::local)
<< timePoint
<< "\n";
}
[1] 我使用 tzselect
解决了这个问题:
The following information has been given:
United States
Eastern Time - Michigan - most locations
Therefore TZ='America/Detroit' will be used.
Local time is now: za mei 9 18:59:38 EDT 2015.
Universal Time is now: za mei 9 22:59:38 UTC 2015.
关于c++ - 输出 boost::chrono::system_clock::time_point 作为相对于 UTC 的本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129809/