c ++ 11时钟/ time_points之间的隐式转换

时间:2017-12-24 17:43:50

标签: date c++11 time chrono

是否可以在两个C ++ 11时钟的time_point之间进行隐式/显式转换?

动机: chrono::duration提供了从纪元存储时间间隔的方法,概念上不等于自己拥有纪元的自定义时钟的time_point。 在时钟之间进行隐式转换可以减少使用Howard Hinnant的date library <date/date.h>,它提供了操作和打印time_point系统时钟的方法。

示例:

#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>>;

目标:转换tp以便std::stream date.h转换工作并打印出当前时间,在我的情况下是: 2017-12-24 17:02:56.000000

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

明确演员:这可以降低可读性,支持语言的转换功能并便于访问date.h例程。

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

1 个答案:

答案 0 :(得分:1)

我建议模仿tz.h中找到的date::utc_clockdate::tai_clock的实现。例如,utc_clock实现了两个函数来转换为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>&);

因此,您可以将std::chrono::system_clock视为&#34; hub&#34;。任何实现这些转换的时钟都可以转换为实现这些转换的任何其他时钟,方法是通过隐藏system_clock来实现这些转换。为了促进这种反弹,我们引入了date::clock_cast

此外,utc_time可用作集线器,如果这对您的类型更有效。例如tai_clock实现:

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非常聪明,可以处理这个&#34;双枢纽&#34;系统,因此可以将转换为utc_time的时钟转换为使用sys_time作为其中心的另一个时钟。

如果您还为时钟实施to_stream,则可以直接使用format格式化clock::time_point。并且clock_cast可能对您的to_stream功能的实施很有用。

同样from_stream可以用来将clock::time_point挂钩到date::parse

https://howardhinnant.github.io/date/tz.html搜索&#34; clock_cast&#34;例如使用它。对于您的用例,to_sys / from_sys API似乎最有用。只需这两个函数,您就可以在clock_cast和tz.h中的任何其他时钟(以及满足这些要求的任何其他自定义时钟)之间使用SomeClock

完整演示

#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';
}