forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdatetime_value.cpp
More file actions
4065 lines (3810 loc) · 142 KB
/
Copy pathvdatetime_value.cpp
File metadata and controls
4065 lines (3810 loc) · 142 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "vec/runtime/vdatetime_value.h"
#include <cctz/civil_time.h>
#include <cctz/time_zone.h>
#include <glog/logging.h>
#include <cctype>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <ctime>
// IWYU pragma: no_include <bits/chrono.h>
#include <algorithm>
#include <chrono> // IWYU pragma: keep
// IWYU pragma: no_include <bits/std_abs.h>
#include <cmath>
#include <cstdint>
#include <string_view>
#include "common/compiler_util.h"
#include "common/config.h"
#include "common/exception.h"
#include "common/status.h"
#include "util/timezone_utils.h"
#include "vec/common/int_exp.h"
namespace doris {
static const char* s_ab_month_name[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", nullptr};
static const char* s_ab_day_name[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", nullptr};
uint8_t mysql_week_mode(uint32_t mode) {
mode &= 7;
if (!(mode & WEEK_MONDAY_FIRST)) {
mode ^= WEEK_FIRST_WEEKDAY;
}
return mode;
}
static bool check_space(char ch) {
// \t, \n, \v, \f, \r are 9~13, respectively.
return UNLIKELY(ch == ' ' || (ch >= 9 && ch <= 13));
}
static bool check_date_punct(char ch) {
return UNLIKELY(!(isdigit(ch) || isalpha(ch)));
}
static bool time_zone_begins(const char* ptr, const char* end) {
return *ptr == '+' || (*ptr == '-' && ptr + 3 < end && *(ptr + 3) == ':') ||
(isalpha(*ptr) && *ptr != 'T');
}
bool VecDateTimeValue::check_range(uint32_t year, uint32_t month, uint32_t day, uint32_t hour,
uint32_t minute, uint32_t second, uint16_t type) {
bool time = hour > (type == TIME_TIME ? TIME_MAX_HOUR : 23) || minute > 59 || second > 59;
if (type == TIME_TIME) {
return time;
} else {
return time || check_date(year, month, day);
}
}
bool VecDateTimeValue::check_date(uint32_t year, uint32_t month, uint32_t day) {
if (month == 2 && day == 29 && doris::is_leap(year)) {
return false;
}
if (year > 9999 || month == 0 || month > 12 || day > S_DAYS_IN_MONTH[month] || day == 0) {
return true;
}
return false;
}
// The interval format is that with no delimiters
// YYYY-MM-DD HH-MM-DD.FFFFFF AM in default format
// 0 1 2 3 4 5 6 7
bool VecDateTimeValue::from_date_str(const char* date_str, size_t len) {
return from_date_str_base(date_str, len, nullptr);
}
//parse timezone to get offset
bool VecDateTimeValue::from_date_str(const char* date_str, size_t len,
const cctz::time_zone& local_time_zone) {
return from_date_str_base(date_str, len, &local_time_zone);
}
bool VecDateTimeValue::from_date_str_base(const char* date_str, int len,
const cctz::time_zone* local_time_zone) {
const char* ptr = date_str;
const char* end = date_str + len;
// ONLY 2, 6 can follow by a space
const static int allow_space_mask = 4 | 64;
const static int MAX_DATE_PARTS = 8;
uint32_t date_val[MAX_DATE_PARTS];
int32_t date_len[MAX_DATE_PARTS];
_neg = false;
// Skip space character
while (ptr < end && check_space(*ptr)) {
ptr++;
}
if (ptr == end || !isdigit(*ptr)) {
return false;
}
// Fix year length
const char* pos = ptr;
while (pos < end && (isdigit(*pos) || *pos == 'T')) {
pos++;
}
int year_len = 4;
int digits = pos - ptr;
bool is_interval_format = false;
bool has_bar = false;
// Compatible with MySQL.
// For YYYYMMDD/YYYYMMDDHHMMSS is 4 digits years
if (pos == end || *pos == '.') {
if (digits == 4 || digits == 8 || digits >= 14) {
year_len = 4;
} else {
year_len = 2;
}
is_interval_format = true;
}
int field_idx = 0;
int field_len = year_len;
long sec_offset = 0;
while (ptr < end && isdigit(*ptr) && field_idx < MAX_DATE_PARTS - 1) {
const char* start = ptr;
int temp_val = 0;
bool scan_to_delim = (!is_interval_format) && (field_idx != 6);
while (ptr < end && isdigit(*ptr) && (scan_to_delim || field_len--)) {
temp_val = temp_val * 10 + (*ptr++ - '0');
}
// Impossible
if (temp_val > 999999L) {
return false;
}
date_val[field_idx] = temp_val;
date_len[field_idx] = ptr - start;
field_len = 2;
if (ptr == end) {
field_idx++;
break;
}
// timezone
if (UNLIKELY((field_idx > 2 ||
!has_bar) /*dont treat xxxx-xx-xx:xx:xx as xxxx-xx(-xx:xx:xx)*/
&& time_zone_begins(ptr, end))) {
if (local_time_zone == nullptr) {
return false;
}
auto get_tz_offset = [&](const std::string& str_tz,
const cctz::time_zone* local_time_zone) -> long {
cctz::time_zone given_tz {};
if (!TimezoneUtils::find_cctz_time_zone(str_tz, given_tz)) {
throw Exception {ErrorCode::INVALID_ARGUMENT, ""};
}
auto given = cctz::convert(cctz::civil_second {}, given_tz);
auto local = cctz::convert(cctz::civil_second {}, *local_time_zone);
// these two values is absolute time. so they are negative. need to use (-local) - (-given)
return std::chrono::duration_cast<std::chrono::seconds>(given - local).count();
};
try {
sec_offset = get_tz_offset(std::string {ptr, end},
local_time_zone); // use the whole remain string
} catch ([[maybe_unused]] Exception& e) {
return false; // invalid format
}
field_idx++;
break;
}
if (field_idx == 2 && *ptr == 'T') {
// YYYYMMDDTHHMMDD, skip 'T' and continue
ptr++;
field_idx++;
continue;
}
// Second part
if (field_idx == 5) {
if (*ptr == '.') {
ptr++;
field_len = 6;
} else if (isdigit(*ptr)) {
field_idx++;
break;
}
field_idx++;
continue;
}
// escape separator
while (ptr < end && (check_date_punct(*ptr) || check_space(*ptr))) {
if (check_space(*ptr)) {
if (((1 << field_idx) & allow_space_mask) == 0) {
return false;
}
}
if (*ptr == '-') {
has_bar = true;
}
ptr++;
}
field_idx++;
}
int num_field = field_idx;
if (num_field <= 3) {
_type = TIME_DATE;
} else {
_type = TIME_DATETIME;
}
if (!is_interval_format) {
year_len = date_len[0];
}
for (; field_idx < MAX_DATE_PARTS; ++field_idx) {
date_len[field_idx] = 0;
date_val[field_idx] = 0;
}
if (year_len == 2) {
if (date_val[0] < YY_PART_YEAR) {
date_val[0] += 2000;
} else {
date_val[0] += 1900;
}
}
if (num_field < 3) {
return false;
}
if (!check_range_and_set_time(date_val[0], date_val[1], date_val[2], date_val[3], date_val[4],
date_val[5], _type)) {
return false;
}
return sec_offset ? date_add_interval<TimeUnit::SECOND>(
TimeInterval {TimeUnit::SECOND, sec_offset, false})
: true;
}
// [0, 101) invalid
// [101, (YY_PART_YEAR - 1) * 10000 + 1231] for two digits year 2000 ~ 2069
// [(YY_PART_YEAR - 1) * 10000 + 1231, YY_PART_YEAR * 10000L + 101) invalid
// [YY_PART_YEAR * 10000L + 101, 991231] for two digits year 1970 ~1999
// (991231, 10000101) invalid, because support 1000-01-01
// [10000101, 99991231] for four digits year date value.
// (99991231, 101000000) invalid, NOTE below this is datetime vale hh:mm:ss must exist.
// [101000000, (YY_PART_YEAR - 1)##1231235959] two digits year datetime value
// ((YY_PART_YEAR - 1)##1231235959, YY_PART_YEAR##0101000000) invalid
// ((YY_PART_YEAR)##1231235959, 99991231235959] two digits year datetime value 1970 ~ 1999
// (999991231235959, ~) valid
int64_t VecDateTimeValue::standardize_timevalue(int64_t value) {
_type = TIME_DATE;
if (value <= 0) {
return 0;
}
if (value >= 10000101000000L) {
// 9999-99-99 99:99:99
if (value > 99999999999999L) {
return 0;
}
// between 1000-01-01 00:00:00L and 9999-99-99 99:99:99
// all digits exist.
_type = TIME_DATETIME;
return value;
}
// 2000-01-01
if (value < 101) {
return 0;
}
// two digits year. 2000 ~ 2069
if (value <= (YY_PART_YEAR - 1) * 10000L + 1231L) {
return (value + 20000000L) * 1000000L;
}
// two digits year, invalid date
if (value < YY_PART_YEAR * 10000L + 101) {
return 0;
}
// two digits year. 1970 ~ 1999
if (value <= 991231L) {
return (value + 19000000L) * 1000000L;
}
// TODO(zhaochun): Don't allow year betwen 1000-01-01
if (value < 10000101) {
return 0;
}
// four digits years without hour.
if (value <= 99991231L) {
return value * 1000000L;
}
// below 0000-01-01
if (value < 101000000) {
return 0;
}
// below is with datetime, must have hh:mm:ss
_type = TIME_DATETIME;
// 2000 ~ 2069
if (value <= (YY_PART_YEAR - 1) * 10000000000L + 1231235959L) {
return value + 20000000000000L;
}
if (value < YY_PART_YEAR * 10000000000L + 101000000L) {
return 0;
}
// 1970 ~ 1999
if (value <= 991231235959L) {
return value + 19000000000000L;
}
return value;
}
bool VecDateTimeValue::from_date_int64(int64_t value) {
_neg = false;
value = standardize_timevalue(value);
if (value <= 0) {
return false;
}
uint64_t date = value / 1000000;
uint64_t time = value % 1000000;
auto [year, month, day, hour, minute, second] = std::tuple {0, 0, 0, 0, 0, 0};
year = date / 10000;
date %= 10000;
month = date / 100;
day = date % 100;
hour = time / 10000;
time %= 10000;
minute = time / 100;
second = time % 100;
return check_range_and_set_time(year, month, day, hour, minute, second, _type);
}
void VecDateTimeValue::set_zero(int type) {
memset(this, 0, sizeof(*this));
_type = type;
}
void VecDateTimeValue::set_type(int type) {
_type = type;
if (type == TIME_DATE) {
_hour = 0;
_minute = 0;
_second = 0;
}
}
void VecDateTimeValue::set_max_time(bool neg) {
set_zero(TIME_TIME);
_hour = static_cast<uint8_t>(TIME_MAX_HOUR);
_minute = TIME_MAX_MINUTE;
_second = TIME_MAX_SECOND;
_neg = neg;
}
bool VecDateTimeValue::from_time_int64(int64_t value) {
_type = TIME_TIME;
if (value > TIME_MAX_VALUE) {
// 0001-01-01 00:00:00 to convert to a datetime
if (value > 10000000000L) {
if (from_date_int64(value)) {
return true;
}
}
set_max_time(false);
return false;
} else if (value < -1 * TIME_MAX_VALUE) {
set_max_time(true);
return false;
}
if (value < 0) {
_neg = 1;
value = -value;
}
_hour = value / 10000;
value %= 10000;
_minute = value / 100;
if (_minute > TIME_MAX_MINUTE) {
return false;
}
_second = value % 100;
return _second <= TIME_MAX_SECOND;
}
char* VecDateTimeValue::append_date_buffer(char* to) const {
uint32_t temp;
// Year
temp = _year / 100;
*to++ = (char)('0' + (temp / 10));
*to++ = (char)('0' + (temp % 10));
temp = _year % 100;
*to++ = (char)('0' + (temp / 10));
*to++ = (char)('0' + (temp % 10));
*to++ = '-';
// Month
*to++ = (char)('0' + (_month / 10));
*to++ = (char)('0' + (_month % 10));
*to++ = '-';
// Day
*to++ = (char)('0' + (_day / 10));
*to++ = (char)('0' + (_day % 10));
return to;
}
char* VecDateTimeValue::append_time_buffer(char* to) const {
if (_neg) {
*to++ = '-';
}
// Hour
uint32_t temp = _hour;
if (temp >= 100) {
*to++ = (char)('0' + (temp / 100));
temp %= 100;
}
*to++ = (char)('0' + (temp / 10));
*to++ = (char)('0' + (temp % 10));
*to++ = ':';
// Minute
*to++ = (char)('0' + (_minute / 10));
*to++ = (char)('0' + (_minute % 10));
*to++ = ':';
/* Second */
*to++ = (char)('0' + (_second / 10));
*to++ = (char)('0' + (_second % 10));
return to;
}
char* VecDateTimeValue::to_datetime_buffer(char* to) const {
to = append_date_buffer(to);
*to++ = ' ';
return append_time_buffer(to);
}
char* VecDateTimeValue::to_date_buffer(char* to) const {
return append_date_buffer(to);
}
char* VecDateTimeValue::to_time_buffer(char* to) const {
return append_time_buffer(to);
}
int32_t VecDateTimeValue::to_buffer(char* buffer) const {
switch (_type) {
case TIME_TIME:
return to_time_buffer(buffer) - buffer;
case TIME_DATE:
return to_date_buffer(buffer) - buffer;
case TIME_DATETIME:
return to_datetime_buffer(buffer) - buffer;
default:
break;
}
return 0;
}
char* VecDateTimeValue::to_string(char* to) const {
int len = to_buffer(to);
*(to + len) = '\0';
return to + len + 1;
}
int64_t VecDateTimeValue::to_datetime_int64() const {
return (_year * 10000L + _month * 100 + _day) * 1000000L + _hour * 10000 + _minute * 100 +
_second;
}
int64_t VecDateTimeValue::to_date_int64() const {
return _year * 10000 + _month * 100 + _day;
}
int64_t VecDateTimeValue::to_time_int64() const {
int sign = _neg == 0 ? 1 : -1;
return sign * (_hour * 10000 + _minute * 100 + _second);
}
int64_t VecDateTimeValue::to_int64() const {
switch (_type) {
case TIME_TIME:
return to_time_int64();
case TIME_DATE:
return to_date_int64();
case TIME_DATETIME:
return to_datetime_int64();
default:
return 0;
}
}
bool VecDateTimeValue::get_date_from_daynr(uint64_t daynr) {
if (daynr <= 0 || daynr > DATE_MAX_DAYNR) {
return false;
}
auto [year, month, day] = std::tuple {0, 0, 0};
year = daynr / 365;
uint32_t days_befor_year = 0;
while (daynr < (days_befor_year = doris::calc_daynr(year, 1, 1))) {
year--;
}
uint32_t days_of_year = daynr - days_befor_year + 1;
int leap_day = 0;
if (doris::is_leap(year)) {
if (days_of_year > 31 + 28) {
days_of_year--;
if (days_of_year == 31 + 28) {
leap_day = 1;
}
}
}
month = 1;
while (days_of_year > S_DAYS_IN_MONTH[month]) {
days_of_year -= S_DAYS_IN_MONTH[month];
month++;
}
day = days_of_year + leap_day;
if (check_range(year, month, day, 0, 0, 0, _type)) {
return false;
}
unchecked_set_time(year, month, day, _hour, _minute, _second);
return true;
}
bool VecDateTimeValue::from_date_daynr(uint64_t daynr) {
_neg = false;
if (!get_date_from_daynr(daynr)) {
return false;
}
_hour = 0;
_minute = 0;
_second = 0;
_type = TIME_DATE;
return true;
}
/// @return: tail
static char* int_to_str(uint64_t val, char* to) {
char buf[64];
char* ptr = buf;
// Use do/while for 0 value
do {
*ptr++ = '0' + (val % 10);
val /= 10;
} while (val);
while (ptr > buf) {
*to++ = *--ptr;
}
return to;
}
static char* append_string(const char* from, char* to) {
while (*from) {
*to++ = *from++;
}
return to;
}
static char* append_with_prefix(const char* str, int str_len, char prefix, int target_len,
char* to) {
// full_len is the lower bound. if less, use prefix to pad. if greater, accept all.
int diff = target_len - str_len;
// use prefix to pad
while (diff-- > 0) { // won't be INT_MIN. it's ok
*to++ = prefix;
}
memcpy(to, str, str_len);
return to + str_len;
}
int VecDateTimeValue::compute_format_len(const char* format, size_t len) {
int size = 0;
const char* ptr = format;
const char* end = format + len;
while (ptr < end) {
if (*ptr != '%' || (ptr + 1) < end) {
size++;
ptr++;
continue;
}
switch (*++ptr) {
case 'M':
case 'W':
size += 10;
break;
case 'D':
case 'Y':
case 'x':
case 'X':
size += 4;
break;
case 'a':
case 'b':
size += 10;
break;
case 'j':
size += 3;
break;
case 'u':
case 'U':
case 'v':
case 'V':
case 'y':
case 'm':
case 'd':
case 'h':
case 'i':
case 'I':
case 'l':
case 'p':
case 'S':
case 's':
case 'c':
case 'e':
size += 2;
break;
case 'k':
case 'H':
size += 7;
break;
case 'r':
size += 11;
break;
case 'T':
size += 8;
break;
case 'f':
size += 6;
break;
case 'w':
case '%':
default:
size++;
break;
}
}
return size;
}
static const char digits100[201] =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899";
char* write_two_digits_to_string(int number, char* dst) {
memcpy(dst, &digits100[number * 2], 2);
return dst + 2;
}
char* write_four_digits_to_string(int number, char* dst) {
memcpy(dst, &digits100[(number / 100) * 2], 2);
memcpy(dst + 2, &digits100[(number % 100) * 2], 2);
return dst + 4;
}
bool VecDateTimeValue::to_format_string_conservative(const char* format, size_t len, char* to,
size_t max_valid_length) const {
if (check_range(_year, _month, _day, _hour, _minute, _second, _type)) {
return false;
}
char* const begin = to; // to check written bytes
char buf[64];
char* cursor = buf;
char* pos = nullptr;
const char* ptr = format;
const char* end = format + len;
char ch = '\0';
while (ptr < end) {
if (to - begin + SAFE_FORMAT_STRING_MARGIN > max_valid_length) [[unlikely]] {
return false;
}
if (*ptr != '%' || (ptr + 1) == end) {
*to++ = *ptr++;
continue;
}
// Skip '%'
ptr++;
switch (ch = *ptr++) {
case 'y':
// Year, numeric (two digits)
to = write_two_digits_to_string(_year % 100, to);
cursor += 2;
pos = cursor;
break;
case 'Y':
// Year, numeric, four digits
to = write_four_digits_to_string(_year, to);
cursor += 4;
pos = cursor;
break;
case 'd':
// Day of month (00...31)
to = write_two_digits_to_string(_day, to);
cursor += 2;
pos = cursor;
break;
case 'H':
to = write_two_digits_to_string(_hour, to);
cursor += 2;
pos = cursor;
break;
case 'i':
// Minutes, numeric (00..59)
to = write_two_digits_to_string(_minute, to);
cursor += 2;
pos = cursor;
break;
case 'm':
to = write_two_digits_to_string(_month, to);
cursor += 2;
pos = cursor;
break;
case 'h':
case 'I':
// Hour (01..12)
to = write_two_digits_to_string((_hour % 24 + 11) % 12 + 1, to);
cursor += 2;
pos = cursor;
break;
case 's':
case 'S':
// Seconds (00..59)
to = write_two_digits_to_string(_second, to);
cursor += 2;
pos = cursor;
break;
case 'a':
// Abbreviated weekday name
if (_type == TIME_TIME || (_year == 0 && _month == 0)) {
return false;
}
to = append_string(s_ab_day_name[weekday()], to);
break;
case 'b':
// Abbreviated month name
if (_month == 0) {
return false;
}
to = append_string(s_ab_month_name[_month], to);
break;
case 'c':
// Month, numeric (0...12)
pos = int_to_str(_month, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
break;
case 'D':
// Day of the month with English suffix (0th, 1st, ...)
pos = int_to_str(_day, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
if (_day >= 10 && _day <= 19) {
to = append_string("th", to);
} else {
switch (_day % 10) {
case 1:
to = append_string("st", to);
break;
case 2:
to = append_string("nd", to);
break;
case 3:
to = append_string("rd", to);
break;
default:
to = append_string("th", to);
break;
}
}
break;
case 'e':
// Day of the month, numeric (0..31)
pos = int_to_str(_day, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
break;
case 'f':
// Microseconds (000000..999999)
pos = int_to_str(0, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 6, to);
break;
case 'j':
// Day of year (001..366)
pos = int_to_str(daynr() - doris::calc_daynr(_year, 1, 1) + 1, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 3, to);
break;
case 'k':
// Hour (0..23)
pos = int_to_str(_hour, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
break;
case 'l':
// Hour (1..12)
pos = int_to_str((_hour % 24 + 11) % 12 + 1, cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
break;
case 'M':
// Month name (January..December)
if (_month == 0) {
return false;
}
to = append_string(s_month_name[_month], to);
break;
case 'p':
// AM or PM
if ((_hour % 24) >= 12) {
to = append_string("PM", to);
} else {
to = append_string("AM", to);
}
break;
case 'r':
// Time, 12-hour (hh:mm:ss followed by AM or PM)
*to++ = (char)('0' + (((_hour + 11) % 12 + 1) / 10));
*to++ = (char)('0' + (((_hour + 11) % 12 + 1) % 10));
*to++ = ':';
// Minute
*to++ = (char)('0' + (_minute / 10));
*to++ = (char)('0' + (_minute % 10));
*to++ = ':';
/* Second */
*to++ = (char)('0' + (_second / 10));
*to++ = (char)('0' + (_second % 10));
if ((_hour % 24) >= 12) {
to = append_string(" PM", to);
} else {
to = append_string(" AM", to);
}
break;
case 'T':
// Time, 24-hour (hh:mm:ss)
*to++ = (char)('0' + ((_hour % 24) / 10));
*to++ = (char)('0' + ((_hour % 24) % 10));
*to++ = ':';
// Minute
*to++ = (char)('0' + (_minute / 10));
*to++ = (char)('0' + (_minute % 10));
*to++ = ':';
/* Second */
*to++ = (char)('0' + (_second / 10));
*to++ = (char)('0' + (_second % 10));
break;
case 'u':
// Week (00..53), where Monday is the first day of the week;
// WEEK() mode 1
if (_type == TIME_TIME) {
return false;
}
to = write_two_digits_to_string(week(mysql_week_mode(1)), to);
cursor += 2;
pos = cursor;
break;
case 'U':
// Week (00..53), where Sunday is the first day of the week;
// WEEK() mode 0
if (_type == TIME_TIME) {
return false;
}
to = write_two_digits_to_string(week(mysql_week_mode(0)), to);
cursor += 2;
pos = cursor;
break;
case 'v':
// Week (01..53), where Monday is the first day of the week;
// WEEK() mode 3; used with %x
if (_type == TIME_TIME) {
return false;
}
to = write_two_digits_to_string(week(mysql_week_mode(3)), to);
cursor += 2;
pos = cursor;
break;
case 'V':
// Week (01..53), where Sunday is the first day of the week;
// WEEK() mode 2; used with %X
if (_type == TIME_TIME) {
return false;
}
to = write_two_digits_to_string(week(mysql_week_mode(2)), to);
cursor += 2;
pos = cursor;
break;
case 'w':
// Day of the week (0=Sunday..6=Saturday)
if (_type == TIME_TIME || (_month == 0 && _year == 0)) {
return false;
}
pos = int_to_str(doris::calc_weekday(daynr(), true), cursor);
to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
break;
case 'W':
// Weekday name (Sunday..Saturday)
to = append_string(s_day_name[weekday()], to);
break;
case 'x': {
// Year for the week, where Monday is the first day of the week,
// numeric, four digits; used with %v
if (_type == TIME_TIME) {
return false;
}
uint32_t year = 0;
calc_week(*this, mysql_week_mode(3), &year, true);
to = write_four_digits_to_string(year, to);
cursor += 4;
pos = cursor;
break;
}
case 'X': {
// Year for the week where Sunday is the first day of the week,
// numeric, four digits; used with %V
if (_type == TIME_TIME) {
return false;
}
uint32_t year = 0;
calc_week(*this, mysql_week_mode(2), &year);
to = write_four_digits_to_string(year, to);
cursor += 4;
pos = cursor;
break;
}
default:
// put it literal
*to++ = ch;
break;
}
}
*to++ = '\0';
return true;
}
uint8_t VecDateTimeValue::calc_week(const VecDateTimeValue& value, uint8_t mode, uint32_t* year,
bool disable_lut) {
// mode=3 is used for week_of_year()
if (config::enable_time_lut && !disable_lut && mode == 3 && value._year >= 1950 &&
value._year < 2030) {
return doris::TimeLUT::GetImplement()
->week_of_year_table[value._year - doris::LUT_START_YEAR][value._month - 1]
[value._day - 1];
}
// mode=4 is used for week()
if (config::enable_time_lut && !disable_lut && mode == 4 && value._year >= 1950 &&
value._year < 2030) {
return doris::TimeLUT::GetImplement()
->week_table[value._year - doris::LUT_START_YEAR][value._month - 1][value._day - 1];
}
// not covered by pre calculated dates, calculate at runtime
bool monday_first = mode & WEEK_MONDAY_FIRST;
bool week_year = mode & WEEK_YEAR;
bool first_weekday = mode & WEEK_FIRST_WEEKDAY;
uint64_t day_nr = value.daynr();
uint64_t daynr_first_day = doris::calc_daynr(value._year, 1, 1);
uint8_t weekday_first_day = doris::calc_weekday(daynr_first_day, !monday_first);
int days = 0;
*year = value._year;
// Check weather the first days of this year belongs to last year
if (value._month == 1 && value._day <= (7 - weekday_first_day)) {
if (!week_year && ((first_weekday && weekday_first_day != 0) ||
(!first_weekday && weekday_first_day > 3))) {
return 0;
}
(*year)--;
week_year = true;
daynr_first_day -= (days = doris::calc_days_in_year(*year));
weekday_first_day = (weekday_first_day + 53 * 7 - days) % 7;
}
// How many days since first week
if ((first_weekday && weekday_first_day != 0) || (!first_weekday && weekday_first_day > 3)) {
// days in new year belongs to last year.