2017-01-25 14 views
0

文字列として日付/時刻オブジェクトを書式設定するBoostを取得する方法を試してみるのに問題があります。私は書式指定子を設定しようとしていますが、無視され、あらかじめ定義されたスタイルで日付を出力し続けます。Boost date_time出力形式が無視されています

それはとてもここで、デモで説明するのはおそらく簡単だが、問題を示すいくつかのサンプルコードです:

#include <boost/date_time.hpp> 
#include <iostream> 

int main(void) 
{ 
    int i = 1485324852; 
    boost::posix_time::ptime pt = boost::posix_time::from_time_t(i); 

    boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet(); 
    output_facet->format("%d/%m/%Y %H:%M"); 

    std::stringstream ss; 
    ss.imbue(std::locale(std::locale::classic(), output_facet)); 
    ss << pt; 

    std::string strTime = ss.str(); 
    std::cout << "Time " << i << " converted to UI string: " << strTime << "\n"; 

    return 0; 
} 

それは私に、この出力を与える:

Time 1485324852 converted to UI string: 2017-Jan-25 06:14:12 

私は何を期待することは次のとおりです。

Time 1485324852 converted to UI string: 25/01/2017 16:14 

私はoutput_facet%d/%m/%Y %H:%M形式指定子はexample given in the Boost docsであるが、これは無視されている。

フォーマットを設定する際にどこが間違っているのかわかりません。それは誰かが私にそれを指摘することができるので、おそらく何か明らかですか?

+0

参照http://stackoverflow.com/questions/1904317/how-to-format-date-tim e-object-with-format-dd-mm-yyyy – Asalle

答えて

2

私はあなたがlocal_dateposix_timeを混合していると思います。あなたのコードの末尾に追加

はあなたの問題を解決し、目的の形式出力します。あなたはposix_timeを使用して目的のフォーマットを印刷したい場合は、変更する必要があり、それにも関わらず

boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time); 
ss >> ldt; 
ss.str(""); 
ss << ldt; 
std::cout << ss.str() << std::endl; 

を:

boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet(); 

〜:

boost::posix_time::time_facet *output_facet = new boost::posix_time::time_facet(); 
1

さて、私はそれを再現しました。あなたのコードで正しい出力を得ることができません。 ここにスニペットがあり、正しい出力を与えることができます。私が見る唯一の違いはlocal_time_facettime_facetです。

#include <iostream> 
#include <boost/date_time/posix_time/posix_time.hpp> 
#include <boost/date_time/posix_time/posix_time_io.hpp> 

using namespace boost::posix_time; 
using namespace std; 

int main(int argc, char **argv) { 
    boost::posix_time::ptime now = boost::posix_time::from_time_t(123456789); 
    time_facet *facet = new time_facet("%d/%b/%Y %H:%M:%S"); 
    cout.imbue(locale(cout.getloc(), facet)); 
    cout << now << endl; 
} 
+0

どういう意味なのか分かりません。私は 'local_time'を使っていません。上記のコードにあなたの提案した変更を加えれば、それはあなたのためにコンパイルされ、期待される 'dd/mm/yyyy'フォーマットを生成しますか? – Malvineous

+0

つまらない、私は間違っていた:) – Asalle

関連する問題