Format ptime object to output string

I am trying to find to simply convert ptime object in boost C ++ to string using predefined format.

Something following this logic.

#include "boost/date_time/posix_time/posix_time.hpp"
ptime timeObj = time_from_string("2002-01-20 23:59:59");
timeObj.format("%m %d");
string result = timeObj.toString();

      

However, the closest I can find is using date_facet / time_facet to format the time and then outputting it to a stream like this example from boost: http://www.boost.org/doc/libs/1_49_0 /doc/html/date_time/date_time_io.html#date_time.date_facet

//example to customize output to be "LongWeekday LongMonthname day, year"
//                                  "%A %b %d, %Y"
date d(2005,Jun,25);
date_facet* facet(new date_facet("%A %B %d, %Y"));
std::cout.imbue(std::locale(std::cout.getloc(), facet));
std::cout << d << std::endl;
// "Saturday June 25, 2005"

      

I would like to know if there is an easier way to do this. Thanks in advance:)

+3


source to share





All Articles