【问题标题】:Interoperability between boost::date_time and std::chronoboost::date_time 和 std::chrono 之间的互操作性
【发布时间】:2011-06-22 01:48:30
【问题描述】:

boost::date_time 和 std::chrono 的互操作性如何?

例如,有没有办法在 boost::posix_time::ptime 和 std::chrono::time_point 之间进行转换?

我尝试搜索有关此类转换的文档,但找不到任何文档。

【问题讨论】:

    标签: c++ datetime boost c++11 chrono


    【解决方案1】:

    我在 boost 提交邮件列表中找到了这个:http://lists.boost.org/boost-commit/2009/04/15209.php

    以下是相关功能:

    template < class Clock, class Duration> 
    struct convert_to<posix_time::ptime, chrono::time_point<Clock, Duration> > { 
        inline static posix_time::ptime apply(const chrono::time_point<Clock, Duration>& from) 
        { 
            typedef chrono::time_point<Clock, Duration> time_point_t; 
            typedef chrono::nanoseconds duration_t; 
            typedef duration_t::rep rep_t; 
            rep_t d = chrono::duration_cast<duration_t>(from.time_since_epoch()).count(); 
            rep_t sec = d/1000000000; 
            rep_t nsec = d%1000000000; 
            return boost::posix_time::from_time_t(0)+ 
            boost::posix_time::seconds(static_cast<long>(sec))+ 
            #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS 
            boost::posix_time::nanoseconds(nsec); 
            #else 
            boost::posix_time::microseconds((nsec+500)/1000); 
            #endif 
        } 
    }; 
    
    template < class Clock, class Duration> 
    struct convert_to<chrono::time_point<Clock, Duration>, posix_time::ptime> { 
        inline static chrono::time_point<Clock, Duration> apply(const posix_time::ptime& from) 
        { 
            boost::posix_time::time_duration const time_since_epoch=from-boost::posix_time::from_time_t(0); 
            chrono::time_point<Clock, Duration> t=chrono::system_clock::from_time_t(time_since_epoch.total_seconds()); 
            long nsec=time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()); 
            return t+chrono::nanoseconds(nsec); 
    
        } 
    }; 
    

    我不确定它们何时会成为增强版本的一部分。他们现在似乎不在加速后备箱中......

    【讨论】:

    • 很好的发现。这些函数似乎假设 Clock 的纪元与 ptime 的相同:New Years, 1970。如果该假设为真,则这些函数有效。如果不是,这些函数是静默运行时错误。
    • 即使时代不同,这些功能能否正确实现?
    • 我相信你可以非常接近。我将首先制作一个自定义 chrono::clock (不需要是命名空间 chrono),它的纪元是 1970 年的新年。如上所述在 ptime 和您的新时钟之间进行转换。然后,您可以通过从每个时钟(一个接一个)获取 now() 在两个计时时钟之间进行大致转换。减去每个 time_point 的 time_since_epoch() 以获得持续时间,该持续时间是两个计时时钟的历元之间的差异。您可以添加/减去该差异以在两个计时时间点之间进行转换。
    • 这真的很好,但我认为 nsec+500 应该是 nsec+(nsec>0?500:-500) 才能正确舍入负数。
    【解决方案2】:

    您可以将 time_t 与 std::chrono::system_clock::time_point 相互转换:

    class system_clock
    {
    public:
        ...
        static time_t     to_time_t  (const time_point& __t);
        static time_point from_time_t(time_t __t);
    };
    

    您可以将 time_t 转换为 ptime:

    ptime from_time_t(time_t t);
    

    但是我看不到将 ptime 转换为 time_t 的方法。

    【讨论】:

    • 你可以使用boost的to_tm(ptime)获取tm结构,然后使用mktime(...)从time.h/ctime获取time_t。见cplusplus.com/reference/ctime/mktime
    • 确保您的软件在 2038 年后不会运行 en.wikipedia.org/wiki/Year_2038_problem
    • 为了扩展 @DarienPardinas 的评论,32 位 Linux 平台对 time_t 使用带符号的 32 位整数。这将导致 Y2038 问题。
    • @EmileCormier:这是一个替代日期时间库,与std::chrono::system_clock 具有更好的互操作性:howardhinnant.github.io/date_v2.html 它将带您回溯至 -32768 年,最远至 32767 年。
    猜你喜欢
    • 2014-05-04
    • 2019-08-03
    • 1970-01-01
    • 2017-10-26
    • 2013-12-05
    • 2011-03-16
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多