如何将分数历元时间戳(双)转换为std::chrono::time_point

How to convert a fractional epoch timestamp (double) to an std::chrono::time_point?

本文关键字:std 转换 chrono point time 时间戳      更新时间:2023-10-16

我有一个分数历元时间戳,表示为double,我想将其转换为适当的std::chrono::time_point。epoch是自1970年1月1日以来常见的UNIX epoch。我知道存在std::chrono::system_clock::from_time_t,但time_t没有分数部分。用C++11实现这一点的最佳方法是什么?

这个问题与到boost::posix_time::ptime的unix时间戳有关,只是它要求的是C++11而不是boost版本。

假设epoch与已知的clock类型相同,则可以使用具有double表示的持续时间,并将其转换为该时钟使用的持续时间。

// change period to appropriate units - I'm assuming seconds
typedef std::chrono::duration<double, std::ratio<1>> d_seconds;
d_seconds since_epoch_full(324324.342);
auto since_epoch = std::chrono::duration_cast<clock::duration>(since_epoch_full);
clock::time_point point(since_epoch);

这对于任何涉及该时钟的计算都应该是可以的,因为您使用的精度与时钟相同,但在转换过程中可能会失去一些精度。如果你不想失去这一点,你就必须使用time_point专门化,它使用基于double的持续时间类型。然后在计算中使用它(当然,还有浮点数学的所有注意事项)。

typedef std::chrono::time_point<clock, d_seconds> d_time_point;

然而,这将使涉及同一时钟的任何计算复杂化,因为这将需要转换。为了让这更容易,你可以构建自己的时钟包装器来进行转换并使用它:

template <typename Clock>
struct my_clock_with_doubles {
    typedef double rep;
    typedef std::ratio<1> period;
    typedef std::chrono::duration<rep, period> duration;
    typedef std::chrono::time_point<my_clock_with_doubles<Clock>> time_point;
    static const bool is_steady = Clock::is_steady;
    static time_point now() noexcept {
        return time_point(std::chrono::duration_cast<duration>(
                   Clock::now().time_since_epoch()
               ));
    }
    static time_t to_time_t(const time_point& t) noexcept {
        return Clock::to_time_t(typename Clock::time_point(
                             std::chrono::duration_cast<typename Clock::duration>(
                                 t.time_since_epoch()
                             )
                        ));
    }
    static time_point from_time_t(time_t t) noexcept {
        return time_point(std::chrono::duration_cast<duration>(
                   Clock::from_time_t(t).time_since_epoch()
               ));
    }
};