Boost logo

Boost Users :

Subject: Re: [Boost-users] [Chrono] Is there any functions that convert Chrono's time_point and duration to their Ptime equivalents?
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-01-19 16:51:21


Le 19/01/12 19:49, Matthew Jenks a écrit :
> Hello,
>
> I have been trying to find in the boost libraries if there are any
> interoperability functions for Boost::Chrono and Boost::Posix_Time?
> Does anyone know if they exist and what class they are in?
>
>
Hi,

no, there aren't.

You can get them from
https://svn.boost.org/svn/boost/sandbox/conversion/boost/conversion/boost/chrono_time_point_to_posix_time_ptime.hpp
and
https://svn.boost.org/svn/boost/sandbox/conversion/boost/conversion/boost/chrono_duration_to_posix_time_duration.hpp.

Of course, you should extract the code from to be independent of the
proposed library TBoost.Conversion reviewed but without review results yet.

     template< typename Rep, typename Period>

       posix_time::time_duration operator()(chrono::duration<Rep, Period> const& from)
       {
         typedef chrono::duration<Rep, Period> src_duration_t;
         typedef chrono::nanoseconds duration_t;
         typedef duration_t::rep rep_t;
         rep_t d = chrono::duration_cast<duration_t>(from).count();
         rep_t sec = d/1000000000;
         rep_t nsec = d%1000000000;
         return posix_time::seconds(static_cast<long long>(sec))+
             #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
                 posix_time::nanoseconds(nsec);
             #else
                 posix_time::microseconds((nsec+500)/1000);
             #endif
       }

     template< typename Duration>
       posix_time::ptime operator()(const chrono::time_point<chrono::system_clock, Duration>& from)
       {
         typedef chrono::time_point<chrono::system_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 posix_time::from_time_t(0)+
                 posix_time::seconds(static_cast<long>(sec))+
#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
                 posix_time::nanoseconds(nsec);
#else
                 posix_time::microseconds((nsec+500)/1000);
#endif
       }

HTH,
Vicente



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net