这是我的代码。我希望输出“ 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
我想知道结果...与语言环境有什么关系吗?
最佳答案
谢谢。所有。我解决了!
是我的错对于每个人的知识,
我留下我的答案:)
我爱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