如何创建自定义 boost::posix_time to_string 格式化程序?
目前我必须使用类似
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();
第一个速度快,但与浏览器想要的标头时间格式不兼容,第二个太慢。所以我想知道 - 如何创建快速而有效的 ptime 到字符串格式化程序,将 ptime 转换为 "%a, %d %b %Y %H:%M:%S GMT"
格式并且不会使用 ostringstream
和 .str()
(因为它们对于我的目的来说太慢了)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我总是因为使用 sprintf 而受到批评,但这有什么问题吗?
我不知道 ptime 类型的详细信息,所以我不知道你必须使缓冲区有多大才能安全,或者你会为 sprintf 的参数添加什么,但毫无疑问你可以填写细节。
还有您可能感兴趣的 C 函数
strftime
,http://www.cplusplus.com/reference/clibrary/ctime/strftime/I always get flamed for using sprintf but anything wrong with this?
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/