boost::local_time 读错了 iso_extended_format

boost::local_time Does not read correct iso_extended_format

我需要读取 ISO 扩展格式的时间,并将其转换为 UTC 时间,就像 Javascript 的 Date 对象一样:

new Date("2014-12-19T15:53:14.533+01:00")

2014-12-19T14:53:14.533Z

在boost中,没有boost::posix_time::from_iso_extended_string并且boost::posix_time::from_iso_string不解析时区。我尝试使用 boost::local_time::local_date_time 但它似乎也不起作用:

    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();
    }

有什么帮助或建议吗?

Live on coliru

时区根本没有被解析:

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

documentation says:

At this time, local_date_time objects can only be streamed in with a Posix Time Zone string. A complete description of a Posix Time Zone string can be found in the documentation for the posix_time_zone class.

因此,您需要单独解析时区或使用 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