Boost logo

Boost Users :

From: Daniel Lidström (daniel.lidstrom_at_[hidden])
Date: 2006-02-13 08:26:50


Beman Dawes scribbled:

> Daniel Lidström scribbled:
>
> > > Hello!
> > >
> > > Why does boost::progress_timer write two newlines on destruction?
> > > One would suffice for me.
>
> > Another thing about progress_timer: Why is the only option to
> > output seconds? I'd like to be able to use my own formatter, so
> > that I can have the output in HH:MM:SS.mmm for example:
>
> > boost::progress_timer timer(cout, hhmmssmmm_formatter());
>
> Care to submit a patch?

Here is a patch. With it, it is possible to use progress_timer as such:

#include <boost/progress.hpp>
#include <iomanip>

using namespace std;

// Time: S.mmm
struct my_formatter
{
   string operator()(double s) const
   {
      stringstream stream;
      stream
          << "Time: " << fixed << setprecision(3) << s
          << " s\n"
          ;
      return stream.str();
   }
};

// formats as HH:MM:SS.mmm
string form(double s)
{
   stringstream stream;
   int h = static_cast<int>(s/3600);
   int min = static_cast<int>((s-h*3600))/60;
   double sec = s - h*3600 - min*60;
   stream << setfill('0') << setw(2) << h;
   stream << ':';
   stream << setfill('0') << setw(2) << min;
   stream << ':';
   stream << fixed << setprecision(3) << setfill('0') << setw(6) << sec;
   stream << '\n';

   return stream.str();
}

int main()
{
   {
      boost::progress_timer t(cout, my_formatter());
   }
   {
      boost::progress_timer t(cout, form);
   }
}

Hälsningar,
Daniel




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net