Boost logo

Boost Users :

Subject: [Boost-users] Boost.Chrono custom I/O
From: Leo Cacciari (leo.cacciari_at_[hidden])
Date: 2017-11-15 15:51:42


Hi,

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;
  }

  int
  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
that?

Thanks for the help

lc

-- 
Leo Cacciari
Aliae nationes servitutem pati possunt. Populi Romani est propria libertas.

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