0

目前我必须使用类似的东西

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

或者:

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();

首先是快速但与浏览器想要的标头时间格式不兼容,其次是太慢了。所以我想知道 - 如何创建快速而有效的 ptime 到字符串格式化程序,它将 ptime 转换为"%a, %d %b %Y %H:%M:%S GMT"格式并且不会使用ostringstreamand .str()(因为它们对于我的目的来说太慢了)?

4

1 回答 1

1

我总是因为使用 sprintf 而被激怒,但这有什么问题吗?

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

我不知道 ptime 类型的详细信息,所以我不知道你需要多大的缓冲区才能保证安全,或者你会为 sprintf 的参数放什么,但毫无疑问你可以填写细节。

还有strftime你可能感兴趣的 C 函数,http://www.cplusplus.com/reference/clibrary/ctime/strftime/

于 2011-08-22T05:51:22.080 回答