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
c ++ boost datetime-format


source to share


No one has answered this question yet

Check out similar questions:

2873
How do I iterate over the words of a string?
1483
Why should I use a pointer and not the object itself?
1455
Easiest way to convert int to string in C ++
724
Given a DateTime object, how do I get an ISO 8601 date in string format?
13
How to convert boost :: ptime to string
6
How to change C ++ posix_time :: ptime acceleration to millisecond?
1
boost :: PO and cannot bind to 'std :: basic_ostream <char> &&
1
Use trait to parse date in boost :: spirit
0
boost :: posix_time. How to represent both microsecond and nanosecond lifetimes in one application
0
increase the percentage format of the locale



All Articles
Loading...
X
Show
Funny
Dev
Pics