-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmy_time.cc
2908 lines (2540 loc) · 95.9 KB
/
my_time.cc
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
/* Copyright (c) 2004, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@defgroup MY_TIME Mysys time utilities
@ingroup MYSYS
@{
@file mysys/my_time.cc
Implementation of low level time utilities.
*/
/**
@ingroup MY_TIME
@page LOW_LEVEL_FORMATS Low-level memory and disk formats
- @subpage datetime_and_date_low_level_rep
- @subpage time_low_level_rep
*/
#include "my_time.h"
#include <assert.h> // assert
#include <algorithm> // std::max
#include <cctype> // std::isspace
#include <climits> // UINT_MAX
#include <cstdio> // std::sprintf
#include <cstring> // std::memset
#include "field_types.h" // enum_field_types
#include "integer_digits.h" // count_digits, write_digits, write_two_digits
#include "my_byteorder.h" // int3store
#include "my_systime.h" // localtime_r
#include "myisampack.h" // mi_int2store
#include "template_utils.h" // pointer_cast
const ulonglong log_10_int[20] = {1,
10,
100,
1000,
10000UL,
100000UL,
1000000UL,
10000000UL,
100000000ULL,
1000000000ULL,
10000000000ULL,
100000000000ULL,
1000000000000ULL,
10000000000000ULL,
100000000000000ULL,
1000000000000000ULL,
10000000000000000ULL,
100000000000000000ULL,
1000000000000000000ULL,
10000000000000000000ULL};
const char my_zero_datetime6[] = "0000-00-00 00:00:00.000000";
static constexpr const char time_separator = ':';
/** Day number with 1970-01-01 as base. */
static constexpr ulong const days_at_timestart = 719528;
const uchar days_in_month[] = {31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31, 0};
/**
Offset of system time zone from UTC in seconds used to speed up
work of my_system_gmt_sec() function.
*/
static my_time_t my_time_zone = 0;
// Right-shift of a negative value is implementation-defined
// Assert that we have arithmetic shift of negative numbers
static_assert((-2 >> 1) == -1, "Right shift of negative numbers is arithmetic");
static longlong my_packed_time_get_int_part(longlong i) { return (i >> 24); }
static longlong my_packed_time_make(longlong i, longlong f) {
assert(std::abs(f) <= 0xffffffLL);
return (static_cast<ulonglong>(i) << 24) + f;
}
static longlong my_packed_time_make_int(longlong i) {
return (static_cast<ulonglong>(i) << 24);
}
// The behavior of <cctype> functions is undefined if the argument's value is
// neither representable as unsigned char nor equal to EOF. To use these
// functions safely with plain chars, cast to unsigned char.
static inline int isspace_char(char ch) {
return std::isspace(static_cast<unsigned char>(ch));
}
static inline int isdigit_char(char ch) {
return std::isdigit(static_cast<unsigned char>(ch));
}
static inline int ispunct_char(char ch) {
return std::ispunct(static_cast<unsigned char>(ch));
}
/**
Calc days in one year.
@note Works with both two and four digit years.
@return number of days in that year
*/
uint calc_days_in_year(uint year) {
return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)) ? 366
: 365);
}
/**
Set MYSQL_TIME structure to 0000-00-00 00:00:00.000000
@param [out] tm The value to set.
@param time_type Timestasmp type
*/
void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type) {
memset(tm, 0, sizeof(*tm));
tm->time_type = time_type;
}
/**
Set hour, minute and second of a MYSQL_TIME variable to maximum time value.
Unlike set_max_time(), does not touch the other structure members.
*/
void set_max_hhmmss(MYSQL_TIME *tm) {
tm->hour = TIME_MAX_HOUR;
tm->minute = TIME_MAX_MINUTE;
tm->second = TIME_MAX_SECOND;
}
/**
Set MYSQL_TIME variable to maximum time value
@param tm OUT The variable to set.
@param neg Sign: 1 if negative, 0 if positive.
*/
void set_max_time(MYSQL_TIME *tm, bool neg) {
set_zero_time(tm, MYSQL_TIMESTAMP_TIME);
set_max_hhmmss(tm);
tm->neg = neg;
}
/**
@brief Check datetime value for validity according to flags.
@param[in] my_time Date to check.
@param[in] not_zero_date my_time is not the zero date
@param[in] flags flags to check
(see str_to_datetime() flags in my_time.h)
@param[out] was_cut set to 2 if value was invalid according to flags.
(Feb 29 in non-leap etc.). This remains unchanged
if value is not invalid.
@details Here we assume that year and month is ok!
If month is 0 we allow any date. (This only happens if we allow zero
date parts in str_to_datetime())
Disallow dates with zero year and non-zero month and/or day.
@retval false OK
@retval true error
*/
bool check_date(const MYSQL_TIME &my_time, bool not_zero_date,
my_time_flags_t flags, int *was_cut) {
if (not_zero_date) {
if (((flags & TIME_NO_ZERO_IN_DATE) || !(flags & TIME_FUZZY_DATE)) &&
(my_time.month == 0 || my_time.day == 0)) {
*was_cut = MYSQL_TIME_WARN_ZERO_IN_DATE;
return true;
} else if ((!(flags & TIME_INVALID_DATES) && my_time.month &&
my_time.day > days_in_month[my_time.month - 1] &&
(my_time.month != 2 || calc_days_in_year(my_time.year) != 366 ||
my_time.day != 29))) {
*was_cut = MYSQL_TIME_WARN_OUT_OF_RANGE;
return true;
}
} else if (flags & TIME_NO_ZERO_DATE) {
*was_cut = MYSQL_TIME_WARN_ZERO_DATE;
return true;
}
return false;
}
/**
Check if TIME fields can be adjusted to make the time value valid.
@param my_time Time value.
@retval true if the value cannot be made valid.
@retval false if the value is already valid or can be adjusted to
become valid.
*/
bool check_time_mmssff_range(const MYSQL_TIME &my_time) {
return my_time.minute >= 60 || my_time.second >= 60 ||
my_time.second_part > 999999;
}
/**
Check TIME range. The value can include day part,
for example: '1 10:20:30.123456'.
minute, second and second_part values are not checked
unless hour is equal TIME_MAX_HOUR.
@param my_time Time value.
@returns Test result.
@retval false if value is Ok.
@retval true if value is out of range.
*/
bool check_time_range_quick(const MYSQL_TIME &my_time) {
longlong hour = static_cast<longlong>(my_time.hour) + 24LL * my_time.day;
/* The input value should not be fatally bad */
assert(!check_time_mmssff_range(my_time));
if (hour <= TIME_MAX_HOUR &&
(hour != TIME_MAX_HOUR || my_time.minute != TIME_MAX_MINUTE ||
my_time.second != TIME_MAX_SECOND || !my_time.second_part))
return false;
return true;
}
/**
Check datetime, date, or normalized time (i.e. time without days) range.
@param my_time Datetime value.
@retval false on success
@retval true on error
*/
bool check_datetime_range(const MYSQL_TIME &my_time) {
/*
In case of MYSQL_TIMESTAMP_TIME hour value can be up to TIME_MAX_HOUR.
In case of MYSQL_TIMESTAMP_DATETIME it cannot be bigger than 23.
*/
return my_time.year > 9999U || my_time.month > 12U || my_time.day > 31U ||
my_time.minute > 59U || my_time.second > 59U ||
my_time.second_part > 999999U ||
(my_time.hour >
(my_time.time_type == MYSQL_TIMESTAMP_TIME ? TIME_MAX_HOUR : 23U));
}
#define MAX_DATE_PARTS 8
/**
Parses a time zone displacement string on the form `{+-}HH:MM`, converting
to seconds.
@param[in] str Time zone displacement string.
@param[in] length Length of said string.
@param[out] result Calculated displacement in seconds.
@retval false Ok.
@retval true Not a valid time zone displacement string.
*/
bool time_zone_displacement_to_seconds(const char *str, size_t length,
int *result) {
if (length < 6) return true;
int sign = str[0] == '+' ? 1 : (str[0] == '-' ? -1 : 0);
if (sign == 0) return true;
if (!(std::isdigit(str[1]) && std::isdigit(str[2]))) return true;
int hours = (str[1] - '0') * 10 + str[2] - '0';
if (str[3] != ':') return true;
if (!(std::isdigit(str[4]) && std::isdigit(str[5]))) return true;
int minutes = (str[4] - '0') * 10 + str[5] - '0';
if (minutes >= MINS_PER_HOUR) return true;
int seconds = hours * SECS_PER_HOUR + minutes * SECS_PER_MIN;
if (seconds > MAX_TIME_ZONE_HOURS * SECS_PER_HOUR) return true;
// The SQL standard forbids -00:00.
if (sign == -1 && hours == 0 && minutes == 0) return true;
for (size_t i = 6; i < length; ++i)
if (!std::isspace(str[i])) return true;
*result = seconds * sign;
return false;
}
/**
Convert a timestamp string to a MYSQL_TIME value.
DESCRIPTION
At least the following formats are recognized (based on number of digits)
YYMMDD, YYYYMMDD, YYMMDDHHMMSS, YYYYMMDDHHMMSS
YY-MM-DD, YYYY-MM-DD, YY-MM-DD HH.MM.SS
YYYYMMDDTHHMMSS where T is a the character T (ISO8601)
Also dates where all parts are zero are allowed
The second part may have an optional .###### fraction part.
The datetime value may be followed by a time zone displacement +/-HH:MM.
NOTES
This function should work with a format position vector as long as the
following things holds:
- All date are kept together and all time parts are kept together
- Date and time parts must be separated by blank
- Second fractions must come after second part and be separated
by a '.'. (The second fractions are optional)
- AM/PM must come after second fractions (or after seconds if no fractions)
- Year must always been specified.
- If time is before date, then we will use datetime format only if
the argument consist of two parts, separated by space.
Otherwise we will assume the argument is a date.
- The hour part must be specified in hour-minute-second order.
status->warnings is set to:
0 Value OK
MYSQL_TIME_WARN_TRUNCATED If value was cut during conversion
MYSQL_TIME_WARN_OUT_OF_RANGE check_date(date,flags) considers date invalid
l_time->time_type is set as follows:
MYSQL_TIMESTAMP_NONE String wasn't a timestamp, like
[DD [HH:[MM:[SS]]]].fraction.
l_time is not changed.
MYSQL_TIMESTAMP_DATE DATE string (YY MM and DD parts ok)
MYSQL_TIMESTAMP_DATETIME Full timestamp
MYSQL_TIMESTAMP_ERROR Timestamp with wrong values.
All elements in l_time is set to 0
flags is a bit field with the following possible values:
TIME_FUZZY_DATE
TIME_DATETIME_ONLY
TIME_NO_ZERO_IN_DATE
TIME_NO_ZERO_DATE
TIME_INVALID_DATES
@param str_arg String to parse
@param length Length of string
@param[out] l_time Date is stored here
@param flags Bitfield
TIME_FUZZY_DATE|TIME_DATETIME_ONLY|TIME_NO_ZERO_IN_DATE|TIME_NO_ZERO_DATE|TIME_INVALID_DATES
(described above)
@param status Conversion status and warnings
@retval false Ok
@retval true Error
*/
bool str_to_datetime(const char *const str_arg, std::size_t length,
MYSQL_TIME *l_time, my_time_flags_t flags,
MYSQL_TIME_STATUS *status) {
uint field_length = 0;
uint year_length = 0;
uint digits;
uint number_of_fields;
uint date[MAX_DATE_PARTS];
uint date_len[MAX_DATE_PARTS];
uint start_loop;
ulong not_zero_date;
// Hyphen is mandated after digit sequence 1 and 2, e.g. 2000-12-23, i.e.
// after the year part and after the month part.
constexpr ulong allow_hyphen = (1 << 0) | (1 << 1);
// Colon is mandated after digit sequence 4 and 5, e.g. 2000-12-23 14:44:10,
// after the hour part and after the minutes part.
constexpr ulong allow_colon = (1 << 3) | (1 << 4);
bool is_internal_format = false;
const char *pos;
const char *last_field_pos = nullptr;
const char *end = str_arg + length;
bool found_delimiter = false;
bool found_space = false;
bool found_displacement = false;
uint frac_pos;
uint frac_len;
int displacement = 0;
const char *str = str_arg;
assert(status->warnings == 0 && status->fractional_digits == 0 &&
status->nanoseconds == 0);
/* Skip space at start */
for (; str != end && isspace_char(*str); str++)
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, str);
if (str == end || !isdigit_char(*str)) {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
is_internal_format = false;
/* This has to be changed if want to activate different timestamp formats */
/*
Calculate number of digits in first part.
If length= 8 or >= 14 then year is of format YYYY.
(YYYY-MM-DD, YYYYMMDD, YYYYYMMDDHHMMSS)
*/
for (pos = str; pos != end && (isdigit_char(*pos) || *pos == 'T'); pos++)
;
digits = static_cast<uint>(pos - str);
start_loop = 0; /* Start of scan loop */
date_len[0] = 0; /* Length of year field */
if (pos == end || *pos == '.') {
/* Found date in internal format (only numbers like YYYYMMDD) */
year_length = (digits == 4 || digits == 8 || digits >= 14) ? 4 : 2;
field_length = year_length;
is_internal_format = true;
} else {
field_length = 4;
}
/*
Only allow space in the first "part" of the datetime field and:
- after days, part seconds
2003-03-03 20:00:20.44
*/
ulong allow_space = ((1 << 2) | (1 << 6));
not_zero_date = 0;
uint i;
for (i = start_loop;
i < MAX_DATE_PARTS - 1 && str != end && isdigit_char(*str); i++) {
const char *start = str;
ulong tmp_value = static_cast<uchar>(*str++ - '0');
/*
Internal format means no delimiters; every field has a fixed
width. Otherwise, we scan until we find a delimiter and discard
leading zeroes -- except for the microsecond part, where leading
zeroes are significant, and where we never process more than six
digits.
*/
bool scan_until_delim = !is_internal_format && (i != 6);
while (str != end && isdigit_char(str[0]) &&
(scan_until_delim || --field_length)) {
tmp_value =
tmp_value * 10 + static_cast<ulong>(static_cast<uchar>(*str - '0'));
str++;
if (tmp_value > 999999) /* Impossible date part */
{
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
}
date_len[i] = static_cast<uint>(str - start);
date[i] = tmp_value;
not_zero_date |= tmp_value;
/* Length of next field */
field_length = 2;
if ((last_field_pos = str) == end) {
i++; /* Register last found part */
break;
}
/* Allow a 'T' after day to allow CCYYMMDDT type of fields */
if (i == 2 && *str == 'T') {
str++; /* ISO8601: CCYYMMDDThhmmss */
continue;
}
if (i == 5) /* Seconds */
{
if (*str == '.') /* Followed by part seconds */
{
str++;
/*
Shift last_field_pos, so '2001-01-01 00:00:00.'
is treated as a valid value
*/
last_field_pos = str;
field_length = 6; /* 6 digits */
} else if (isdigit_char(str[0])) {
/*
We do not see a decimal point which would have indicated a
fractional second part in further read. So we skip the further
processing of digits.
*/
i++;
break;
} else if (str[0] == '+' || str[0] == '-') {
if (!time_zone_displacement_to_seconds(str, end - str, &displacement)) {
found_displacement = true;
str += end - str;
last_field_pos = str;
} else {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
}
continue;
}
if (i == 6 && (str[0] == '+' || str[0] == '-')) {
if (!time_zone_displacement_to_seconds(str, end - str, &displacement)) {
found_displacement = true;
str += end - str;
last_field_pos = str;
} else {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
}
bool one_delim_seen = false;
while (str != end && (ispunct_char(*str) || isspace_char(*str))) {
if (one_delim_seen) {
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, str);
}
if (isspace_char(*str)) {
if (!(allow_space & (1 << i))) {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
if (i == 6) {
status->set_deprecation(
MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS, str_arg, end,
str);
}
found_space = true;
if (*str != ' ') {
status->set_deprecation(
MYSQL_TIME_STATUS::DEPRECATION::DP_WRONG_SPACE, str_arg, end,
str);
}
} else if (!((*str == '-' && allow_hyphen & (1 << i)) ||
(*str == ':' && allow_colon & (1 << i))) &&
i != 2) {
if (is_internal_format && year_length == 2 && date_len[0] == 1) {
// skip deprecation the case of year given as 4.12.3, because
// changing it to 4-12-3 would yield a different year (2004-12-03 vs
// 0004-12-03). We will deprecate short years in WL#13603 instead.
} else {
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_WRONG_KIND,
str_arg, end, str, i > 1);
}
} else if (i == 2 && (*str != '.' || !is_internal_format)) {
// Corner case: i == 2 is the position between date and time. Sometimes
// in internal format it will be a period, but we can't flag that
// here. Example: In '10101.5' > date'2021-10-12', the ".5" is accepted
// but discarded silently. Substituting a space here will not parse,
// so we stay silent.
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_WRONG_SPACE,
str_arg, end, str);
}
str++;
one_delim_seen = true;
found_delimiter = true; /* Should be a 'normal' date */
}
/* Check if next position is AM/PM */
if (i == 6) /* Seconds, time for AM/PM */
{
i++; /* Skip AM/PM part */
}
last_field_pos = str;
}
if (found_delimiter) {
if (found_space && i == 3 && str == end) {
// superfluous space at end of date (and no time given)
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, str - 1);
} else if (!found_space && (flags & TIME_DATETIME_ONLY)) {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true; /* Can't be a datetime */
}
}
str = last_field_pos;
number_of_fields = i - start_loop;
while (i < MAX_DATE_PARTS) {
date_len[i] = 0;
date[i++] = 0;
}
if (!is_internal_format) {
year_length = date_len[0];
if (!year_length) /* Year must be specified */
{
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
l_time->year = date[static_cast<uint>(0)];
l_time->month = date[static_cast<uint>(1)];
l_time->day = date[static_cast<uint>(2)];
l_time->hour = date[static_cast<uint>(3)];
l_time->minute = date[static_cast<uint>(4)];
l_time->second = date[static_cast<uint>(5)];
l_time->time_zone_displacement = displacement;
frac_pos = static_cast<uint>(6);
frac_len = date_len[frac_pos];
status->fractional_digits = frac_len;
if (frac_len < 6)
date[frac_pos] *=
static_cast<uint>(log_10_int[DATETIME_MAX_DECIMALS - frac_len]);
l_time->second_part = date[frac_pos];
} else {
l_time->year = date[0];
l_time->month = date[1];
l_time->day = date[2];
l_time->hour = date[3];
l_time->minute = date[4];
l_time->second = date[5];
if (date_len[6] < 6)
date[6] *=
static_cast<uint>(log_10_int[DATETIME_MAX_DECIMALS - date_len[6]]);
l_time->second_part = date[6];
l_time->time_zone_displacement = displacement;
status->fractional_digits = date_len[6];
}
l_time->neg = false;
if (year_length == 2 && not_zero_date)
l_time->year += (l_time->year < YY_PART_YEAR ? 2000 : 1900);
/*
Set time_type before check_datetime_range(),
as the latter relies on initialized time_type value.
*/
l_time->time_type =
(number_of_fields <= 3 ? MYSQL_TIMESTAMP_DATE
: (found_displacement ? MYSQL_TIMESTAMP_DATETIME_TZ
: MYSQL_TIMESTAMP_DATETIME));
if (number_of_fields < 3 || check_datetime_range(*l_time)) {
/* Only give warning for a zero date if there is some garbage after */
if (!not_zero_date) /* If zero date */
{
for (; str != end; str++) {
if (!isspace_char(*str)) {
not_zero_date = 1; /* Give warning */
break;
}
}
}
status->warnings |=
not_zero_date ? MYSQL_TIME_WARN_TRUNCATED : MYSQL_TIME_WARN_ZERO_DATE;
goto err;
}
if (check_date(*l_time, not_zero_date != 0, flags, &status->warnings))
goto err;
/* Scan all digits left after microseconds */
if (status->fractional_digits == 6 && str != end) {
if (isdigit_char(*str)) {
/*
We don't need the exact nanoseconds value.
Knowing the first digit is enough for rounding.
*/
status->nanoseconds = 100 * (*str++ - '0');
for (; str != end && isdigit_char(*str); str++) {
}
}
}
if (str != end && (str[0] == '+' || str[0] == '-')) {
if (time_zone_displacement_to_seconds(str, end - str, &displacement)) {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
} else {
l_time->time_type = MYSQL_TIMESTAMP_DATETIME_TZ;
l_time->time_zone_displacement = displacement;
return false;
}
}
for (; str != end; str++) {
if (!isspace_char(*str)) {
status->warnings = MYSQL_TIME_WARN_TRUNCATED;
break;
}
// superfluous space at end
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, str);
}
return false;
err:
set_zero_time(l_time, MYSQL_TIMESTAMP_ERROR);
return true;
}
/**
Convert a time string to a MYSQL_TIME struct.
status.warning is set to:
MYSQL_TIME_WARN_TRUNCATED flag if the input string
was cut during conversion, and/or
MYSQL_TIME_WARN_OUT_OF_RANGE flag, if the value is out of range.
@note
Because of the extra days argument, this function can only
work with times where the time arguments are in the above order.
@param str A string in full TIMESTAMP format or
[-] DAYS [H]H:MM:SS, [H]H:MM:SS, [M]M:SS, [H]HMMSS,
[M]MSS or [S]S
@param length Length of str
@param[out] l_time Store result here
@param[out] status Conversion status, including warnings.
@param flags Optional flags to control conversion
@retval false Ok
@retval true Error
*/
bool str_to_time(const char *str, std::size_t length, MYSQL_TIME *l_time,
MYSQL_TIME_STATUS *status, my_time_flags_t flags) {
ulong date[5];
ulonglong value;
const char *end = str + length;
const char *end_of_days;
bool found_days;
bool found_hours;
uint state;
const char *start;
bool seen_colon = false;
const char *str_arg = str;
assert(status->warnings == 0 && status->fractional_digits == 0 &&
status->nanoseconds == 0);
l_time->time_type = MYSQL_TIMESTAMP_NONE;
l_time->neg = false;
for (; str != end && isspace_char(*str); str++) {
length--;
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, str);
}
if (str != end && *str == '-') {
l_time->neg = true;
str++;
length--;
}
if (str == end) return true;
// Remember beginning of first non-space/- char.
start = str;
/* Check first if this is a full TIMESTAMP */
if (length >= 12) { /* Probably full timestamp */
MYSQL_TIME_STATUS tmpstatus;
(void)str_to_datetime(str, length, l_time,
(TIME_FUZZY_DATE | TIME_DATETIME_ONLY), &tmpstatus);
if (l_time->time_type >= MYSQL_TIMESTAMP_ERROR) {
*status = tmpstatus;
const bool error = l_time->time_type == MYSQL_TIMESTAMP_ERROR;
if (error) status->squelch_deprecation();
return error;
}
assert(status->warnings == 0 && status->fractional_digits == 0 &&
status->nanoseconds == 0);
}
/* Not a timestamp. Try to get this as a DAYS_TO_SECOND string */
for (value = 0; str != end && isdigit_char(*str); str++)
value = value * 10L + static_cast<long>(*str - '0');
if (value > UINT_MAX) return true;
/* Skip all space after 'days' */
end_of_days = str;
int spaces = 0;
for (; str != end && isspace_char(str[0]); str++) spaces++;
if (spaces > 1 || (spaces == 1 && str == end)) {
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, end_of_days);
}
state = 0;
found_days = found_hours = false;
if (static_cast<uint>(end - str) > 1 && str != end_of_days &&
isdigit_char(*str)) { /* Found days part */
date[0] = static_cast<ulong>(value);
state = 1; /* Assume next is hours */
found_days = true;
} else if ((end - str) > 1 && *str == time_separator &&
isdigit_char(str[1])) {
date[0] = 0; /* Assume we found hours */
date[1] = static_cast<ulong>(value);
state = 2;
found_hours = true;
str++; /* skip ':' */
seen_colon = true;
} else {
/* String given as one number; assume HHMMSS format */
date[0] = 0;
date[1] = static_cast<ulong>(value / 10000);
date[2] = static_cast<ulong>(value / 100 % 100);
date[3] = static_cast<ulong>(value % 100);
state = 4;
goto fractional;
}
/* Read hours, minutes and seconds */
for (;;) {
for (value = 0; str != end && isdigit_char(*str); str++)
value = value * 10L + static_cast<long>(*str - '0');
date[state++] = static_cast<ulong>(value);
if (state == 4 || (end - str) < 2 || *str != time_separator ||
!isdigit_char(str[1]))
break;
str++; /* Skip time_separator (':') */
seen_colon = true;
}
if (state != 4) { /* Not HH:MM:SS */
/* Fix the date to assume that seconds was given */
if (!found_hours && !found_days) {
std::size_t len = sizeof(long) * (state - 1);
memmove(pointer_cast<uchar *>(date + 4) - len,
pointer_cast<uchar *>(date + state) - len, len);
memset(date, 0, sizeof(long) * (4 - state));
} else
memset((date + state), 0, sizeof(long) * (4 - state));
}
fractional:
/* Get fractional second part */
if ((end - str) >= 2 && *str == '.' && isdigit_char(str[1])) {
int field_length = 5;
str++;
value = static_cast<uint>(static_cast<uchar>(*str - '0'));
while (++str != end && isdigit_char(*str)) {
if (field_length-- > 0)
value = value * 10 + static_cast<uint>(static_cast<uchar>(*str - '0'));
}
if (field_length >= 0) {
status->fractional_digits = DATETIME_MAX_DECIMALS - field_length;
if (field_length > 0)
value *= static_cast<long>(log_10_int[field_length]);
} else {
/* Scan digits left after microseconds */
status->fractional_digits = 6;
status->nanoseconds = 100 * (str[-1] - '0');
for (; str != end && isdigit_char(*str); str++) {
}
}
date[4] = static_cast<ulong>(value);
} else if ((end - str) == 1 && *str == '.') {
str++;
date[4] = 0;
} else
date[4] = 0;
/* Check for exponent part: E<gigit> | E<sign><digit> */
/* (may occur as result of %g formatting of time value) */
if ((end - str) > 1 && (*str == 'e' || *str == 'E') &&
(isdigit_char(str[1]) || ((str[1] == '-' || str[1] == '+') &&
(end - str) > 2 && isdigit_char(str[2]))))
return true;
/* Integer overflow checks */
if (date[0] > UINT_MAX || date[1] > UINT_MAX || date[2] > UINT_MAX ||
date[3] > UINT_MAX || date[4] > UINT_MAX)
return true;
if (!seen_colon && (flags & TIME_STRICT_COLON)) {
memset(l_time, 0, sizeof(*l_time));
status->warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
return true;
}
l_time->year = 0; /* For protocol::store_time */
l_time->month = 0;
l_time->day = 0;
l_time->hour = date[1] + date[0] * 24; /* Mix days and hours */
l_time->minute = date[2];
l_time->second = date[3];
l_time->second_part = date[4];
l_time->time_type = MYSQL_TIMESTAMP_TIME;
l_time->time_zone_displacement = 0;
if (check_time_mmssff_range(*l_time)) {
status->warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
l_time->time_type = MYSQL_TIMESTAMP_ERROR;
return true;
}
/* Adjust the value into supported MYSQL_TIME range */
adjust_time_range(l_time, &status->warnings);
/* Check if there is garbage at end of the MYSQL_TIME specification */
if (str != end) {
do {
if (!isspace_char(*str)) {
status->warnings |= MYSQL_TIME_WARN_TRUNCATED;
// No char was actually used in conversion - bad value
if (str == start) {
l_time->time_type = MYSQL_TIMESTAMP_NONE;
return true;
}
break;
} else {
status->set_deprecation(MYSQL_TIME_STATUS::DEPRECATION::DP_SUPERFLUOUS,
str_arg, end, str);
}
} while (++str != end);
}
return false;
}
/**
Convert number to TIME
@param nr Number to convert.
@param [out] ltime Variable to convert to.
@param [out] warnings Warning vector.
@retval false OK
@retval true No. is out of range
*/
bool number_to_time(longlong nr, MYSQL_TIME *ltime, int *warnings) {
if (nr > TIME_MAX_VALUE) {
/* For huge numbers try full DATETIME, like str_to_time does. */
if (nr >= 10000000000LL) /* '0001-00-00 00-00-00' */
{
int warnings_backup = *warnings;
if (number_to_datetime(nr, ltime, 0, warnings) != -1LL) return false;
*warnings = warnings_backup;
}
set_max_time(ltime, false);
*warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
return true;
} else if (nr < -TIME_MAX_VALUE) {
set_max_time(ltime, true);
*warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
return true;
}
if ((ltime->neg = (nr < 0))) nr = -nr;
if (nr % 100 >= 60 || nr / 100 % 100 >= 60) /* Check hours and minutes */
{
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
*warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
return true;
}
ltime->time_type = MYSQL_TIMESTAMP_TIME;
ltime->year = ltime->month = ltime->day = 0;
TIME_set_hhmmss(ltime, static_cast<uint>(nr));
ltime->second_part = 0;
return false;
}
/**
Adjust 'time' value to lie in the MYSQL_TIME range.
If the time value lies outside of the range [-838:59:59, 838:59:59],
set it to the closest endpoint of the range and set
MYSQL_TIME_WARN_OUT_OF_RANGE flag in the 'warning' variable.
@param[in,out] my_time pointer to MYSQL_TIME value
@param[out] warning set MYSQL_TIME_WARN_OUT_OF_RANGE flag if the value is
out of range
*/
void adjust_time_range(MYSQL_TIME *my_time, int *warning) {
assert(!check_time_mmssff_range(*my_time));
if (check_time_range_quick(*my_time)) {
my_time->day = my_time->second_part = 0;
set_max_hhmmss(my_time);
*warning |= MYSQL_TIME_WARN_OUT_OF_RANGE;
}
}
/**
Prepare offset of system time zone from UTC for my_system_gmt_sec() func.
*/
void my_init_time() {
time_t seconds;
struct tm *l_time;
struct tm tm_tmp;