Boost.Chrono custom I/O

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Boost.Chrono custom I/O

Boost - Users mailing list

I'm using boost.chrono in a project and I need some non-standard input-output.

First of all, I need to print days as days. Consider the following code

 namespace testing {
   typedef boost::chrono::duration<long double,boost::ratio<86400>> day;

  main(int argc,char *argv[]) {
     using namespace testing;
     day d(10.5);
     std::cout << "Value: " << d << std::endl;

This prints

Value: 10.5 [86400/1]seconds

Which is obviously right; but I'd rather have:

Value 10.5 days

Obviously, a solution is to define operator<<(ostream&,const & day),
however I'd rather use the facet approach, which looks like easier to
extend to parsing and more flexible (e.g. allowing to print the above
value as "10.5 days" somewhere and as "10.5 d" elsewhere.)

If I understand the documentation, I must define a subclass of
boost::chrono::duration_units and then imbue the facet into the
stream. Is this right? Where can I found some guidelines on how to do

Thanks for the help


Leo Cacciari

Aliae nationes servitutem pati possunt. Populi Romani est propria libertas.
Boost-users mailing list
[hidden email]