// // Created by zhaohe on 19-6-2. // #pragma once #include #include #include #include #include #include #include #include #include #include namespace iflytop { namespace core { using namespace std; using namespace chrono; template class T_TimeUtils { public: time_point zero() { return time_point(nanoseconds(0)); } time_point now() { return move(T::now()); } int64_t getus() { return duration_cast(T::now().time_since_epoch()).count(); } int64_t gets() { return duration_cast(T::now().time_since_epoch()).count(); } int64_t getms() { return duration_cast(T::now().time_since_epoch()).count(); } int64_t geth() { return duration_cast(T::now().time_since_epoch()).count(); } int64_t tpToMs(time_point t) { return duration_cast(t.time_since_epoch()).count(); } int64_t tpToUs(time_point t) { return duration_cast(t.time_since_epoch()).count(); } int64_t tpToS(time_point t) { return duration_cast(t.time_since_epoch()).count(); } int64_t dToMs(nanoseconds ns) { return duration_cast(ns).count(); } int64_t dToUs(nanoseconds ns) { return duration_cast(ns).count(); } int64_t dToS(nanoseconds ns) { return duration_cast(ns).count(); } int64_t dToNs(nanoseconds ns) { return ns.count(); } inline time_point msToTp(int64_t ms) { time_point tp = time_point(milliseconds(ms)); return move(tp); } // 时间操作 inline time_point addh(time_point point, int value) { return point + hours(value); } inline time_point adds(time_point point, int value) { return point + seconds(value); } inline time_point addms(time_point point, int value) { return point + milliseconds(value); } inline time_point addus(time_point point, int value) { return point + microseconds(value); } inline time_point addh(int value) { return addh(T::now(), value); } inline time_point adds(int value) { return adds(T::now(), value); } inline time_point addms(int value) { return addms(T::now(), value); } inline time_point addus(int value) { return addus(T::now(), value); } inline int64_t ms2us(int64_t ms) { return ms * 1000; } /** * @brief 计算流逝时间 */ int64_t elapsedTimeS(time_point begin) { return dToS(T::now() - begin); } int64_t elapsedTimeMs(time_point begin) { return dToMs(T::now() - begin); } int64_t elapsedTimeUs(time_point begin) { return dToUs(T::now() - begin); } int64_t elapsedTimeS(int64_t ms) { return (getms() - ms) / 1000; } int64_t inline elapsedTimeMs(int64_t ms) { return (getms() - ms); } int64_t inline elapsedTimeMs(int64_t now, int64_t ms) { return (now - ms); } int64_t inline elapsedTimeUs(int64_t ms) { return (getms() - ms) / 1000 / 1000; } /** * @brief 倒计时 还剩多久 */ int64_t countdownTimeS(time_point endtime) { return dToS(endtime - T::now()); } int64_t countdownTimeMs(time_point endtime) { return dToMs(endtime - T::now()); } int64_t countdownTimeUs(time_point endtime) { return dToUs(endtime - T::now()); } int64_t countdownTimeNs(time_point endtime) { return dToNs(endtime - T::now()); } }; typedef T_TimeUtils tu_sys; // not use in future typedef T_TimeUtils tu_steady; // not use in future typedef time_point tp_sys; // not use in future typedef time_point tp_steady; // not use in future // new api name typedef T_TimeUtils zsystem_clock; typedef T_TimeUtils zsteady_clock; typedef time_point zsystem_tp; typedef time_point zsteady_tp; }; // namespace core } // namespace iflytop