的boost :: DATE_TIME和std ::时辰之间的互操作性 [英] Interoperability between boost::date_time and std::chrono

查看:178
本文介绍了的boost :: DATE_TIME和std ::时辰之间的互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何互操作是的boost :: date_time的和std ::时辰?

How interoperable are boost::date_time and std::chrono?

例如,是有办法的boost ::了posix_time ::分组时间和std ::时辰:: time_point之间的转换?

For example, is there a way to convert between boost::posix_time::ptime and std::chrono::time_point?

我试图寻找这样的转换文档,但找不到任何。

I tried searching for documentation on such conversions but couldn't find any.

推荐答案

我发现这对升压提交邮件列表:<一href=\"http://lists.boost.org/boost-commit/2009/04/15209.php\">http://lists.boost.org/boost-commit/2009/04/15209.php

I found this on the boost commits mailing list: 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); 

    } 
}; 

我不知道,当他们将成为提振发布的一部分。他们似乎并不在提升后备箱现在...

I'm not sure when they're going to become part of a boost release. They don't seem to be in boost trunk right now...

这篇关于的boost :: DATE_TIME和std ::时辰之间的互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆