【问题标题】:Boost: How to print/convert posix_time::ptime in milliseconds from Epoch?Boost:如何从 Epoch 以毫秒为单位打印/转换 posix_time::ptime?
【发布时间】:2011-08-02 05:18:31
【问题描述】:

我无法将posix_time::ptime 转换为time_tposix_time::milliseconds 表示的时间戳,或任何其他可以轻松打印的适当类型(来自Epoch)。

我实际上只需要以毫秒为单位打印posix_time::ptime 表示的时间戳,所以如果有一种简单的方法可以以这种格式打印,我实际上不需要转换。

【问题讨论】:

    标签: boost time posix epoch milliseconds


    【解决方案1】:

    此代码将打印自 1941-12-07T00:00:00 以来的毫秒数。显然,您可以选择适合您需要的任何时代。

    void print_ptime_in_ms_from_epoch(const boost::posix_time::ptime& pt)
      {
        using boost::posix_time::ptime;
        using namespace boost::gregorian;
        std::cout << (pt-ptime(date(1941, Dec, 7))).total_milliseconds() << "\n";
      }
    

    【讨论】:

    • 这里值得注意的是,在 pt 中找到的时区偏移信息会影响结果。例如,如果 pt 是用 microsec_clock::universal_time() 抓取的,你会得到与 microsec_clock::local_time() 不同的答案。
    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多