Interoperability between boost::date_time and std::chrono(boost::date_time 和 std::chrono 之间的互操作性)
问题描述
boost::date_time 和 std::chrono 的互操作性如何?
How interoperable are boost::date_time and std::chrono?
例如,有没有办法在 boost::posix_time::ptime 和 std::chrono::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.
推荐答案
我在 boost 提交邮件列表中找到了这个: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); 
    } 
}; 
我不确定它们何时会成为 Boost 版本的一部分.他们现在似乎不在升压后备箱中...
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::chrono 之间的互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost::date_time 和 std::chrono 之间的互操作性
				
        
 
            
        基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
 - 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - 常量变量在标题中不起作用 2021-01-01
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 - 这个宏可以转换成函数吗? 2022-01-01
 - 我有静态或动态 boost 库吗? 2021-01-01
 - C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				