-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cpp
More file actions
72 lines (51 loc) · 1.78 KB
/
date.cpp
File metadata and controls
72 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "date.h"
namespace Date {
namespace {
inline struct tm currTM() {
const auto now = std::chrono::system_clock::now();
const auto now_t = std::chrono::system_clock::to_time_t(now);
struct tm tm{};
localtime_s(&tm, &now_t);
return tm;
}
}
std::wstring CurrTimeWStr() {
const auto tm = currTM();
wchar_t buffer[TIME_FORMAT_LEN()];
wcsftime(buffer, TIME_FORMAT_LEN(), TIME_FORMAT, &tm);
return buffer;
}
std::chrono::hours CurrTimeHour() {
const auto tm = currTM();
return std::chrono::hours(tm.tm_hour);
}
std::chrono::minutes CurrTimeMin() {
const auto tm = currTM();
return std::chrono::minutes(tm.tm_min);
}
std::chrono::seconds CurrTimeSec() {
const auto tm = currTM();
return std::chrono::seconds(tm.tm_sec);
}
#ifdef _DEBUG
// 调试模式下强制锁在2分钟后提醒
std::chrono::seconds NextHourDistance() {
struct tm tm = currTM();
const auto now = std::chrono::system_clock::from_time_t(mktime(&tm));
tm.tm_min += 2;
tm.tm_sec = 0;
const auto next_hour = std::chrono::system_clock::from_time_t(mktime(&tm));
return std::chrono::duration_cast<std::chrono::seconds>(next_hour - now);
}
#endif
// 和下一个整点的距离:秒
std::chrono::seconds NextHourDistance() {
struct tm tm = currTM();
const auto now = std::chrono::system_clock::from_time_t(mktime(&tm));
tm.tm_hour++;
tm.tm_min = 0;
tm.tm_sec = 0;
const auto next_hour = std::chrono::system_clock::from_time_t(mktime(&tm));
return std::chrono::duration_cast<std::chrono::seconds>(next_hour - now);
}
}