Boost logo

Boost Users :

Subject: [Boost-users] [date_time] Formating overall lacking in flexibility & functionality?
From: Sebastian Karlsson (sairony_at_[hidden])
Date: 2010-04-21 12:34:14


Sorry for being a bit of a douche, but I find the formatting options
in date_time lacking and would like to see what others think. We have
a use case that I think displays some of the shortcomings.

We have a time duration stored as an int representing the number of
miliseconds. When displaying the time duration we want to use
MM:SS,FFF, padding with 0s in order to get them to line up nicely.
First problem here is it's not possible to control the number of
fractions, we use substr to remove the last 3 unwanted digits, this
however naturally doesn't respect rounding. The second problem is that
it doesn't seem to be possible to control the padding, ( although this
isn't a problem for our use case ).

Another problem is that it doesn't seem to be possible to format a
time_duration directly, the only solution which I've been able to find
is to construct an arbitrary valid date and keep the time part at
zero. This seems both unnecessary and imposes restrictions if one were
interested in actually measuring and format long durations of time.

I also think formatting a date time is often unnecessary verbose. I
would personally appreciate formatting functionality along the lines
of:
// Apply formating_string to duration using supplied locale
std::string format_time_duration( const
boost::posix_time::time_duration& duration, const std::string&
formating_string, const std::locale& locale = std::locale::classic()
);

For reference, this is how we format our times:
std::string format_highscore( unsigned int miliseconds )
{
       boost::posix_time::time_duration td =
boost::posix_time::milliseconds( 10004 );
       boost::posix_time::ptime pt( ( boost::gregorian::date( 2000, 1, 1 ) ) );
       pt += td;
       std::stringstream ss;
       boost::posix_time::time_facet* df( new boost::posix_time::time_facet(
"%M:%S,%f" ) );
       ss.imbue( std::locale( ss.getloc(), df ) );
       ss << pt;
       std::string ret = ss.str();
       ret = ret.substr( 0, ret.size() - 3 );
       return ret;
}


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