Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

c++ - Interoperability between boost::date_time and std::chrono

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found this on the boost commits mailing list: http://lists.boost.org/boost-commit/2009/04/15209.php

Here are the relevant functions:

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...