boost::date_time と std::chrono はどの程度相互運用可能ですか?
たとえば、boost::posix_time::ptime と std::chrono::time_point の間で変換する方法はありますか?
そのような変換に関するドキュメントを検索しようとしましたが、見つかりませんでした。
ブースト コミット メーリング リストでこれを見つけました: 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);
}
};
それらがいつブースト リリースの一部になるかはわかりません。彼らは現在、ブースト トランクにいないようです...
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 に変換する方法がわかりません。