Steven Varga
Steven Varga

Reputation: 671

Implicit conversion between c++11 clocks/time_points

Is it possible to do implicit/explicit conversion between time_points of two C++11 clocks?

Motivation: chrono::durations provide means of storing time intervals from epoch, conceptually is not equal to a time_point of a custom clock that has an epoch on its own. Having an implicit conversion between clocks eases up the use Howard Hinnant's date library <date/date.h> which provides means to manipulate and print out time_points of system clocks.

Example:

#include <date/date.h>
using namespace date;
namespace ch = std::chrono;
// 
#define EPOCH_OFFSET 100
template<class Duration> using PosixTimePoint = 
                   ch::time_point<ch::system_clock, Duration>;
typedef PosixTimePoint<ch::duration<long,std::micro>> PosixTimePointType;

struct SomeClock{
    typedef ch::duration<long,std::micro>   duration;
    typedef ch::time_point<SomeClock>  time_point;

    ...
    static time_point now() noexcept {
        using namespace std::chrono;
        return time_point (
            duration_cast<duration>( 
                system_clock::now().time_since_epoch()) + date::years(EPOCH_OFFSET) );
    }
    static PosixTimePoint<duration> to_posix( const time_point& tp  ){...}

}
auto tp = SomeClock::now(); //<time_point<SomeClock,ch::duration<long,std::micro>>;

Objective: to convert tp so the std::stream conversions of date.h works and prints out the current time, which in my case is: 2017-12-24 17:02:56.000000

// std::cout << tp; compile error
std::cout << SomeClock::to_posix( tp ); // OK

Explicit cast: this could ease up readability, support conversion feature of the language and facilitate access to date.h routines.

long time_value = static_cast<long>( tp );
auto st = static_cast<PosixTimePointType>( tp ); 
std::cout << static_cast<PosixTimePointType>( tp );

Upvotes: 5

Views: 865

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 219420

I recommend mimicking the implementations of either date::utc_clock or date::tai_clock found in tz.h. For example utc_clock implements two functions to convert to and from sys_time:

template<typename Duration>
static
std::chrono::time_point<std::chrono::system_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys(const std::chrono::time_point<utc_clock, Duration>&);

template<typename Duration>
static
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
from_sys(const std::chrono::time_point<std::chrono::system_clock, Duration>&);

So you can think of std::chrono::system_clock as a "hub". Any clock that implements these conversions can convert to any other clock which implements these conversions by bouncing off of system_clock under the covers. And to facilitate that bounce, date::clock_cast is introduced.

Additionally, utc_time can be used as the hub, if that is more efficient for your type. For example tai_clock implements:

template<typename Duration>
static
std::chrono::time_point<utc_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
to_utc(const std::chrono::time_point<tai_clock, Duration>&) NOEXCEPT;

template<typename Duration>
static
std::chrono::time_point<tai_clock, typename std::common_type<Duration, std::chrono::seconds>::type>
from_utc(const std::chrono::time_point<utc_clock, Duration>&) NOEXCEPT;

clock_cast is smart enough to deal with this "dual-hub" system, so one can convert a clock that converts to/from utc_time, to another clock that uses sys_time as its hub.

If you also implement to_stream for your clock, then you can directly use format to format your clock::time_point. And clock_cast is likely to be useful in the implementation of your to_stream function.

Also from_stream can be used to hook your clock::time_point up to date::parse.

Search https://howardhinnant.github.io/date/tz.html for "clock_cast" for example uses of it. For your use case the to_sys/from_sys API appears to be the most useful. Just these two functions will allow you to use clock_cast between your SomeClock and any other clock in tz.h (and any other custom clock that meets these requirements).


Full Demo

#include "date/tz.h"
#include <iostream>
#include <sstream>

struct SomeClock
{
    using duration = std::chrono::microseconds;
    using rep = duration::rep;
    using period = duration::period;
    using time_point = std::chrono::time_point<SomeClock>;
    static constexpr bool is_steady = false;

    static time_point now() noexcept
    {
        return from_sys(date::floor<duration>(std::chrono::system_clock::now()));
    }

    static constexpr auto offset = date::sys_days{} - date::sys_days{date::year{1870}/1/1};

    template<typename Duration>
    static
    date::sys_time<Duration>
    to_sys(const std::chrono::time_point<SomeClock, Duration>& t)
    {
        return date::sys_time<Duration>{(t - offset).time_since_epoch()};
    }

    template<typename Duration>
    static
    std::chrono::time_point<SomeClock, Duration>
    from_sys(const date::sys_time<Duration>& t)
    {
        return std::chrono::time_point<SomeClock, Duration>{(t + offset).time_since_epoch()};
    }
};

template <class Duration>
using SomeTime = std::chrono::time_point<SomeClock, Duration>;

constexpr date::days SomeClock::offset;

template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
          const SomeTime<Duration>& t)
{
    return date::to_stream(os, fmt, date::clock_cast<std::chrono::system_clock>(t));
}

template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const SomeTime<Duration>& t)
{
    const CharT fmt[] = {'%', 'F', ' ', '%', 'T', CharT{}};
    return to_stream(os, fmt, t);
}

template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
            SomeTime<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
            std::chrono::minutes* offset = nullptr)
{
    using namespace date;
    sys_time<Duration> st;
    date::from_stream(is, fmt, st, abbrev, offset);
    if (!is.fail())
        tp = clock_cast<SomeClock>(st);
    return is;
}

int
main()
{
    std::cout << SomeClock::now() << '\n';
    std::cout << date::format("%a, %b %d, %Y\n", SomeClock::now());
    std::istringstream in{"2017-12-24 19:52:30"};
    SomeClock::time_point t;
    in >> date::parse("%F %T", t);
    std::cout << t << '\n';
}

Upvotes: 4

Related Questions