C ++ :: Boost :: posix_time (elapsed seconds, elapsed fractional seconds)

I am trying to answer two questions that at first did not seem difficult. Q1: How to get the number of elapsed seconds between UTC.Now () and a given date? A1: Like in the code below! Q2: How to determine how many fractional seconds have passed since the last "full" second? I would like to print "total_elapsed_seconds.fractional_seconds" β†’ "1234124.45". How can I do it? A2:

#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> using namespace std; using namespace boost::gregorian; using namespace boost::posix_time; void main() { ptime Jan1st1970(date(1970, 1, 1)); for(int i = 0; i < 10; i++) { ptime Now = second_clock::universal_time(); time_duration diff = Now - Jan1st1970; cout << Now << " : " << diff.total_seconds() << "." << diff.fractional_seconds() << endl; } } 
+4
source share
4 answers

You use second_clock to get the current time. As the name implies, this is accurate only to the nearest second. Since your control time does not have fractional seconds, the fractional seconds always end with 0. Instead, use microsec_clock :

 ptime Now = microsec_clock::universal_time(); 

Also, in such a closed loop, I would not expect the clock to be updated at each iteration, so you can also add sleep from boost :: thread:

 boost::this_thread::sleep(boost::posix_time::milliseconds(25)); 
+8
source

You do not mention which operating system you are using, but I know that the clock in the window does not give you better resolution than about 15 milliseconds (unless you really play some games). However, there is a so-called performance timer in the windows, which can give you a resolution of the nanosecond level. It really is just a counter of how many times the processor cycles (you can divide by processor frequency to get the time), so to use it as a clock, you have to add this time at a known time:

 ptime Start = microsec_clock::universal_time(); initMyClockToZero(); // You have to write this to use the performance timer .... do something .... int64 microseconds = getMyClockMicrosec(); // this too ptime Now = Start + posix_time::microseconds(microseconds); 

I also have a stopwatch style timer that I wrote myself using Windows calls.

 #ifndef STOPWATCH_HPP #define STOPWATCH_HPP #include <iostream> #include <windows.h> //! \brief Stopwatch for timing performance values //! //! This stopwatch class is designed for timing performance of various //! software operations. If the values you get back a greater than a //! few seconds, you should be using a different tool. //! On a Core 2 Duo E6850 @ 3.00GHz, the start/stop sequence takes //! approximately 230 nano seconds in the debug configuration and 180 //! nano seconds in the release configuration. If you are timing on the //! sub-microsecond scale, please take this into account and test it on //! your machine. class Stopwatch{ public: //! \param start if set to true will initialize and then start the //! timer. Stopwatch(bool start=false){ _start.QuadPart = 0; _stop.QuadPart = 0; if(start) Start(); } //! Starts the stopwatch running void Start(){ QueryPerformanceCounter(&_start); } //! Run this when the event being timed is complete void Stop(){ QueryPerformanceCounter(&_stop); } //! Stops the timer and returns the result double StopResult(){ Stop(); return ResultNanoseconds(); } //! You can get the result of the stopwatch start-stop sequence at //! your leisure. double ResultNanoseconds(){ LARGE_INTEGER frequency; QueryPerformanceFrequency(&frequency); double cyclesPerNanosecond = static_cast<double>(frequency.QuadPart) / 1000000000.0; LARGE_INTEGER elapsed; elapsed.QuadPart = _stop.QuadPart - _start.QuadPart; return elapsed.QuadPart / cyclesPerNanosecond; } void PrintResultNanoseconds(){ std::cout << ResultNanoseconds() << "nanosec" << std::endl; } void PrintResultMicroseconds(){ std::cout << ResultNanoseconds()/100 << "microsec" << std::endl; } void PrintResultMilliseconds(){ std::cout << ResultNanoseconds()/100000 << "millisec" << std::endl; } void PrintResultSeconds(){ std::cout << ResultNanoseconds()/1000000000 << "sec" << std::endl; } private: LARGE_INTEGER _start; LARGE_INTEGER _stop; }; #endif STOPWATCH_HPP 
+2
source

It does sound reasonable, however, the conclusion gives me:

 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 2009-May-28 20:14:32 : 1243541672.0 Press any key to continue . . . 

In truth, I expected more numbers - I hope to get a result in the range of 10 ^ (-6) [sec].

+1
source

Add this to your example to get the β€œcurrent” fractional seconds:

 cout << Now.time_of_day().fractional_seconds() << endl; 

And change the clock from second_clock to microsec_clock to get non-zero fractional seconds.

 ptime Now = microsec_clock::universal_time(); 
0
source

All Articles