-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimestamp_parsing.cpp
More file actions
539 lines (454 loc) · 15.3 KB
/
Copy pathtimestamp_parsing.cpp
File metadata and controls
539 lines (454 loc) · 15.3 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include "timestamp_parsing.hpp"
#include <algorithm>
#include <cctype>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "date/date.h"
namespace PJ::CSV {
std::string Trim(const std::string& str) {
size_t start = str.find_first_not_of(" \t\r\n");
if (start == std::string::npos) {
return {};
}
size_t end = str.find_last_not_of(" \t\r\n");
return str.substr(start, end - start + 1);
}
std::optional<double> toDouble(const std::string& str) {
if (str.empty()) {
return std::nullopt;
}
// Handle European comma decimal separator by replacing with dot.
// Only replace if there's exactly one comma and no dots (to avoid
// misinterpreting thousand separators).
std::string normalized = str;
auto comma_count = std::count(normalized.begin(), normalized.end(), ',');
auto dot_count = std::count(normalized.begin(), normalized.end(), '.');
if (comma_count == 1 && dot_count == 0) {
std::replace(normalized.begin(), normalized.end(), ',', '.');
}
const char* start = normalized.c_str();
char* end = nullptr;
errno = 0;
double value = std::strtod(start, &end);
if (errno != 0 || end == start || *end != '\0') {
return std::nullopt;
}
return value;
}
static const char* const UNAMBIGUOUS_FORMATS[] = {
"%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S%z", "%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M:%S%z",
"%Y-%m-%d", "%Y/%m/%d %H:%M:%S",
};
static constexpr size_t NUM_UNAMBIGUOUS_FORMATS = sizeof(UNAMBIGUOUS_FORMATS) / sizeof(UNAMBIGUOUS_FORMATS[0]);
static constexpr int64_t EPOCH_FIRST = 1400000000;
static constexpr int64_t EPOCH_LAST = 2000000000;
static std::pair<std::string, std::chrono::nanoseconds> ExtractFractionalSeconds(const std::string& input) {
size_t dot_pos = input.rfind('.');
size_t colon_pos = input.rfind(':');
if (dot_pos == std::string::npos || colon_pos == std::string::npos || dot_pos <= colon_pos) {
return {input, std::chrono::nanoseconds{0}};
}
size_t frac_start = dot_pos + 1;
size_t frac_end = frac_start;
while (frac_end < input.size() && std::isdigit(static_cast<unsigned char>(input[frac_end]))) {
frac_end++;
}
if (frac_end <= frac_start) {
return {input, std::chrono::nanoseconds{0}};
}
std::string frac_str = input.substr(frac_start, frac_end - frac_start);
if (frac_str.size() < 9) {
frac_str.append(size_t{9} - frac_str.size(), '0');
} else if (frac_str.size() > 9) {
frac_str = frac_str.substr(0, 9);
}
std::chrono::nanoseconds fractional_ns{0};
try {
fractional_ns = std::chrono::nanoseconds{std::stoll(frac_str)};
} catch (...) {}
std::string base_str = input.substr(0, dot_pos) + input.substr(frac_end);
return {base_str, fractional_ns};
}
static std::optional<double> TryParseFormat(
const std::string& base_str, const char* fmt, std::chrono::nanoseconds fractional_ns) {
std::istringstream in{base_str};
in.imbue(std::locale::classic());
date::sys_time<std::chrono::seconds> tp;
date::from_stream(in, fmt, tp);
if (!in.fail()) {
auto duration = tp.time_since_epoch();
auto total_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration) + fractional_ns;
return std::chrono::duration<double>(total_ns).count();
}
return std::nullopt;
}
struct NumericInfo {
bool is_number = false;
bool has_decimal = false;
bool has_exponent = false;
};
static NumericInfo CheckNumeric(const std::string& str) {
NumericInfo info;
info.is_number = true;
bool has_digit = false;
for (size_t i = 0; i < str.size(); i++) {
const char c = str[i];
if (c == 'e' || c == 'E') {
if (info.has_exponent) {
info.is_number = false;
break;
}
info.has_exponent = true;
continue;
}
if (c == '-' || c == '+') {
if (i == 0 || str[i - 1] == 'e' || str[i - 1] == 'E') {
continue;
}
info.is_number = false;
break;
}
if (c == '.' || c == ',') {
if (info.has_decimal || info.has_exponent) {
info.is_number = false;
break;
}
info.has_decimal = true;
continue;
}
if (std::isdigit(static_cast<unsigned char>(c))) {
has_digit = true;
} else {
info.is_number = false;
break;
}
}
if (info.is_number) {
const char last = str.back();
if (!has_digit || last == 'e' || last == 'E' || last == '+' || last == '-') {
info.is_number = false;
}
}
return info;
}
static ColumnType DetectEpochType(int64_t ts) {
if (ts > EPOCH_FIRST * 1000000000LL && ts < EPOCH_LAST * 1000000000LL) {
return ColumnType::EPOCH_NANOS;
}
if (ts > EPOCH_FIRST * 1000000LL && ts < EPOCH_LAST * 1000000LL) {
return ColumnType::EPOCH_MICROS;
}
if (ts > EPOCH_FIRST * 1000LL && ts < EPOCH_LAST * 1000LL) {
return ColumnType::EPOCH_MILLIS;
}
if (ts > EPOCH_FIRST && ts < EPOCH_LAST) {
return ColumnType::EPOCH_SECONDS;
}
return ColumnType::NUMBER;
}
static double EpochToSeconds(int64_t ts, ColumnType type) {
switch (type) {
case ColumnType::EPOCH_NANOS:
return static_cast<double>(ts) * 1e-9;
case ColumnType::EPOCH_MICROS:
return static_cast<double>(ts) * 1e-6;
case ColumnType::EPOCH_MILLIS:
return static_cast<double>(ts) * 1e-3;
case ColumnType::EPOCH_SECONDS:
default:
return static_cast<double>(ts);
}
}
bool IsDayFirstFormat(const std::string& str, char separator) {
int first = 0;
int second = 0;
size_t pos = 0;
while (pos < str.size() && std::isdigit(static_cast<unsigned char>(str[pos]))) {
first = first * 10 + (str[pos] - '0');
pos++;
}
if (pos < str.size() && str[pos] == separator) {
pos++;
}
while (pos < str.size() && std::isdigit(static_cast<unsigned char>(str[pos]))) {
second = second * 10 + (str[pos] - '0');
pos++;
}
if (first > 12 && first <= 31) {
return true;
}
if (second > 12 && second <= 31) {
return false;
}
return true;
}
std::optional<double> AutoParseTimestamp(const std::string& str) {
std::string trimmed = Trim(str);
if (trimmed.empty()) {
return std::nullopt;
}
auto num_info = CheckNumeric(trimmed);
if (num_info.is_number) {
try {
if (!num_info.has_decimal && !num_info.has_exponent) {
int64_t ts = std::stoll(trimmed);
ColumnType epoch_type = DetectEpochType(ts);
return EpochToSeconds(ts, epoch_type);
}
return toDouble(trimmed);
} catch (...) {}
}
auto [base_str, fractional_ns] = ExtractFractionalSeconds(trimmed);
for (size_t i = 0; i < NUM_UNAMBIGUOUS_FORMATS; i++) {
if (auto result = TryParseFormat(base_str, UNAMBIGUOUS_FORMATS[i], fractional_ns)) {
return result;
}
}
if (base_str.find('/') != std::string::npos && !base_str.empty() && base_str[0] != '2') {
const char* fmt = IsDayFirstFormat(base_str, '/') ? "%d/%m/%Y %H:%M:%S" : "%m/%d/%Y %H:%M:%S";
if (auto result = TryParseFormat(base_str, fmt, fractional_ns)) {
return result;
}
}
if (base_str.find('-') != std::string::npos && !base_str.empty() && base_str[0] != '2') {
if (IsDayFirstFormat(base_str, '-')) {
if (auto result = TryParseFormat(base_str, "%d-%m-%Y %H:%M:%S", fractional_ns)) {
return result;
}
}
}
return std::nullopt;
}
std::optional<double> FormatParseTimestamp(const std::string& str, const std::string& format) {
std::string trimmed = Trim(str);
if (trimmed.empty()) {
return std::nullopt;
}
std::string adjusted_format = format;
std::chrono::nanoseconds fractional_ns{0};
std::string adjusted_input = trimmed;
size_t zzz_pos = format.find(".zzz");
if (zzz_pos != std::string::npos) {
size_t z_count = 0;
size_t z_start = zzz_pos + 1;
while (z_start + z_count < format.size() && format[z_start + z_count] == 'z') {
z_count++;
}
size_t input_dot_pos = adjusted_input.find('.', zzz_pos > 0 ? zzz_pos - 5 : 0);
if (input_dot_pos != std::string::npos) {
auto [base, frac] = ExtractFractionalSeconds(adjusted_input);
fractional_ns = frac;
adjusted_input = base;
adjusted_format = format.substr(0, zzz_pos) + format.substr(z_start + z_count);
}
}
std::string strptime_format = adjusted_format;
auto replace_all = [](std::string& s, const std::string& from, const std::string& to) {
size_t pos = 0;
while ((pos = s.find(from, pos)) != std::string::npos) {
s.replace(pos, from.length(), to);
pos += to.length();
}
};
replace_all(strptime_format, "yyyy", "%Y");
replace_all(strptime_format, "yy", "%y");
replace_all(strptime_format, "MM", "%m");
replace_all(strptime_format, "dd", "%d");
replace_all(strptime_format, "hh", "%H");
replace_all(strptime_format, "HH", "%H");
replace_all(strptime_format, "mm", "%M");
replace_all(strptime_format, "ss", "%S");
return TryParseFormat(adjusted_input, strptime_format.c_str(), fractional_ns);
}
ColumnTypeInfo DetectColumnType(const std::string& str) {
ColumnTypeInfo info;
std::string trimmed = Trim(str);
if (trimmed.empty()) {
info.type = ColumnType::STRING;
return info;
}
if (trimmed.size() > 2 && trimmed[0] == '0' && (trimmed[1] == 'x' || trimmed[1] == 'X')) {
info.type = ColumnType::HEX;
return info;
}
auto num_info = CheckNumeric(trimmed);
if (num_info.is_number) {
if (num_info.has_decimal) {
info.type = ColumnType::NUMBER;
return info;
}
try {
int64_t ts = std::stoll(trimmed);
info.type = DetectEpochType(ts);
return info;
} catch (...) {}
info.type = ColumnType::NUMBER;
return info;
}
auto [base_str, fractional_ns] = ExtractFractionalSeconds(trimmed);
info.has_fractional = (fractional_ns.count() > 0 || trimmed != base_str);
auto try_format = [&](const char* fmt) -> bool {
std::istringstream in{base_str};
in.imbue(std::locale::classic());
date::sys_time<std::chrono::seconds> tp;
date::from_stream(in, fmt, tp);
return !in.fail();
};
bool has_slash = (trimmed.find('/') != std::string::npos);
bool has_dash = (trimmed.find('-') != std::string::npos);
bool has_colon = (trimmed.find(':') != std::string::npos);
if ((has_slash || has_dash) && !has_colon) {
auto try_date_format = [&](const char* fmt) -> bool {
std::istringstream in{trimmed};
in.imbue(std::locale::classic());
date::year_month_day ymd;
date::from_stream(in, fmt, ymd);
return !in.fail() && ymd.ok() && (in.peek() == EOF);
};
const char* date_formats[] = {"%Y-%m-%d", "%Y/%m/%d", "%d/%m/%Y", "%m/%d/%Y", "%d-%m-%Y", "%m-%d-%Y"};
for (const char* fmt : date_formats) {
if (try_date_format(fmt)) {
info.type = ColumnType::DATE_ONLY;
info.format = fmt;
return info;
}
}
}
if (has_colon && !has_slash && !has_dash) {
auto try_time_format = [&](const char* fmt) -> bool {
std::istringstream in{base_str};
in.imbue(std::locale::classic());
std::chrono::seconds tod;
date::from_stream(in, fmt, tod);
return !in.fail();
};
const char* time_formats[] = {"%H:%M:%S", "%I:%M:%S %p"};
for (const char* fmt : time_formats) {
if (try_time_format(fmt)) {
info.type = ColumnType::TIME_ONLY;
info.format = fmt;
return info;
}
}
}
for (size_t i = 0; i < NUM_UNAMBIGUOUS_FORMATS; i++) {
if (try_format(UNAMBIGUOUS_FORMATS[i])) {
info.type = ColumnType::DATETIME;
info.format = UNAMBIGUOUS_FORMATS[i];
return info;
}
}
if (base_str.find('/') != std::string::npos && !base_str.empty() && base_str[0] != '2') {
const char* fmt = IsDayFirstFormat(base_str, '/') ? "%d/%m/%Y %H:%M:%S" : "%m/%d/%Y %H:%M:%S";
if (try_format(fmt)) {
info.type = ColumnType::DATETIME;
info.format = fmt;
return info;
}
}
if (base_str.find('-') != std::string::npos && !base_str.empty() && base_str[0] != '2') {
if (IsDayFirstFormat(base_str, '-')) {
const char* fmt = "%d-%m-%Y %H:%M:%S";
if (try_format(fmt)) {
info.type = ColumnType::DATETIME;
info.format = fmt;
return info;
}
}
}
info.type = ColumnType::STRING;
return info;
}
std::optional<double> ParseWithType(const std::string& str, const ColumnTypeInfo& type_info) {
std::string trimmed = Trim(str);
if (trimmed.empty()) {
return std::nullopt;
}
try {
switch (type_info.type) {
case ColumnType::NUMBER:
return toDouble(trimmed);
case ColumnType::HEX:
return static_cast<double>(std::stoll(trimmed, nullptr, 16));
case ColumnType::EPOCH_SECONDS:
case ColumnType::EPOCH_MILLIS:
case ColumnType::EPOCH_MICROS:
case ColumnType::EPOCH_NANOS:
return EpochToSeconds(std::stoll(trimmed), type_info.type);
case ColumnType::DATETIME: {
auto [base_str, fractional_ns] = ExtractFractionalSeconds(trimmed);
if (!type_info.has_fractional) {
fractional_ns = std::chrono::nanoseconds{0};
}
return TryParseFormat(base_str, type_info.format.c_str(), fractional_ns);
}
case ColumnType::DATE_ONLY: {
std::istringstream in{trimmed};
in.imbue(std::locale::classic());
date::year_month_day ymd;
date::from_stream(in, type_info.format.c_str(), ymd);
if (!in.fail() && ymd.ok()) {
auto tp = date::sys_days{ymd};
return std::chrono::duration<double>(tp.time_since_epoch()).count();
}
return std::nullopt;
}
case ColumnType::TIME_ONLY: {
auto [base_str, fractional_ns] = ExtractFractionalSeconds(trimmed);
if (!type_info.has_fractional) {
fractional_ns = std::chrono::nanoseconds{0};
}
std::istringstream in{base_str};
in.imbue(std::locale::classic());
std::chrono::seconds tod;
date::from_stream(in, type_info.format.c_str(), tod);
if (!in.fail()) {
auto total_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(tod) + fractional_ns;
return std::chrono::duration<double>(total_ns).count();
}
return std::nullopt;
}
case ColumnType::STRING:
default:
return std::nullopt;
}
} catch (...) {
return std::nullopt;
}
}
std::optional<double> ParseCombinedDateTime(
const std::string& date_str, const std::string& time_str, const ColumnTypeInfo& date_info,
const ColumnTypeInfo& time_info) {
std::string trimmed_date = Trim(date_str);
std::string trimmed_time = Trim(time_str);
if (trimmed_date.empty() || trimmed_time.empty()) {
return std::nullopt;
}
try {
std::istringstream date_in{trimmed_date};
date_in.imbue(std::locale::classic());
date::year_month_day ymd;
date::from_stream(date_in, date_info.format.c_str(), ymd);
if (date_in.fail() || !ymd.ok()) {
return std::nullopt;
}
auto [time_base, fractional_ns] = ExtractFractionalSeconds(trimmed_time);
if (!time_info.has_fractional) {
fractional_ns = std::chrono::nanoseconds{0};
}
std::istringstream time_in{time_base};
time_in.imbue(std::locale::classic());
std::chrono::seconds tod;
date::from_stream(time_in, time_info.format.c_str(), tod);
if (time_in.fail()) {
return std::nullopt;
}
auto tp = date::sys_days{ymd} + std::chrono::duration_cast<std::chrono::nanoseconds>(tod) + fractional_ns;
return std::chrono::duration<double>(tp.time_since_epoch()).count();
} catch (...) {
return std::nullopt;
}
}
} // namespace PJ::CSV