![](/img/trans.png)
[英]boost::serialize and std::chrono::system_clock::time_point
[英]Output boost::chrono::system_clock::time_point as local time with respect to UTC
我正在尝试将boost :: chrono :: system_clock :: time_point输出到文件流,以便它将显示本地时间,但与UTC时间有所不同,例如:
2015-05-08 11:49:07.890992700 -0400
基于将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”时区差异。 我猜测结果可能取决于系统本地设置。 无论系统设置如何,都有一种方法可以实现此目的?
AFAIK的+ 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
因此,它似乎确实取决于平台和潜在的配置(但是我不认为它取决于选项的语言环境设置)。
另请参阅Live On Coliru
//#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
弄清楚了这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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.