Using date/time to format date strings
Hi all, I'm trying to use the date time library to to do some string formatting but I'm afraid there's something I'm missing. What I'd like to do is feed in a string "05/Aug/2007 11:30" and convert it to an ISO formatted string - could anyone point me in the right direction? I've examined both the documentation and the sample code in io_tutorial.cpp but I'm afraid it hasn't helped - the code below is as far as I've gotten and simply returns the input string unchanged. I'm using boost 1.34.1 with MSVC 2005. Thanks much, Andrew //Begin snippit string formatDate(string inpDate) { stringstream ss; local_date_time checkDate(not_a_date_time); local_time_input_facet* input_facet = new local_time_input_facet(); local_time_facet* output_facet = new local_time_facet(); ss.imbue(locale(locale::classic(), output_facet)); ss.imbue(locale(ss.getloc(), input_facet)); input_facet->format("%d/%b/%Y %H:%M"); output_facet->set_iso_format(); ss << inpDate; return ss.str(); }
I've not used the functionality you mentioned but I don't think that the way you're setting up your stringstream you might have to actually use the output operator to get the desired result. getline(ss, aStringVariable); should do that for you.
ss << inpDate;
return ss.str(); }
Looking at your code sample again, I also wouldn't imbue the stream until after you set the format for the facets. For all you know they're getting copies when you call imbue so the format lines would have no effect on the stream. mike
//Begin snippit string formatDate(string inpDate) { stringstream ss; local_date_time checkDate(not_a_date_time); local_time_input_facet* input_facet = new local_time_input_facet(); local_time_facet* output_facet = new local_time_facet();
ss.imbue(locale(locale::classic(), output_facet)); ss.imbue(locale(ss.getloc(), input_facet));
input_facet->format("%d/%b/%Y %H:%M"); output_facet->set_iso_format();
ss << inpDate;
return ss.str(); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Andrew
-
Michael Linck