I’m trying to come up with an answer to two questions that didn’t seem hard at first.
Q1 : How do I obtain the number of elapsed seconds between UTC.Now() and a given date?
A1 : Just like in the code below!
Q2 : How do I determine how many fractional seconds have elapsed since the last “full” second ? I’d like to print the “total_elapsed_seconds.fractional_seconds” -> “1234124.45”. How do I do that?
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;
}
}
You are using the
second_clockto get the current time. As the name implies, it is accurate only to the nearest second. Since your reference time has no fractional seconds the duration fractional seconds always ends up being 0. Use themicrosec_clockinstead:Also, in such a tight loop, I wouldn’t expect the clock to update on every iteration, so you may also want to add a sleep from boost::thread: