Search code examples
boostboost-date-time

Why boost::time_duration doesn't implement multiplication by a real number?


In the context of a huge computing, I estimate the total time of the computation time during the computation. To do that, I compare my time to the starting time to obtain a duration, and I divide it by the already computed ratio. But I'm surprised : no operator * (boost::time_duration, double) exists !

See this example of what I expected to write:

boost::posix_time::ptime t0 = boost::posix_time::microsec_clock::local_time();
for(unsigned long int i = 0; i < num_iterations; ++i)
{
  //
  // An iteration of my algorithm
  //

  boost::posix_time::ptime t = boost::posix_time::microsec_clock::local_time();
  boost::posix_time::time_duration d = t - t0;
  boost::posix_time::time_duration expected_d = d * ((double)n / (double)i);
}

The only operator available is operator * (boost::time_duration, int). My question is why? Is there good reason to not implementent multiplication by a fractional number? Is it a bad practice to implement it myself in the boost namespace?


Solution

  • You could implement it in the boost namespace, but that is far from best practice.

    I think the boost developers had a good reason not to include floating point division and multiplication to posix_time::time_duration. This probably has to do with the internal representation of posix times. Why don't you use the usual way to do integer division?

    boost::posix_time::time_duration d = t - t0;
    boost::posix_time::time_duration expected_d = (n * d) / i;