【问题标题】:boost posix_time: stringstream input failsboost posix_time:字符串流输入失败
【发布时间】:2015-10-09 00:44:33
【问题描述】:

知道为什么返回非日期时间,而不是时间字符串吗?我在 Ubuntu 15.04 上使用 boost 1.55.0.2

#include <iostream>
#include "boost/date_time/posix_time/posix_time.hpp"

using namespace boost::posix_time;

int main(int argc, char **argv) {

    ptime t2;
    std::stringstream ss("2004-Jan-1 05:21:33.20");
    ss >> t2;

    std::cout<<t2<< std::endl;

    return 0;
}

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    你想要的“简单字符串”格式是YYYY-mmm-DD HH:MM:SS.fffffffff

    请参阅posix time docs

    日期是两位数,因此请尝试使用std::stringstream ss("2004-Jan-01 05:21:33.20");

    Live on coliru!

    【讨论】:

    • 正确,谢谢。我直接从 boost 文档中提取了那个字符串,所以没有费心去检查。 boost.org/doc/libs/1_55_0/doc/html/date_time/examples/…
    • 某处是否有可接受格式的列表? posix 时间文档不清楚。例如,对于这些示例,看起来 YYYY-MM-DD HH:MM:SS.fffffffff 也可以接受,但它也不适用于我。
    • 我觉得to_string这三种转换格式都可以接受,这么简单,iso,iso_extended。因此YYYY-MM-DD HH:MM:SS.fffffffff 是不可接受的,但YYYY-MM-DDTHH:MM:SS,fffffffff 可以。
    • 我的意思是根据示例,日期时间分隔符T 字面意思是“T”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多