views:

158

answers:

1

I have a time duration in milliseconds which I ideally would like to format using the formatting functionality present in the boost::date_time library. However, after creating a boost::posix_time::time_duration I can't seem to find a way to actually apply the formatting string to it.

+2  A: 

You need to add the duration to a time object first, and then output it like this:

boost::posix_time::time_facet* facet = new boost::posix_time::time_facet("%Y%m%d %H:%M:%S.%f");
std::stringstream date_stream;
date_stream.imbue(std::locale(date_stream.getloc(), facet));
date_stream << boost::posix_time::microsec_clock::universal_time();

output:

20100326 12:02:08.024820

Tested with boost 1.41

Eddy Pronk