【问题标题】:Boost Gregorian Date format make wrong resultBoost Gregorian Date 格式产生错误的结果
【发布时间】:2016-03-15 03:18:28
【问题描述】:

这是我的代码。我希望输出“2016-03-15”。 但在我的 Ubuntu 14.04、g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4、eclipse 调试环境中,下面的代码输出“2016-Mar-15”。

boost::gregorian::date current_date(boost::gregorian::day_clock::local_day());
boost::gregorian::date_duration dd(offset);
boost::gregorian::date offset_date = current_date - dd;

auto facet = new pt::time_facet("%Y-%m-%d");
std::stringstream ss;
ss.imbue(std::locale(std::cout.getloc(), facet));
ss << offset_date;

std::cerr << ss.str() << std::endl;

构建并运行,结果是

2016-Mar-15

我想知道结果...与语言环境或其他东西有什么关系吗?

【问题讨论】:

标签: c++ boost


【解决方案1】:

谢谢。全部。我解决了! 这是我的错误。为大家所知, 我留下我的答案:) 我喜欢 Stackoverflow!

using namespace boost::gregorian;
using namespace boost::local_time;
using namespace boost::posix_time;

date current_date(boost::gregorian::day_clock::local_day());
date_duration dd(offset);
date offset_date = current_date - dd;

这是正确的。我应该使用“date_facet”而不是“time_facet”

auto facet = new boost::gregorian::date_facet();
std::stringstream ss;
facet->format("%Y-%m-%d");
ss.imbue(std::locale(locale::classic(), facet));
ss << offset_date;
std::cerr << ss.str() << std::endl; // output : 2016-03-15

这是错误的。我不应该使用“time_facet”

auto facet2 = new boost::posix_time::time_facet();
std::stringstream ss2;
facet->format("%Y-%m-%d");
ss2.imbue(std::locale(locale::classic(), facet2));
ss2 << offset_date;
std::cerr << ss2.str() << std::endl;  // output : 2016-Mar-15

【讨论】:

    猜你喜欢
    • 2016-10-25
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多