(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)
谢谢
c++ __msvc_chrono.hpp 时间类:system 与 steady_clock . now 的区别,函 bool _To_xtime_10_day_clamped&spm=1001.2101.3001.5002&articleId=144144730&d=1&t=3&u=3df13d1605214745b67df6718146f5a3)
2523

被折叠的 条评论
为什么被折叠?



