代码之家  ›  专栏  ›  技术社区  ›  jpo38

boost::posix_time::time_方面和boost::posix_time::time_input_方面之间的“%f”行为不一致

  •  0
  • jpo38  · 技术社区  · 7 年前

    我正在试着转换一个 boost::posix_time::ptime 转换为特定的字符串格式(扩展ISO),然后返回到 boost::posix_time::ptime .

    令人惊讶的是,它看起来像 boost::posix_time::time_facet %f 000000 999999 (不带小数点分隔符)。要不是 boost::posix_time::time_input_facet ,意思是 .000000 .999999 (带小数点分隔符)。

    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <iostream>
    
    int main( int argc, char** argv )
    {
        auto now = boost::posix_time::second_clock::local_time();
    
        std::stringstream outStr;
        {
            boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
            facet->format("%Y-%m-%dT%H:%M:%S.%f");
            outStr.imbue(std::locale(std::locale::classic(), facet));
            outStr << now;
        }
        std::cout << outStr.str() << std::endl;
    
        {
            static const std::string format = "%Y-%m-%dT%H:%M:%S.%f";
            const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
            std::istringstream is(outStr.str());
            is.imbue(loc);
            boost::posix_time::ptime converted;
            is >> converted;
            std::cout << converted << std::endl;
        }
    
        {
            static const std::string format = "%Y-%m-%dT%H:%M:%S%f";
            const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
            std::istringstream is(outStr.str());
            is.imbue(loc);
            boost::posix_time::ptime converted;
            is >> converted;
            std::cout << converted << std::endl;
        }
    
        return 0;
    }
    

    This outputs

    2019-04-30T12:23:29.000000
    not-a-date-time
    2019-Apr-30 12:23:29
    

    2019-04-30T12:23:29.000000
    2019-Apr-30 12:23:29
    not-a-date-time
    

    我用的是boost 1.68。

    我是做错了什么,还是这是一个错误 boost::posix_time::input_time_facet ?

    注意:'%F'没有此问题。

    0 回复  |  直到 7 年前
        1
  •  0
  •   jpo38    7 年前

    这绝对是一个boost bug。我在这里填写问题: https://github.com/boostorg/date_time/issues/102

    使用 %F Try this