2016-03-15 77 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