2011-08-22 59 views
0

目前我有使用这样的东西如何创建自定义boost :: posix_time to_string formaters?

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到字符串formater,将ptime转换为"%a, %d %b %Y %H:%M:%S GMT"格式,并且不会使用ostringstream.str()(因为它们对于我的目的来说太慢了)?

+0

嗨,我有同样的问题,我看你选中此答案正确的,但我似乎无法了解你使用它。你有什么传递给sprintf来获得写入值? –

回答

1

我总是因为使用sprintf而发火,但是有什么不对吗?

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

我不知道的ptime类型的细节,所以我不知道你会如何大,使缓冲区是安全的,或者你会放的论据sprintf的,但毫无疑问,你可以填写细节。

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

相关问题