【问题标题】:How to create custom boost::posix_time to_string formatters?如何创建自定义 boost::posix_time to_string 格式化程序?
【发布时间】:2011-08-22 04:45:53
【问题描述】:

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

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" 格式并且不会使用 ostringstream.str() (因为它们对于我的目的来说太慢了)?

【问题讨论】:

  • 嘿,我遇到了同样的问题,我看到你检查了这个答案是正确的,但我似乎无法理解你是如何使用它的。您将什么传递给 sprintf 以获取写入值?

标签: c++ boost formatting string-formatting boost-date-time


【解决方案1】:

我总是因为使用 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/

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多