【问题标题】:Inconsistent '%f' behaviour between boost::posix_time::time_facet and boost::posix_time::time_input_facetboost::posix_time::time_facet 和 boost::posix_time::time_input_facet 之间的“%f”行为不一致
【发布时间】:2019-05-02 14:33:29
【问题描述】:

我正在尝试将 boost::posix_time::ptime 转换为特定的字符串格式(扩展 ISO),然后再转换回 boost::posix_time::ptime

令人惊讶的是,boost::posix_time::time_facet%f 的意思是 000000999999(没有小数点分隔符)。但是对于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::time_facetboost::posix_time::input_time_facet 中的错误?

注意:'%F' 没有这个问题。

【问题讨论】:

标签: c++ boost


【解决方案1】:

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

使用%F 效果更好,但如果时间没有十进制信息,则会有不同的行为: Try this

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多