Question

Currently I have to use stuff like

ptime t = from_time_t(last_write_time(p));
std::string Created = boost::posix_time::to_iso_extended_string(t) ;

or:

ptime t = from_time_t(last_write_time(p));
std::ostringstream formatter;
formatter.imbue(std::locale(std::cout.getloc(), new boost::posix_time::time_facet("%a, %d %b %Y %H:%M:%S GMT")));
formatter << t;
std::string Created = formatter.str();

first is fast but not compatible with what browsers want as header time format, second is way too slow. So I wonder - how to create fast yet effective ptime to string formatter that would turn ptime into "%a, %d %b %Y %H:%M:%S GMT" format and would not use ostringstream and .str() (because they are too slow for my purpose)?

Was it helpful?

Solution

I always get flamed for using sprintf but anything wrong with this?

char buffer[...];
sprintf(buffer, "%s, %d %s %04d %02d:%02d:%02d GMT", t ...);
std::string Create(buffer);

I don't know the details of the ptime type, so I don't know how big you would have to make the buffer to be safe, or what you would put for the arguments for sprintf, but no doubt you can fill in the details.

There's also the C function strftime which might be of interest to you, http://www.cplusplus.com/reference/clibrary/ctime/strftime/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top