You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
3.9 KiB
83 lines
3.9 KiB
//
|
|
// Created by zhaohe on 19-6-2.
|
|
//
|
|
|
|
#pragma once
|
|
#include <chrono>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <list>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace iflytop {
|
|
namespace core {
|
|
using namespace std;
|
|
using namespace chrono;
|
|
|
|
template <class T>
|
|
class T_TimeUtils {
|
|
public:
|
|
time_point<T> zero() { return time_point<T>(nanoseconds(0)); }
|
|
time_point<T> now() { return move(T::now()); }
|
|
int64_t getus() { return duration_cast<microseconds>(T::now().time_since_epoch()).count(); }
|
|
int64_t gets() { return duration_cast<seconds>(T::now().time_since_epoch()).count(); }
|
|
int64_t getms() { return duration_cast<milliseconds>(T::now().time_since_epoch()).count(); }
|
|
int64_t geth() { return duration_cast<hours>(T::now().time_since_epoch()).count(); }
|
|
int64_t tpToMs(time_point<T> t) { return duration_cast<milliseconds>(t.time_since_epoch()).count(); }
|
|
int64_t tpToUs(time_point<T> t) { return duration_cast<microseconds>(t.time_since_epoch()).count(); }
|
|
int64_t tpToS(time_point<T> t) { return duration_cast<seconds>(t.time_since_epoch()).count(); }
|
|
int64_t dToMs(nanoseconds ns) { return duration_cast<milliseconds>(ns).count(); }
|
|
int64_t dToUs(nanoseconds ns) { return duration_cast<microseconds>(ns).count(); }
|
|
int64_t dToS(nanoseconds ns) { return duration_cast<seconds>(ns).count(); }
|
|
int64_t dToNs(nanoseconds ns) { return ns.count(); }
|
|
inline time_point<T> msToTp(int64_t ms) {
|
|
time_point<T> tp = time_point<T>(milliseconds(ms));
|
|
return move(tp);
|
|
}
|
|
// 时间操作
|
|
inline time_point<T> addh(time_point<T> point, int value) { return point + hours(value); }
|
|
inline time_point<T> adds(time_point<T> point, int value) { return point + seconds(value); }
|
|
inline time_point<T> addms(time_point<T> point, int value) { return point + milliseconds(value); }
|
|
inline time_point<T> addus(time_point<T> point, int value) { return point + microseconds(value); }
|
|
inline time_point<T> addh(int value) { return addh(T::now(), value); }
|
|
inline time_point<T> adds(int value) { return adds(T::now(), value); }
|
|
inline time_point<T> addms(int value) { return addms(T::now(), value); }
|
|
inline time_point<T> addus(int value) { return addus(T::now(), value); }
|
|
inline int64_t ms2us(int64_t ms) { return ms * 1000; }
|
|
/**
|
|
* @brief 计算流逝时间
|
|
*/
|
|
int64_t elapsedTimeS(time_point<T> begin) { return dToS(T::now() - begin); }
|
|
int64_t elapsedTimeMs(time_point<T> begin) { return dToMs(T::now() - begin); }
|
|
int64_t elapsedTimeUs(time_point<T> 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<T> endtime) { return dToS(endtime - T::now()); }
|
|
int64_t countdownTimeMs(time_point<T> endtime) { return dToMs(endtime - T::now()); }
|
|
int64_t countdownTimeUs(time_point<T> endtime) { return dToUs(endtime - T::now()); }
|
|
int64_t countdownTimeNs(time_point<T> endtime) { return dToNs(endtime - T::now()); }
|
|
};
|
|
|
|
typedef T_TimeUtils<system_clock> tu_sys; // not use in future
|
|
typedef T_TimeUtils<steady_clock> tu_steady; // not use in future
|
|
typedef time_point<system_clock> tp_sys; // not use in future
|
|
typedef time_point<steady_clock> tp_steady; // not use in future
|
|
|
|
// new api name
|
|
typedef T_TimeUtils<system_clock> zsystem_clock;
|
|
typedef T_TimeUtils<steady_clock> zsteady_clock;
|
|
|
|
typedef time_point<system_clock> zsystem_tp;
|
|
typedef time_point<steady_clock> zsteady_tp;
|
|
}; // namespace core
|
|
} // namespace iflytop
|