-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkstd_time.hpp
235 lines (203 loc) · 6.76 KB
/
dkstd_time.hpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// author: Khiêm Đoàn Hoà (KhiemDH)
// github: https://github.com/khiemdoan/dkstd
// created: 2017-10-14
// modified: 2019-08-22
#pragma once
#include "ctime"
#include "string"
#include "iomanip"
#include "sstream"
#include "thread"
#ifdef _WIN32
# include "windows.h"
#endif
namespace dkstd
{
namespace time
{
class timedelta
{
public:
timedelta(short days = 0, short hours = 0, short minutes = 0, short seconds = 0);
short days = 0, hours = 0, minutes = 0, seconds = 0;
};
std::time_t get_time() noexcept;
std::tm get_localtime() noexcept;
std::tm get_localtime(std::time_t time) noexcept;
std::string get_time_as_string(std::string format = "%Y-%m-%dT%H:%M:%S");
std::wstring get_time_as_string(std::wstring format = L"%Y-%m-%dT%H:%M:%S");
std::string get_time_as_filename(std::string name = "", std::string ext = "");
std::wstring get_time_as_filename(std::wstring name = L"", std::wstring ext = L"");
std::string time_to_string(std::time_t time, std::string format = "%Y-%m-%dT%H:%M:%S");
std::wstring time_to_string(std::time_t time, std::wstring format = L"%Y-%m-%dT%H:%M:%S");
std::time_t string_to_time(std::string str, std::string format = "%Y-%m-%dT%H:%M:%S");
std::time_t string_to_time(std::wstring str, std::wstring format = L"%Y-%m-%dT%H:%M:%S");
void sleep_for_milliseconds(unsigned long milliseconds);
void sleep_for_seconds(unsigned long seconds);
void sleep_for_minutes(unsigned long minutes);
void sleep_for_hours(unsigned long hours);
std::time_t add_time(std::time_t time, timedelta delta) noexcept;
#ifdef _WIN32
std::time_t filetime_to_timet(const FILETIME& ft) noexcept;
#endif
}
}
// KhiemDH - 2019-07-05
inline dkstd::time::timedelta::timedelta(short days, short hours, short minutes, short seconds)
{
this->days = days;
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
// Get current calendar time
// KhiemDH - 2017-10-14
inline std::time_t dkstd::time::get_time() noexcept
{
return std::time(nullptr);;
}
// Get current calendar time as std::tm type
// KhiemDH - 2017-10-14
inline std::tm dkstd::time::get_localtime() noexcept
{
const std::time_t time = dkstd::time::get_time();
return dkstd::time::get_localtime(time);
}
// Convert std::time_t to std::tm
// KhiemDH - 2019-08-22
inline std::tm dkstd::time::get_localtime(std::time_t time) noexcept
{
std::tm tm = { 0 };
tm.tm_mday = 1;
#ifdef _WIN32
if (localtime_s(&tm, &time) != 0)
#else
if (localtime_r(&time, &tm) == nullptr)
#endif
{
tm = { 0 };
tm.tm_mday = 1;
}
return tm;
}
// Use custom format to get string time
// KhiemDH - 2020-05-11
inline std::string dkstd::time::get_time_as_string(std::string format) {
const std::time_t time = dkstd::time::get_time();
return dkstd::time::time_to_string(time, format);
}
// Use custom format to get string time
// KhiemDH - 2017-10-14
inline std::wstring dkstd::time::get_time_as_string(std::wstring format)
{
const std::time_t time = dkstd::time::get_time();
return dkstd::time::time_to_string(time, format);
}
// Get string to name a file with time
// KhiemDH - 2020-05-11
inline std::string dkstd::time::get_time_as_filename(std::string prefix, std::string ext) {
std::string name = dkstd::time::get_time_as_string("%Y_%m_%d_%H_%M_%S");
if (prefix.length())
name = prefix + "_" + name;
if (ext.length())
name = name + "." + ext;
return name;
}
// Get string to name a file with time
// KhiemDH - 2019-08-05
inline std::wstring dkstd::time::get_time_as_filename(std::wstring prefix, std::wstring ext)
{
std::wstring name = dkstd::time::get_time_as_string(L"%Y_%m_%d_%H_%M_%S");
if (prefix.length())
name = prefix + L"_" + name;
if (ext.length())
name = name + L"." + ext;
return name;
}
// format time to string
// KhiemDH - 2020-05-11
inline std::string dkstd::time::time_to_string(std::time_t time, std::string format) {
std::tm tm = get_localtime(time);
std::stringstream ss;
ss << std::put_time(&tm, format.c_str());
return ss.str();
}
// format time to string
// KhiemDH - 2019-08-03
inline std::wstring dkstd::time::time_to_string(std::time_t time, std::wstring format)
{
std::tm tm = get_localtime(time);
std::wstringstream ss;
ss << std::put_time(&tm, format.c_str());
return ss.str();
}
// convert std::string to std::time_t
// return -1 if fail
// KhiemDH - 2020-05-11
inline std::time_t dkstd::time::string_to_time(std::string str, std::string format) {
std::stringstream ss;
std::tm tm_buf = { 0 };
ss << str;
ss >> std::get_time(&tm_buf, format.c_str());
return std::mktime(&tm_buf);
}
// convert std::string to std::time_t
// return -1 if fail
// KhiemDH - 2017-10-16
inline std::time_t dkstd::time::string_to_time(std::wstring str, std::wstring format)
{
std::wstringstream ss;
std::tm tm_buf = { 0 };
ss << str;
ss >> std::get_time(&tm_buf, format.c_str());
return std::mktime(&tm_buf);
}
// sleep thread in milliseconds
// KhiemDH - 2017-10-14
inline void dkstd::time::sleep_for_milliseconds(unsigned long milliseconds)
{
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
// sleep thread in seconds
// KhiemDH - 2017-10-14
inline void dkstd::time::sleep_for_seconds(unsigned long seconds)
{
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
// sleep thread in minutes
// KhiemDH - 2017-10-14
inline void dkstd::time::sleep_for_minutes(unsigned long minutes)
{
std::this_thread::sleep_for(std::chrono::minutes(minutes));
}
// sleep thread in hours
// KhiemDH - 2019-06-27
inline void dkstd::time::sleep_for_hours(unsigned long hours)
{
std::this_thread::sleep_for(std::chrono::hours(hours));
}
// KhiemDH - 2019-07-05
// Add or subtract time
// std::time_t now = dkstd::time::get_time();
// dkstd::time::timedelta delta = dkstd::time::timedelta(-1);
// std::time_t yesterday = dkstd::time::add_time(now, delta);
inline std::time_t dkstd::time::add_time(std::time_t time, timedelta delta) noexcept
{
std::time_t delta_in_number = (std::time_t)delta.days * 24 * 60 * 60;
delta_in_number += (std::time_t)delta.hours * 60 * 60;
delta_in_number += (std::time_t)delta.minutes * 60;
delta_in_number += delta.seconds;
return time + delta_in_number;
}
#ifdef _WIN32
// convert FILETIME type to time_t type
// use for Windows only
// KhiemDH - 2017-10-14
inline std::time_t dkstd::time::filetime_to_timet(const FILETIME & ft) noexcept
{
ULARGE_INTEGER ull = { 0 };
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
return ull.QuadPart / 10000000ULL - 11644473600ULL;
}
#endif