forked from liulilittle/aggligator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.cpp
226 lines (198 loc) · 6.49 KB
/
DateTime.cpp
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
#include "DateTime.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
namespace aggligator {
static inline DateTime DateTimeToUnixOrLocalTime(DateTime& dateTime, bool to_local_or_unix_time) noexcept {
DateTime local = DateTime::Now();
DateTime utc = DateTime::UtcNow();
int64_t ticks = local.Ticks() - utc.Ticks();
if (ticks < 0) {
ticks = -ticks;
}
if (to_local_or_unix_time) {
return dateTime.AddTicks(+ticks);
}
else {
return dateTime.AddTicks(-ticks);
}
}
DateTime DateTime::ToUtc() noexcept {
return DateTimeToUnixOrLocalTime(*this, false);
}
DateTime DateTime::ToLocal() noexcept {
return DateTimeToUnixOrLocalTime(*this, true);
}
static DateTime PosixTimeToDateTime(boost::posix_time::ptime now) noexcept {
boost::posix_time::time_duration d = now - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1));
return DateTime(1970, 1, 1).AddSeconds(d.total_seconds());
}
DateTime DateTime::Now() noexcept {
return PosixTimeToDateTime(boost::posix_time::second_clock::local_time());
}
DateTime DateTime::UtcNow() noexcept {
return PosixTimeToDateTime(boost::posix_time::second_clock::universal_time());
}
int DateTime::GetGMTOffset(bool abs) noexcept {
auto gmtOffset = []() noexcept {
boost::posix_time::ptime localTime = boost::posix_time::second_clock::local_time();
boost::posix_time::time_duration gmtOffset = localTime - boost::posix_time::second_clock::universal_time();
return gmtOffset.total_seconds();
};
if (abs) {
return gmtOffset();
}
static const int offset = gmtOffset();
return offset;
}
bool DateTime::TryParse(const char* s, int len, DateTime& out) noexcept {
out = MinValue();
if (s == NULL && len != 0) {
return false;
}
if (s != NULL && len == 0) {
return false;
}
if (len < 0) {
len = (int)strlen(s);
}
if (len < 1) {
return false;
}
static const int max_segments_length = 7;
std::string segments[max_segments_length + 1];
const char* p = s;
unsigned int length = 0;
while (p < (s + len) && *p != '\x0') {
char ch = *p;
if (ch >= '0' && ch <= '9') {
char buf[2] = { ch, '\x0' };
segments[length] += buf;
}
else {
if (!segments[length].empty()) {
length++;
if (length >= max_segments_length) {
break;
}
else {
segments[length].clear();
}
}
}
p++;
}
struct {
int y;
int M;
int d;
int H;
int m;
int s;
int f;
} tm;
if (0 == length) {
return false;
}
else {
int* t = (int*)&tm;
for (unsigned int i = 1; i <= max_segments_length; i++) {
if (i > (length + 1)) {
t[i - 1] = 0;
}
else {
std::string& sx = segments[i - 1];
if (sx.empty()) {
t[i - 1] = 0;
}
else {
t[i - 1] = atoi(sx.c_str());
}
}
}
out = DateTime(tm.y, tm.M, tm.d, tm.H, tm.m, tm.s, tm.f);
}
return length > 0;
}
std::string DateTime::ToString(const char* format, bool fixed) noexcept {
std::string result;
if (NULL == format || *format == '\x0') {
return result;
}
char symbol = 0;
int symbol_size = 0;
auto symbol_exec = [&](int ch) noexcept {
std::string seg;
switch (symbol) {
case 'y':
seg = std::to_string(Year());
break;
case 'M':
seg = std::to_string(Month());
break;
case 'd':
seg = std::to_string(Day());
break;
case 'H':
seg = std::to_string(Hour());
break;
case 'm':
seg = std::to_string(Minute());
break;
case 's':
seg = std::to_string(Second());
break;
case 'f':
seg = std::to_string(Millisecond());
break;
case 'u':
seg = std::to_string(Microseconds());
break;
case 'T':
seg = std::to_string((int64_t)TotalHours());
break;
};
int64_t seg_size = seg.size();
if (fixed && seg_size > symbol_size) {
seg = seg.substr(seg_size - symbol_size);
}
elif(seg_size < symbol_size) {
seg = PaddingLeft(seg, symbol_size, '0');
}
if (ch != 0) {
seg.append(1, ch);
}
result += seg;
symbol = 0;
symbol_size = 0;
};
const char* p = format;
for (;;) {
char ch = *p++;
if (ch != 0) { /* yMdHmsfuT */
bool fb =
ch == 'y' ||
ch == 'M' ||
ch == 'd' ||
ch == 'H' ||
ch == 'm' ||
ch == 's' ||
ch == 'f' ||
ch == 'u' ||
ch == 'T';
if (fb) {
if (symbol != 0 && symbol != ch) {
symbol_exec(ch);
}
symbol = ch;
symbol_size++;
continue;
}
}
symbol_exec(ch);
if (ch == 0) {
break;
}
}
return result;
}
}