(6)c++ __msvc_chrono.hpp 时间类:system 与 steady_clock() . now() 的区别,函 bool _To_xtime_10_day_clamped ( )

(16) system_clock() . now() 返回的到底是啥玩意,测试一下:

在这里插入图片描述

(17) 测试 steady_clock() . now()

在这里插入图片描述

++ 为了证明本结论的正确性,再测试一次,把主机关掉再开机,再运行此程序

在这里插入图片描述

(18) 函 bool _To_xtime_10_day_clamped ( ) ,这是 STD 空间里的一个全局函数,也很重要,在别的地方有引用,所以单独给出其源码 。本函数是范例,使用 STL 模板库里的时间量进行编程的范例

/*
// Convert duration to xtime, maximum 10 days from now, returns whether clamping occurred.
// If clamped, timeouts will be transformed into spurious non-timeout wakes, 
// due to ABI restrictions where the other side of the DLL boundary overflows int32_t milliseconds.
// Every function calling this one is TRANSITION, ABI

将持续时间转换为xtime,从现在起最多10天,返回是否发生夹紧。
如果卡住,超时将被转换为虚假的非超时响应,这是由于ABI的限制,DLL边界的另一边溢出时间为int32_t毫秒。
调用这个函数的每个函数都是 TRANSITION、ABI

struct xtime { // store time with nanosecond resolution 纳秒级分辨率的存储时间
    __time64_t sec;  // typedef long long  __time64_t
    long nsec;
};
*/
// 存储纳秒级分辨率的时间值  // typedef long long  __time64_t
// struct xtime { __time64_t sec ; long nsec ; };   纳秒 = 10^9 不算太大 
// 本函的形参 2 是相对于当前时间的时间间隔,圆整成 10 天以内,相对于 1970年 的绝对时刻,存入形参 1;
// 若发生了时长截断,形参2 大于 10 天,则返回 true ,否则返回 false。形参一是左值引用。
template <class _Rep, class _Period>  // int 类型最大值  2,147,483,647 约等于 2 * 10 ^ 9 刚好够用
_NODISCARD bool                       // 这一行是函数的返回值
_To_xtime_10_day_clamped(_CSTD xtime& _Xt, const _CHRONO duration<_Rep, _Period>& _Rel_time) 
noexcept( is_arithmetic_v<_Rep> )   
{   
    // using nanoseconds  = duration<long long, nano>; 得到 10 天值的纳秒表示
    constexpr _CHRONO nanoseconds _Ten_days{ _CHRONO hours{24} *10 };
    // template <  class _Rep, class _Period = ratio<1>  >
    // class duration;  将纳秒的 10 天值更换为 秒 级的 10 天值
    constexpr _CHRONO duration<double> _Ten_days_d{ _Ten_days };

    // 获得当前时间的距离 1970 年的时间间隔的纳秒级表示。往更小单位方向的计时转换,才是允许的 !!!!!!
    _CHRONO nanoseconds _Tx0 = _CHRONO system_clock::now().time_since_epoch();
    const bool _Clamped = _Ten_days_d < _Rel_time; // 若形参 2 的时长太大,则为 true ,需要裁剪时长

    if (_Clamped)   _Tx0 += _Ten_days;
    else            _Tx0 += _CHRONO duration_cast<_CHRONO nanoseconds>(_Rel_time);

    // template <class _To, class _Rep, class _Period, enable_if_t<_Is_duration_v<_To>, int> _Enabled>
    // _To  duration_cast ( const duration<_Rep, _Period>&_Dur )  // 时间值的单位转换,truncate 截断
    const auto _Whole_seconds = _CHRONO duration_cast<_CHRONO seconds>(_Tx0); // 以秒为单位

    _Xt.sec  = _Whole_seconds.count();
    _Tx0    -= _Whole_seconds;
    _Xt.nsec = static_cast<long>(_Tx0.count());  // 秒数 与 纳秒数

    return _Clamped;
}

(19)

谢谢

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值