1

Is there any elegant way of converting the boost chrono time_point to the standard library equivalent?

cerkiewny
  • 2,761
  • 18
  • 36
  • Both `std::system_clock` and `boost::system_clock` have `to_time_t` and `from_time_t` static functions. However, this does not seem to be the case of `steady_clock`. Maybe, those `time_point`s can be converted somehow. – Daniel Langr Jun 20 '18 at 12:15
  • 1
    I don't get why it is being downvoted? – cerkiewny Jun 20 '18 at 12:16
  • Agree, hate downvoting without commenting / flagging. – Daniel Langr Jun 20 '18 at 12:18
  • I am also asking clear question, if people think this is obviously a "no can't do because types are from different libraries". If it is so obvious or stupid quesiton please explain why. – cerkiewny Jun 20 '18 at 12:19
  • @DanielLangr the problem is that time_t is less accurate than the time_point heh, looks like I will need to introduce the api dependancy of boost chrono than. – cerkiewny Jun 20 '18 at 12:20
  • Both `time_point`s are class templates. What are the template arguments in the case you are asking? – Tadeusz Kopec for Ukraine Jun 20 '18 at 12:34
  • @TadeuszKopec the boost::chrono::time_point is, but boost::chrono::steady_clock::time_point isn't; it is time point specialization with boost::chrono::steady_clock as a clock typedef for std it is the same – cerkiewny Jun 20 '18 at 12:51

1 Answers1

1

You can't guarantee a conversion, I am afraid, unless you accept a priori that boost::chrono::steady_clock and std::chrono::steady_clock have the same epoch. If you can accept this, then something like the following could work:

#include <boost/chrono.hpp>
#include <chrono>

/* Convert from boost::ratio to std::ratio */
template <typename T> struct conv_ratio;
template <std::intmax_t N, std::intmax_t D>
struct conv_ratio<boost::ratio<N, D>> {
    using type = std::ratio<N, D>;
};
template <typename T>
using conv_ratio_t = typename conv_ratio<T>::type;

/* Convert from boost::duration to std::duration */
template <typename T> struct conv_duration;
template <typename Rep, typename Period>
struct conv_duration<boost::chrono::duration<Rep, Period>> {
    using type = std::chrono::duration<Rep, conv_ratio_t<Period>>;
};
template <typename T>
using conv_duration_t = typename conv_duration<T>::type;

/* Convert from A::time_point to B::time_point.  This assumes that A
 * and B are clocks with the same epoch. */
template <typename A, typename B>
typename B::time_point convert_timepoint_same_clock(
    typename A::time_point const & tp)
{
    return typename B::time_point(
        conv_duration_t<typename A::time_point::duration>(
            tp.time_since_epoch().count()));
}


int main()
{
    auto now_boost = boost::chrono::steady_clock::now();
    auto now_std = convert_timepoint_same_clock<
        boost::chrono::steady_clock, std::chrono::steady_clock>(now_boost);
}
md5i
  • 3,018
  • 1
  • 18
  • 32
  • You could also normalise them against time.now() on both clocks to see if there is the difference between the epochs, thanks. – cerkiewny Jun 20 '18 at 19:49