【问题标题】:how to convert this date:2011-02-10T07:18:04+0000 to unix timestamp in Qt如何将此日期:2011-02-10T07:18:04+0000 转换为 Qt 中的 unix 时间戳
【发布时间】:2011-02-10 07:40:32
【问题描述】:

我正在使用 Facebook Graph Api,并且在我将其从 2011-02-10T06%3A52%3A02%2B0000 解码到 2011-02- 后,它在分页参数中返回此字符串10T07:18:04+0000
现在我喜欢将解码字符串转换为 Qt 或纯 c++ 中的 unix 时间戳

【问题讨论】:

    标签: c++ qt timestamp facebook-graph-api


    【解决方案1】:

    您可以使用QDate::fromString() 方法从格式化字符串创建QDate

    编辑:更正使用QDateTime 代替,因为它提供了toTime_t() 方法来获取时间戳:

    QDateTime dt = QDateTime::fromString(formatted, formatString);
    long timestamp = dt.toTime_t();
    

    我会让你弄清楚formatString 应该是什么样子。

    【讨论】:

    • 加法:如果百分比编码实际上是字符串的一部分,而不是字符串的工件:通过 QByteArray::fromPercentEncoding() 将其传递以摆脱它。
    • 这是我为获得真实日期人类可读格式所做的工作
    • 我怎样才能得到这个格式化的 QDate 并将其转换为时间戳?
    【解决方案2】:

    我相信标准函数strptime 应该足够了。 (但是,根据下面的评论,它的可移植性不如基于 Qt 的解决方案)

    【讨论】:

    • strptime() 在 Windows 下不存在,因此不符合 Qt 的跨平台卓越精神。
    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2020-12-23
    • 2016-04-24
    相关资源
    最近更新 更多