63 operator long double()
const
69 long double ns()
const {
return ns_; }
71 long double us()
const {
return ns_ / 1e3; }
73 long double ms()
const {
return ns_ / 1e6; }
75 long double s()
const {
return ns_ / 1e9; }
77 long double min()
const {
return ns_ / 60 / 1e9; }
79 long double hr()
const {
return ns_ / 3600 / 1e9; }
89 if (t.ns_ > 3600 * 1e9)
90 return os << t.ns_ / 3600 / 1e9 <<
"hr";
92 return os << t.ns_ / 60 / 1e9 <<
"min";
94 return os << t.ns_ / 1e9 <<
"s";
96 return os << t.ns_ / 1e6 <<
"ms";
98 return os << t.ns_ / 1e3 <<
"μs";
99 return os << t.ns_ <<
"ns";
127 return Time((
long double)(std::chrono::
128 duration_cast<std::chrono::nanoseconds>(
129 std::chrono::high_resolution_clock::
143 template <
class F,
class... Args>
148 return now() - start;
157 std::this_thread::sleep_for(std::chrono::duration<long double>(tr.
s()));
166 std::string
datetime(
const std::string &fmt =
"%Y-%m-%d %H:%M:%S")
168 std::stringstream ss;
169 auto now = std::chrono::system_clock::now();
170 auto time = std::chrono::system_clock::to_time_t(
now);
173 localtime_s(&tm_buffer, &time);
175 localtime_r(&time, &tm_buffer);
177 ss << std::put_time(&tm_buffer, fmt.c_str());
188 using namespace std::chrono;
189 thread_local auto last = steady_clock::now();
190 std::this_thread::sleep_until(last + duration<double>(1.0 / target));
191 double actual = 1.0 / duration<double>(steady_clock::now() - last).count();
192 last = steady_clock::now();
表示以纳秒为单位的时间量,支持单位转换、算术运算和自动选择合适的输出单位。
Definition time.h:52
long double s() const
返回秒数。
Definition time.h:75
long double us() const
返回微秒数(1 微秒 = 1000 纳秒)。
Definition time.h:71
friend bool operator>=(Time t1, Time t2)
Definition time.h:117
Time(long double ns=0)
构造一个 Time 对象。
Definition time.h:60
friend bool operator==(Time t1, Time t2)
Definition time.h:112
friend bool operator<(Time t1, Time t2)
Definition time.h:114
friend Time operator+(Time t1, Time t2)
Definition time.h:104
long double hr() const
返回小时数。
Definition time.h:79
friend std::ostream & operator<<(std::ostream &os, const Time t)
输出 Time 对象到流,自动选择合适单位。
Definition time.h:87
friend bool operator<=(Time t1, Time t2)
Definition time.h:116
friend bool operator!=(Time t1, Time t2)
Definition time.h:113
long double min() const
返回分钟数。
Definition time.h:77
friend bool operator>(Time t1, Time t2)
Definition time.h:115
long double ms() const
返回毫秒数。
Definition time.h:73
long double ns() const
返回纳秒数。
Definition time.h:69
Time operator/(long double d) const
Definition time.h:107
friend Time operator-(Time t1, Time t2)
Definition time.h:105
Time operator*(long double d) const
Definition time.h:106
void sleep(const Time &tr)
休眠指定时间。
Definition time.h:155
std::string datetime(const std::string &fmt="%Y-%m-%d %H:%M:%S")
获取当前日期时间字符串。
Definition time.h:166
Time timer(F &&f, Args &&...args)
测量函数调用的执行时间。
Definition time.h:144
Time now()
获取当前时间点(自纪元以来的纳秒数)。
Definition time.h:125
double fps(double target)
控制循环的帧率。
Definition time.h:186