如何将小数时期时间戳(双)转换为std :: chrono :: time_point? [英] How to convert a fractional epoch timestamp (double) to an std::chrono::time_point?

查看:1596
本文介绍了如何将小数时期时间戳(双)转换为std :: chrono :: time_point?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小时纪元时间戳,表示为 double ,我想转换为一个适当的 std :: chrono :: time_point 。这个时代是自1970年1月1日以来的通常的UNIX时代。我知道有 std :: chrono :: system_clock :: from_time_t ,但是一个 time_t 没有分数部分。使用C ++ 11做最好的方法是什么?

I have a fractional epoch timestamp, represented as double, that I would like to convert to an appropriate std::chrono::time_point. The epoch is the usual UNIX epoch since 1/1/1970. I know that there exists std::chrono::system_clock::from_time_t, but a time_t does not have a fractional part. What would be the best way to do this with C++11 means?

这个问题与 unix时间戳boost :: posix_time :: ptime ,除了它要求的C ++ 11而不是Boost版本。

This question is related to unix timestamp to boost::posix_time::ptime, except that it's asking for the C++11 rather than Boost version of it.

推荐答案

假设时期与已知的时钟类型相同,用 double 表示,并转换为该时钟使用的持续时间。

Assuming the epoch is the same as a known clock type you can use a duration with a double representation and convert to the duration used by that clock.

// 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 持续时间类型。然后在你的计算中使用它(当然,所有的浮点数学的注意事项)。

This should be ok for any calculation involving that clock, since you're using the same precision as the clock, but it may lose some of the precision in the conversion. If you don't want to lose that you'll have to use a time_point specialization that uses that double-based duration type. And then use that in your calculations (of course, with all the caveats of floating-point math).

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

但是,这会使涉及同一个时钟的任何计算复杂化,因为它需要转换。为了使这个更容易,你可以建立自己的时钟包装器进行转换和使用:

However, this will complicate any calculations involving that same clock, as it will require conversions. To make this easier, you can build your own clock wrapper that does the conversions and use that:

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()
               ));
    }
};

这篇关于如何将小数时期时间戳(双)转换为std :: chrono :: time_point?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆