0

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)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rella
  • 65,003
  • 109
  • 363
  • 636
  • Hey, I'm having the same problem and I see you checked this answer as correct but I can't seem to understand how you used it. what have you pass to the sprintf to get the write values? – Roee Gavirel Feb 23 '12 at 13:45

1 Answers1

1

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/

john
  • 85,011
  • 4
  • 57
  • 81