2011-10-26 3 views
5

現在の時刻をローカルシステムに単純化する方法は誰でも知っていますか?今すぐ簡単に人間が読める日時を後で

boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time(); 
boost::posix_time::time_facet *facet = new boost::posix_time::time_facet("%d-%m-%Y %H:%M:%S"); 

私はcout.imbueを使用するように言うが、私はただ単純な文字列をしたいの例を見てきました。 "%d-%m-%Y %H:%M:%S"または任意のファセット希望に

void FormatDateTime(
    std::string const&    format, 
    boost::posix_time::ptime const& date_time, 
    std::string&     result) 
    { 
    boost::posix_time::time_facet * facet = 
     new boost::posix_time::time_facet(format.c_str()); 
    std::ostringstream stream; 
    stream.imbue(std::locale(stream.getloc(), facet)); 
    stream << date_time; 
    result = stream.str(); 
    } 

セット形式:

答えて

6

あなたはこのコードを試すことができます。

現地時間では、boost::posix_time::second_clock::local_time()を第2引数(date_time)として使用してください。

+0

...私は後押し::必要posix_time :: PTIME今=ブースト:: posix_time :: second_clock :: local_time(); – Bluebaron

+0

他のすべてが良いです。 – Bluebaron

+0

はこのファンクションが呼び出されるたびに新しいファセットを作成せず、オーバーレイを引き起こします。 – rve

3

私は手遅れだが、私のようなサーチャーのために知っている:これは、ローカルコンピュータに固有ではありません

#include <boost/format.hpp> 

const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); 

const boost::wformat f = boost::wformat(L"%02d.%02d.%s %02d:%02d") 
       % now.date().year_month_day().dayas_number() 
       % now.date().year_month_day().month.as_number() 
       % now.date().year_month_day().year 
       % now.time_of_day().hours() 
       % now.time_of_day().minutes(); 

const std::wstring result = f.str(); // "21.06.2013 14:38" 
+0

day.as_number()にする必要があります。 6文字の制限のため編集できません。 –

+0

コードをテストし、年が常に正しくフォーマットされているとは限りません。時には、年にカンマ(2,017.01.11-16:18:01)が含まれていることもあります(2017.01.11-16:18:01)、Mac OSでテストされていることがあります。このstd :: to_wstring(now.date()。year_month_day()。年)は役に立ちました。 – MPeli

関連する問題