【问题标题】:boost Date_Time date parsing doesn't workboost Date_Time 日期解析不起作用
【发布时间】:2014-05-01 17:21:14
【问题描述】:

我正在尝试使用 boost 1.55 Date_Time 库编写代码来解析日期时间字符串。但它总是产生非日期时间的日期。

boost::gregorian::date d(2005, 6, 25);
boost::gregorian::date d2;
boost::gregorian::date_facet* facet(new boost::gregorian::date_facet("%Y %m %d"));
stringstream ss;
ss.imbue(std::locale(std::cout.getloc(), facet));

ss << d; string s = ss.str(); // s = "2005 06 25"
cout << s << endl;
    stringstream ss2(s);
ss2 >> d2; // not-a-date-time
cout << d2 << endl;

我尝试了不同的格式说明符,但没有帮助。 我正在使用 Visual C++ 2013。 我的代码有问题吗?

更新:

如果这有什么不同的话,我的系统区域设置是俄语。

【问题讨论】:

  • 我可以在 Mac OS 上重现这个
  • 好像......嗯......你在写入流后读取空的结尾? ss 不需要被寻找 0 或重置什么的吗?
  • @ebyrob 那也是,请参阅我编辑的答案。然而,事实也丢失了
  • @ebyrob 是的。我过度简化了示例代码。我现在已经修好了。

标签: c++ boost


【解决方案1】:

你想解析所以你需要输入方面:

Live on Coliru

#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian/gregorian_io.hpp>
#include <iostream>

int main()
{
    boost::gregorian::date const d(2005, 6, 25);
    boost::gregorian::date d2;

    std::stringstream oss;
    oss.imbue(std::locale(std::cout.getloc(), new boost::gregorian::date_facet("%Y %m %d")));

    oss << d; 

    oss.imbue(std::locale(std::cout.getloc(), new boost::gregorian::date_input_facet("%Y %m %d")));

    if (oss >> d2)
        std::cout << d2 << std::endl;
    else
        std::cout << "Not parsed\n";
}

打印

2005-Jun-25

在我的机器上

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多