boost :: date_time和std :: chrono的互操作性如何?
例如,有没有办法在boost :: posix_time :: ptime和std :: chrono :: time_point之间进行转换?
我尝试搜索有关此类转换的文档,但找不到任何文档。
答案 0 :(得分:13)
我在boost commits邮件列表中找到了这个: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);
}
};
我不确定他们什么时候会成为助推器的一部分。他们现在似乎没有加速行李......
答案 1 :(得分:12)
您可以将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的方法。