Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <iostream>
35#include <chrono>
36#include <cstdint>
37#include <utility>
38#include <thread>
39#include <sstream>
40#include <ctime>
41#include <iomanip>
42
43namespace console
44{
51 class Time
52 {
53 long double ns_;
54
55 public:
60 Time(long double ns = 0) : ns_(ns) {}
61
63 operator long double() const
64 {
65 return ns_;
66 }
67
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; }
80
87 friend std::ostream &operator<<(std::ostream &os, const Time t)
88 {
89 if (t.ns_ > 3600 * 1e9)
90 return os << t.ns_ / 3600 / 1e9 << "hr";
91 if (t.ns_ > 60 * 1e9)
92 return os << t.ns_ / 60 / 1e9 << "min";
93 if (t.ns_ > 1e9)
94 return os << t.ns_ / 1e9 << "s";
95 if (t.ns_ > 1e6)
96 return os << t.ns_ / 1e6 << "ms";
97 if (t.ns_ > 1e3)
98 return os << t.ns_ / 1e3 << "μs";
99 return os << t.ns_ << "ns";
100 }
101
104 friend Time operator+(Time t1, Time t2) { return t1.ns_ + t2.ns_; }
105 friend Time operator-(Time t1, Time t2) { return t1.ns_ - t2.ns_; }
106 Time operator*(long double d) const { return ns_ * d; }
107 Time operator/(long double d) const { return ns_ / d; }
109
112 friend bool operator==(Time t1, Time t2) { return t1.ns_ == t2.ns_; }
113 friend bool operator!=(Time t1, Time t2) { return t1.ns_ != t2.ns_; }
114 friend bool operator<(Time t1, Time t2) { return t1.ns_ < t2.ns_; }
115 friend bool operator>(Time t1, Time t2) { return t1.ns_ > t2.ns_; }
116 friend bool operator<=(Time t1, Time t2) { return t1.ns_ <= t2.ns_; }
117 friend bool operator>=(Time t1, Time t2) { return t1.ns_ >= t2.ns_; }
119 };
120
126 {
127 return Time((long double)(std::chrono::
128 duration_cast<std::chrono::nanoseconds>(
129 std::chrono::high_resolution_clock::
130 now()
131 .time_since_epoch())
132 .count()));
133 }
134
143 template <class F, class... Args>
144 Time timer(F &&f, Args &&...args)
145 {
146 Time start = now();
147 f(args...);
148 return now() - start;
149 }
150
155 void sleep(const Time &tr)
156 {
157 std::this_thread::sleep_for(std::chrono::duration<long double>(tr.s()));
158 }
159
166 std::string datetime(const std::string &fmt = "%Y-%m-%d %H:%M:%S")
167 {
168 std::stringstream ss;
169 auto now = std::chrono::system_clock::now();
170 auto time = std::chrono::system_clock::to_time_t(now);
171 std::tm tm_buffer;
172#ifdef _WIN32
173 localtime_s(&tm_buffer, &time);
174#else
175 localtime_r(&time, &tm_buffer);
176#endif
177 ss << std::put_time(&tm_buffer, fmt.c_str());
178 return ss.str();
179 }
180
186 double fps(double target)
187 {
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();
193 return actual;
194 }
195}
表示以纳秒为单位的时间量,支持单位转换、算术运算和自动选择合适的输出单位。
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
@ F
Definition kb.h:58
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