Get current time in milliseconds using C++ and Boost

C++BoostTimeTimestampMilliseconds

C++ Problem Overview


In my thread (using boost::thread) I need to retrieve the current time in ms or less and to convert into ms:

Actually, reading here I've found this:

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

And seems to work, but after I need to have a long value of the milliseconds of the now...

How can I do it?

C++ Solutions


Solution 1 - C++

You can use boost::posix_time::time_duration to get the time range. E.g like this

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

And to get a higher resolution you can change the clock you are using. For example to the boost::posix_time::microsec_clock, though this can be OS dependent. On Windows, for example, boost::posix_time::microsecond_clock has milisecond resolution, not microsecond.

An example which is a little dependent on the hardware.

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

On my win7 machine. The first out is either 0 or 1000. Second resolution. The second one is nearly always 500, because of the higher resolution of the clock. I hope that help a little.

Solution 2 - C++

If you mean milliseconds since epoch you could do

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();

However, it's not particularly clear what you're after.

Have a look at the example in the documentation for DateTime at Boost Date Time

Solution 3 - C++

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

Solution 4 - C++

Try this: import headers as mentioned.. gives seconds and milliseconds only. If you need to explain the code read 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);

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionghibozView Question on Stackoverflow
Solution 1 - C++mkaesView Answer on Stackoverflow
Solution 2 - C++Brian O'KennedyView Answer on Stackoverflow
Solution 3 - C++Macbeth's EnigmaView Answer on Stackoverflow
Solution 4 - C++user1476945View Answer on Stackoverflow