时区根本不被解析:
Live On Coliru
#include <iostream>
#include <sstream>
#include <boost/date_time.hpp>
int main()
{
boost::posix_time::ptime time;
try
{
std::stringstream ss("2014-12-19T15:53:14.533+01:00");
boost::local_time::local_time_input_facet* ifc= new boost::local_time::local_time_input_facet();
ifc->set_iso_extended_format();
ss.imbue(std::locale(ss.getloc(), ifc));
boost::local_time::local_date_time zonetime(boost::local_time::not_a_date_time);
if( ss >> zonetime ) {
time = zonetime.utc_time();
}
std::string remains;
if (std::getline(ss, remains))
std::cout << "Remaining: '" << remains << "'\n";
}
catch( std::exception const& e )
{
std::cout << "ERROR:" << e.what() <<std::endl;
}
std::cout << "UTC time: " << time;
return 0;
}
印刷:
Remaining: '+01:00'
UTC time: 2014-Dec-19 15:53:14.533000
文档说:
目前,local_date_time 对象只能使用 Posix 时区字符串进行流式传输。Posix 时区字符串的完整描述可以在 posix_time_zone 类的文档中找到。
因此,您需要单独解析时区或使用 POSIX 时区。是的,我认为这是非常令人困惑和不足的。实际上,您不能对当地时间使用扩展的 iso 格式。
您可能可以%ZP
改用:
Live On Coliru
#include <iostream>
#include <sstream>
#include <boost/date_time.hpp>
int main()
{
boost::posix_time::ptime time;
try
{
std::stringstream ss("2014-12-19T15:53:14.533-07:00");
boost::local_time::local_time_input_facet* ifc= new boost::local_time::local_time_input_facet("%Y-%m-%d %H:%M:%S%F %ZP");
//ifc->set_iso_extended_format();
ss.imbue(std::locale(ss.getloc(), ifc));
using namespace boost::local_time;
local_date_time ldt(not_a_date_time);
if(ss >> ldt) {
time = ldt.utc_time();
std::string remains;
if (std::getline(ss, remains))
std::cout << "Remaining: '" << remains << "'\n";
}
}
catch( std::exception const& e )
{
std::cout << "ERROR:" << e.what() <<std::endl;
}
std::cout << "UTC time: " << time;
return 0;
}
印刷
UTC time: 2014-Dec-19 22:53:14.533000