Uzyskaj bieżący czas w milisekundach używając C++ i Boost

W moim wątku (używając boost:: thread) muszę pobrać bieżący czas w ms lub mniej i przekonwertować na ms:

Właściwie czytając tutaj znalazłem to:

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

I wydaje się działać, ale po muszę mieć długą wartość milisekund teraz...

Jak mogę to zrobić?
Author: Peter Mortensen, 2011-07-18

4 answers

Możesz użyć boost::posix_time::time_duration, aby uzyskać zakres czasu. E. g like this

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

I aby uzyskać wyższą rozdzielczość, możesz zmienić zegar, którego używasz. Na przykład do boost::posix_time::microsec_clock, choć może to być zależne od systemu operacyjnego. Na przykład w systemie Windows {[4] } mA rozdzielczość milisekundy, a nie mikrosekundy.

Przykład, który jest trochę zależny od sprzętu.

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;
}

Na mojej maszynie win7. Pierwsze Wyjście To 0 LUB 1000. Druga rozdzielczość. Drugi to prawie zawsze 500, ze względu na wyższa rozdzielczość zegara. Mam nadzieję, że to trochę pomoże.

 67
Author: mkaes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-07 16:38:53

Jeśli masz na myśli milisekundy od epoki możesz zrobić

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();
Nie jest jednak jasne, czego szukasz.

Spójrz na przykład w dokumentacji dla DateTime w Boost Date Time

 13
Author: Brian O'Kennedy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-18 14:39:33
// 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
}
 6
Author: Macbeth's Enigma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-12 20:46:09

Spróbuj tak: Importuj nagłówki, jak wspomniano.. daje tylko sekundy i milisekundy. Jeśli chcesz wyjaśnić kod przeczytaj TEN 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);

}
 -8
Author: user1476945,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-29 23:19:04