Get current time in milliseconds using C ++ and Boost

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

Actually, here I found this:

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

And it seems to work, but after I need to have a long millisecond value now ...

How can i do this?

+50
c ++ boost timestamp time milliseconds
Jul 18 2018-11-18T00:
source share
4 answers

You can use boost::posix_time::time_duration to get the time range. For example, for example,

 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, for boost::posix_time::microsec_clock , although this is OS dependent. On Windows, for example, boost::posix_time::microsecond_clock has a milisecond resolution, not a microsecond.

An example that is slightly dependent on 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 output is 0 or 1000. The second resolution. The second one is almost always 500, due to the higher resolution of the watch. I hope I help a little.

+65
Jul 18 2018-11-18T00:
source share

If you mean milliseconds from an era , you can 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 is not particularly clear what you are after.

Take a look at the example in the documentation for DateTime at Time Increasing Time

+12
Jul 18 2018-11-18T00:
source share
 // 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
Jul 12 '13 at 20:46
source share

Try the following: importing the headers as follows gives only seconds and milliseconds. 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); } 
-8
Jun 29 2018-12-12T00:
source share



All Articles