Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting time in milliseconds using boost::date_time library

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.

like image 440
Ylisar Avatar asked Apr 18 '10 13:04

Ylisar


1 Answers

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

like image 98
Eddy Pronk Avatar answered Nov 15 '22 00:11

Eddy Pronk