如何创建自定义 boost::posix_time to_string 格式化程序?

发布于 2024-11-30 14:46:57 字数 649 浏览 0 评论 0原文

目前我必须使用类似

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-12-07 14:46:57

我总是因为使用 sprintf 而受到批评,但这有什么问题吗?

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

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

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文