71 operator intmax_t()
const {
return ns_; }
74 double ns()
const {
return ns_; }
76 double us()
const {
return ns_ / 1e3; }
78 double ms()
const {
return ns_ / 1e6; }
80 double s()
const {
return ns_ / 1e9; }
82 double min()
const {
return ns_ / 6e10; }
84 double hr()
const {
return ns_ / 36e11; }
93 if (t.
ns_ >= 3600000000000LL)
return os << t.
hr() <<
"hr";
94 if (t.
ns_ >= 60000000000LL)
return os << t.
min() <<
"min";
95 if (t.
ns_ >= 1000000000LL)
return os << t.
s() <<
"s";
96 if (t.
ns_ >= 1000000LL)
return os << t.
ms() <<
"ms";
97 if (t.
ns_ >= 1000LL)
return os << t.
us() <<
"μs";
98 return os << t.
ns() <<
"ns";
106 std::ostringstream os;
129 template <
class Rep,
class Period>
133 std::chrono::duration_cast<std::chrono::nanoseconds>(d)
137 template <
class Rep,
class Period>
141 std::chrono::duration_cast<std::chrono::nanoseconds>(d)
161 return Time(n * 1e3);
170 return Time(n * 1e6);
179 return Time(n * 1e9);
188 return Time(n * 6e10);
197 return Time(n * 3.6e12);
206 std::chrono::duration_cast<std::chrono::nanoseconds>(
207 std::chrono::system_clock::now().time_since_epoch())
216 inline Time operator""_ns(
unsigned long long ns) {
225 inline Time operator""_us(
unsigned long long us) {
226 return Time(us * 1e3);
234 inline Time operator""_ms(
unsigned long long ms) {
235 return Time(ms * 1e6);
243 inline Time operator""_s(
unsigned long long s) {
244 return Time(s * 1e9);
252 inline Time operator""_min(
unsigned long long min) {
261 inline Time operator""_hr(
unsigned long long hr) {
262 return Time(hr * 3.6e12);
270 inline Time operator""_ns(
long double ns) {
279 inline Time operator""_us(
long double us) {
280 return Time(us * 1e3);
288 inline Time operator""_ms(
long double ms) {
289 return Time(ms * 1e6);
297 inline Time operator""_s(
long double s) {
298 return Time(s * 1e9);
315 inline Time operator""_hr(
long double hr) {
316 return Time(hr * 3.6e12);
327 template <
class F,
class... Args>
331 return now() - start;
339 std::this_thread::sleep_for(std::chrono::duration<long double>(tr.
s()));
348 inline std::string
datetime(
const std::string &fmt =
"%Y-%m-%d %H:%M:%S") {
349 std::stringstream ss;
350 auto now = std::chrono::system_clock::now();
351 auto time = std::chrono::system_clock::to_time_t(
now);
354 localtime_s(&tm_buffer, &time);
356 localtime_r(&time, &tm_buffer);
358 ss << std::put_time(&tm_buffer, fmt.c_str());
367 inline double fps(
double target) {
368 using namespace std::chrono;
369 thread_local auto last = steady_clock::now();
370 std::this_thread::sleep_until(last + duration<double>(1.0 / target));
372 = 1.0 / duration<double>(steady_clock::now() - last).count();
373 last = steady_clock::now();
381 auto time = std::mktime(&
tm_);
382 if (time != (std::time_t)-1) {
384 localtime_s(&
tm_, &time);
386 localtime_r(&time, &
tm_);
393 auto now = std::chrono::system_clock::now();
394 std::time_t time = std::chrono::system_clock::to_time_t(
now);
396 localtime_s(&
tm_, &time);
398 localtime_r(&time, &
tm_);
419 const std::string &fmt =
"%Y-%m-%d %H:%M:%S") {
420 std::istringstream ss(
str);
421 ss >> std::get_time(&
tm_, fmt.c_str());
422 if (ss.fail())
throw BadFormat(
"Failed to parse DateTime");
426 const std::wstring &fmt = L
"%Y-%m-%d %H:%M:%S") {
427 std::wistringstream ss(
str);
428 ss >> std::get_time(&
tm_, fmt.c_str());
429 if (ss.fail())
throw BadFormat(
"Failed to parse DateTime");
432 int year()
const {
return tm_.tm_year + 1900; }
448 return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
461 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
462 if (m == 2 &&
is_leap(y))
return 29;
472 return w == 0 ? 7 : w;
533 std::string
str(
const std::string &fmt =
"%Y-%m-%d %H:%M:%S")
const {
534 std::ostringstream ss;
535 ss << std::put_time(&
tm_, fmt.c_str());
545 wstr(
const std::wstring &fmt = L
"%Y-%m-%d %H:%M:%S")
const {
546 std::wostringstream ss;
547 ss << std::put_time(&
tm_, fmt.c_str());
553 return std::mktime(
const_cast<std::tm *
>(&
tm_));
563 std::time_t sec =
static_cast<std::time_t
>(t.
s());
566 localtime_s(&
tm_, &tt);
568 localtime_r(&tt, &
tm_);
586 return Time(
static_cast<long long>(
598 return !(lhs == rhs);
618 return os << dt.
str();
624 friend std::wostream &
626 return os << dt.
wstr();
static bool is_leap(int y)
判断是否为闰年。
Definition time.h:447
DateTime end_of_month() const
获取当月最后一天的 DateTime。
Definition time.h:500
int hour() const
Definition time.h:435
int second() const
Definition time.h:437
bool is_weekend() const
判断是否为周末。
Definition time.h:521
bool is_leap() const
Definition time.h:451
DateTime begin_of_day() const
获取当天 00:00:00 的 DateTime。
Definition time.h:507
std::time_t to_time_t() const
转为 std::time_t。
Definition time.h:552
friend std::ostream & operator<<(std::ostream &os, const DateTime &dt)
输出日期时间到流,格式为 "%Y-%m-%d %H:%M:%S"。
Definition time.h:617
friend bool operator>=(const DateTime &lhs, const DateTime &rhs)
Definition time.h:609
DateTime end_of_day() const
获取当天 23:59:59 的 DateTime。
Definition time.h:514
int iso_weekday() const
获取本周第几天(周一=1, ..., 周日=7)。
Definition time.h:470
std::wstring wstr(const std::wstring &fmt=L"%Y-%m-%d %H:%M:%S") const
格式化为宽字符串。
Definition time.h:545
int minute() const
Definition time.h:436
friend DateTime operator-(DateTime dt, Time t)
Definition time.h:580
int day_of_year() const
获取该年中的第几天(1-based)。
Definition time.h:526
int month() const
Definition time.h:433
DateTime & operator-=(Time t)
减少指定秒数。
Definition time.h:576
void normalize()
Definition time.h:380
int yearday() const
Definition time.h:439
static int days_in_month(int y, int m)
获取指定月份的天数。
Definition time.h:459
friend bool operator<=(const DateTime &lhs, const DateTime &rhs)
Definition time.h:606
DateTime begin_of_month() const
获取当月第一天的 DateTime。
Definition time.h:495
DateTime(const std::string &str, const std::string &fmt="%Y-%m-%d %H:%M:%S")
Definition time.h:418
friend DateTime operator+(DateTime dt, Time t)
Definition time.h:578
friend bool operator>(const DateTime &lhs, const DateTime &rhs)
Definition time.h:603
DateTime(int year, int month, int day, int hour=0, int minute=0, int second=0)
Definition time.h:402
int weekday() const
Definition time.h:438
int year() const
Definition time.h:432
std::tm tm_
Definition time.h:378
int day() const
Definition time.h:434
friend bool operator!=(const DateTime &lhs, const DateTime &rhs)
Definition time.h:597
bool isdst() const
Definition time.h:440
std::string str(const std::string &fmt="%Y-%m-%d %H:%M:%S") const
格式化为字符串。
Definition time.h:533
DateTime()
Definition time.h:392
friend bool operator==(const DateTime &lhs, const DateTime &rhs)
Definition time.h:594
int iso_week() const
获取 ISO 8601 周数。
Definition time.h:479
DateTime & operator+=(Time t)
增加指定秒数。
Definition time.h:562
DateTime(const std::wstring &str, const std::wstring &fmt=L"%Y-%m-%d %H:%M:%S")
Definition time.h:425
friend Time operator-(const DateTime &lhs, const DateTime &rhs)
计算两个 DateTime 的时间差。
Definition time.h:585
friend bool operator<(const DateTime &lhs, const DateTime &rhs)
Definition time.h:600
int days_in_month() const
Definition time.h:465
表示以纳秒为单位的时间量,支持单位转换、算术运算和自动选择合适的输出单位。
Definition time.h:57
std::string str() const
将 Time 对象转换为字符串表示。
Definition time.h:105
intmax_t ns_
纳秒数
Definition time.h:58
friend Time operator+(Time t, std::chrono::duration< Rep, Period > d)
Definition time.h:130
friend bool operator>=(Time t1, Time t2)
Definition time.h:126
Time operator/(double d) const
Definition time.h:116
friend bool operator==(Time t1, Time t2)
Definition time.h:121
friend bool operator<(Time t1, Time t2)
Definition time.h:123
double min() const
返回分钟数。
Definition time.h:82
friend Time operator+(Time t1, Time t2)
Definition time.h:113
friend std::ostream & operator<<(std::ostream &os, const Time t)
输出 Time 对象到流,自动选择合适单位。
Definition time.h:92
Time operator*(double d) const
Definition time.h:115
friend bool operator<=(Time t1, Time t2)
Definition time.h:125
friend bool operator!=(Time t1, Time t2)
Definition time.h:122
intmax_t count() const
获取纳秒数。
Definition time.h:68
friend Time operator-(Time t, std::chrono::duration< Rep, Period > d)
Definition time.h:138
friend bool operator>(Time t1, Time t2)
Definition time.h:124
Time(long long ns=0)
构造一个 Time 对象。
Definition time.h:65
double s() const
返回秒数。
Definition time.h:80
double ms() const
返回毫秒数。
Definition time.h:78
double ns() const
返回纳秒数。
Definition time.h:74
friend Time operator-(Time t1, Time t2)
Definition time.h:114
double us() const
返回微秒数。
Definition time.h:76
double hr() const
返回小时数。
Definition time.h:84
定义 console 库使用的自定义异常类层次结构。
void sleep(const Time &tr)
休眠指定时间。
Definition time.h:338
Time nanoseconds(double n)
静态函数,构造纳秒。
Definition time.h:151
T min(const MultiArray< T, Dims... > &a)
求最小值。
Definition multiarray.h:1676
Time milliseconds(double n)
静态函数,构造毫秒。
Definition time.h:169
Time seconds(double n)
静态函数,构造秒。
Definition time.h:178
Time minutes(double n)
静态函数,构造分钟。
Definition time.h:187
Time hours(double n)
静态函数,构造小时。
Definition time.h:196
std::string datetime(const std::string &fmt="%Y-%m-%d %H:%M:%S")
获取当前日期时间字符串。
Definition time.h:348
Time timer(F &&f, Args &&...args)
测量函数调用的执行时间。
Definition time.h:328
Time now()
获取当前时间点(自纪元以来的纳秒数)。
Definition time.h:204
Time microseconds(double n)
静态函数,构造微秒。
Definition time.h:160
double fps(double target)
控制循环的帧率。
Definition time.h:367