2011-07-18 42 views
50

ブースト私はms以下に現在の時刻を取得し、ミリ秒に変換する必要があります。(ブーストを使用して::スレッド)C++を使用して、ミリ秒単位で現在の時刻を取得し、私のスレッドで

実は、ここに読んIましたこの発見:

tick = boost::posix_time::second_clock::local_time(); 
now = boost::posix_time::second_clock::local_time(); 

をそして動作しているようですが、私はそれを行うことができますどのようになりました...

のミリ秒単位の長い値を持っている必要があります後?

+1

あなたはエポックタイムスタンプを意味するのですか? – Nim

+6

"しかし、私は現在のミリ秒の長い値を持つ必要がある後。 ....何? –

答えて

66

boost::posix_time::time_durationを使用すると、時間範囲を取得できます。例:

boost::posix_time::time_duration diff = tick - now; 
diff.total_milliseconds(); 

さらに高解像度を得るには、使用している時計を変更することができます。たとえば、boost::posix_time::microsec_clockにすると、これはOSに依存する可能性があります。たとえば、Windowsの場合、boost::posix_time::microsecond_clockにはマイクロ秒ではなく、ミリ秒の解像度があります。

ハードウェアに少し依存する例です。

int main(int argc, char* argv[]) 
{ 
    boost::posix_time::ptime t1 = boost::posix_time::second_clock::local_time(); 
    boost::this_thread::sleep(boost::posix_time::millisec(500)); 
    boost::posix_time::ptime t2 = boost::posix_time::second_clock::local_time(); 
    boost::posix_time::time_duration diff = t2 - t1; 
    std::cout << diff.total_milliseconds() << std::endl; 

    boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time(); 
    boost::this_thread::sleep(boost::posix_time::millisec(500)); 
    boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time(); 
    boost::posix_time::time_duration msdiff = mst2 - mst1; 
    std::cout << msdiff.total_milliseconds() << std::endl; 
    return 0; 
} 

私のwin7マシンです。最初の出力は0または1000です。2番目の解像度。 2番目のクロックは、クロックの分解能が高いため、ほぼ常に500です。少し助けてくれることを願っています。

+3

ありがとうございますが、例えば、今すぐミリ秒で送信する必要がある場合、どうすればいいですか? – ghiboz

+1

私はあなたがいくつかの 'using'ステートメントでできると思います。 –

+2

@hiboz:[boost :: chrono :: high_resolution_clock](http://www.boost.org/doc/libs/1_47_0/doc/html/chrono/reference。 html#chrono.reference.cpp0x.system_clocks_hpp.high_resolution_clock) –

12

あなたは

ptime time_t_epoch(date(1970,1,1)); 
ptime now = microsec_clock::local_time(); 
time_duration diff = now - time_t_epoch; 
x = diff.total_milliseconds(); 

を行うことができエポックからのミリ秒を意味している場合しかし、それはあなたが後にしているものを特に明確ではありません。 ..述べたように、インポートヘッダのみ秒とミリ秒を与える:

はこれを試してみてくださいBoost Date Time

+5

+1また、ここでは日付がboost :: gregorian :: date型であることに注意してください。 – ntg

-8

でのDateTimeのドキュメントの例を見てください。あなたがコードを説明する必要がある場合this linkを読んでください。

#include <windows.h> 

#include <stdio.h> 

void main() 
{ 

    SYSTEMTIME st; 
    SYSTEMTIME lt; 

    GetSystemTime(&st); 
    // GetLocalTime(&lt); 

    printf("The system time is: %02d:%03d\n", st.wSecond, st.wMilliseconds); 
    // printf("The local time is: %02d:%03d\n", lt.wSecond, lt.wMilliseconds); 

} 
+4

これはWindowsベースのソリューションです。クロスプラットフォームソリューションを提供する方が良いでしょう –

+4

これはブーストとは関係ありません。 – Qix

6
// Get current date/time in milliseconds. 
#include "boost/date_time/posix_time/posix_time.hpp" 
namespace pt = boost::posix_time; 

int main() 
{ 
    pt::ptime current_date_microseconds = pt::microsec_clock::local_time(); 

    long milliseconds = current_date_microseconds.time_of_day().total_milliseconds(); 

    pt::time_duration current_time_milliseconds = pt::milliseconds(milliseconds); 

    pt::ptime current_date_milliseconds(current_date_microseconds.date(), 
             current_time_milliseconds); 

    std::cout << "Microseconds: " << current_date_microseconds 
       << " Milliseconds: " << current_date_milliseconds << std::endl; 

    // Microseconds: 2013-Jul-12 13:37:51.699548 Milliseconds: 2013-Jul-12 13:37:51.699000 
} 
関連する問題