2016-03-15 79 views
0

这是我的代码。我期望输出“2016-03-15”。 但下面的代码在我的Ubuntu 14.04中输出“2016-Mar-15”,g ++(Ubuntu 4.8.4-2ubuntu1〜14.04.1)4.8.4,eclipse调试环境。Boost Gregorian日期格式错误结果

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 

我不知道结果......为区域或什么什么关系?

+0

http://stackoverflow.com/a/32780366/1004522 –

回答

2

谢谢。所有。我解决它! 这是我的错误。为大家的知识, 我离开我的答案:) 我爱的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