-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_time.cc
915 lines (790 loc) · 30 KB
/
sql_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
/* Copyright (c) 2000, 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.
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 SQL_TIME Server time functions
@ingroup Runtime_Environment
@{
@file sql/sql_time.cc
Implementation of server functions to handle date and time.
*/
#include "sql/sql_time.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "decimal.h"
#include "my_compiler.h"
#include "my_macros.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "nulls.h"
#include "sql-common/my_decimal.h"
#include "sql/current_thd.h"
#include "sql/derror.h"
#include "sql/field.h"
#include "sql/sql_class.h" // THD, MODE_STRICT_ALL_TABLES, MODE_STRICT_TRANS_TABLES
#include "sql/sql_const.h"
#include "sql/system_variables.h"
#include "sql/tztime.h" // struct Time_zone
#include "string_with_len.h"
/**
Name description of interval names used in statements.
'interval_type_to_name' is ordered and sorted on interval size and
interval complexity.
Order of elements in 'interval_type_to_name' should correspond to
the order of elements in 'interval_type' enum
@see interval_type, interval_names
*/
const LEX_CSTRING interval_type_to_name[INTERVAL_LAST] = {
{STRING_WITH_LEN("YEAR")},
{STRING_WITH_LEN("QUARTER")},
{STRING_WITH_LEN("MONTH")},
{STRING_WITH_LEN("WEEK")},
{STRING_WITH_LEN("DAY")},
{STRING_WITH_LEN("HOUR")},
{STRING_WITH_LEN("MINUTE")},
{STRING_WITH_LEN("SECOND")},
{STRING_WITH_LEN("MICROSECOND")},
{STRING_WITH_LEN("YEAR_MONTH")},
{STRING_WITH_LEN("DAY_HOUR")},
{STRING_WITH_LEN("DAY_MINUTE")},
{STRING_WITH_LEN("DAY_SECOND")},
{STRING_WITH_LEN("HOUR_MINUTE")},
{STRING_WITH_LEN("HOUR_SECOND")},
{STRING_WITH_LEN("MINUTE_SECOND")},
{STRING_WITH_LEN("DAY_MICROSECOND")},
{STRING_WITH_LEN("HOUR_MICROSECOND")},
{STRING_WITH_LEN("MINUTE_MICROSECOND")},
{STRING_WITH_LEN("SECOND_MICROSECOND")}};
/**
Generate flags to use when converting a string to a date or datetime value
@todo Consider whether to always accept zero date and zero in date.
(Add TIME_FUZZY_DATE).
Accept invalid dates when ALLOW_INVALID_DATES SQL mode is set.
Reject zero date if TIME_NO_ZERO_DATE is set.
Reject dates with zero as day or month when TIME_NO_ZERO_IN_DATE is set,
even if TIME_FUZZY_DATE is also set.
TIME_FRAC_TRUNCATE controls whether to round or truncate if the input
value has greater precision than the wanted result.
@param thd Thread handle
@returns Flags to use for calls to e.g my_str_to_datetime()
*/
my_time_flags_t DatetimeConversionFlags(const THD *thd) {
const sql_mode_t mode = thd->variables.sql_mode;
return (mode & MODE_INVALID_DATES ? TIME_INVALID_DATES : 0) |
(mode & MODE_NO_ZERO_DATE ? TIME_NO_ZERO_DATE : 0) |
(mode & MODE_NO_ZERO_IN_DATE ? TIME_NO_ZERO_IN_DATE : 0) |
(thd->is_fsp_truncate_mode() ? TIME_FRAC_TRUNCATE : 0);
}
/**
Convert a string to 8-bit representation,
for use in str_to_time/str_to_date/str_to_date.
In the future to_ascii() can be extended to convert
non-ASCII digits to ASCII digits
(for example, ARABIC-INDIC, DEVANAGARI, BENGALI, and so on)
so DATE/TIME/DATETIME values understand digits in the
respected scripts.
@return number of bytes written to dst
*/
static uint to_ascii(const CHARSET_INFO *cs, const char *src, size_t src_length,
char *dst, size_t dst_length) {
int cnvres;
my_wc_t wc;
const char *srcend = src + src_length;
char *dst0 = dst;
char *dstend = dst + dst_length - 1;
while (dst < dstend &&
(cnvres = (cs->cset->mb_wc)(cs, &wc, pointer_cast<const uchar *>(src),
pointer_cast<const uchar *>(srcend))) >
0 &&
wc < 128) {
src += cnvres;
*dst++ = static_cast<char>(wc);
}
*dst = '\0';
return dst - dst0;
}
/**
Character set-aware version of str_to_time().
@return False on success, true on error.
*/
bool str_to_time(const CHARSET_INFO *cs, const char *str, size_t length,
MYSQL_TIME *l_time, my_time_flags_t flags,
MYSQL_TIME_STATUS *status) {
char cnv[MAX_TIME_FULL_WIDTH + 3]; // +3 for nanoseconds (for rounding)
if (!my_charset_is_ascii_based(cs)) {
length = to_ascii(cs, str, length, cnv, sizeof(cnv));
str = cnv;
}
bool rc = str_to_time(str, length, l_time, status);
rc = rc || time_add_nanoseconds_adjust_frac(l_time, status->nanoseconds,
&status->warnings,
(flags & TIME_FRAC_TRUNCATE));
return rc;
}
/**
Character set-aware version of str_to_datetime().
@return False on success, true on error.
*/
bool str_to_datetime(const CHARSET_INFO *cs, const char *str, size_t length,
MYSQL_TIME *l_time, my_time_flags_t flags,
MYSQL_TIME_STATUS *status) {
char cnv[MAX_DATETIME_FULL_WIDTH + 3]; // +3 for nanoseconds (for rounding)
if (!my_charset_is_ascii_based(cs)) {
length = to_ascii(cs, str, length, cnv, sizeof(cnv));
str = cnv;
}
bool rc = str_to_datetime(str, length, l_time, flags, status);
rc = rc || datetime_add_nanoseconds_adjust_frac(l_time, status->nanoseconds,
&status->warnings,
flags & TIME_FRAC_TRUNCATE);
return rc;
}
/**
Convert a timestamp string to a MYSQL_TIME value and produce a warning
if string was truncated during conversion.
@note See description of str_to_datetime() for more information.
@note Uses current_thd
@return False on success, true on error.
*/
bool str_to_datetime_with_warn(String *str, MYSQL_TIME *l_time,
my_time_flags_t flags) {
MYSQL_TIME_STATUS status;
THD *thd = current_thd;
if (thd->variables.sql_mode & MODE_NO_ZERO_DATE) flags |= TIME_NO_ZERO_DATE;
if (thd->variables.sql_mode & MODE_INVALID_DATES) flags |= TIME_INVALID_DATES;
if (thd->is_fsp_truncate_mode()) flags |= TIME_FRAC_TRUNCATE;
bool ret_val = propagate_datetime_overflow(
thd, &status.warnings, str_to_datetime(str, l_time, flags, &status));
if (ret_val || status.warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(str), l_time->time_type,
NullS))
return true;
if (ret_val) status.squelch_deprecation();
}
check_deprecated_datetime_format(current_thd, str->charset(), status);
if (ret_val) return true;
return convert_time_zone_displacement(thd->time_zone(), l_time);
}
/**
Convert lldiv_t to datetime.
@param lld The value to convert from.
@param[out] ltime The variable to convert to.
@param flags Conversion flags.
@param[in,out] warnings Warning flags.
@return False on success, true on error.
*/
static bool lldiv_t_to_datetime(lldiv_t lld, MYSQL_TIME *ltime,
my_time_flags_t flags, int *warnings) {
if (lld.rem < 0 || // Catch negative numbers with zero int part, e.g: -0.1
number_to_datetime(lld.quot, ltime, flags, warnings) == -1LL) {
/* number_to_datetime does not clear ltime in case of ZERO DATE */
set_zero_time(ltime, MYSQL_TIMESTAMP_ERROR);
if (!*warnings) /* Neither sets warnings in case of ZERO DATE */
*warnings |= MYSQL_TIME_WARN_TRUNCATED;
return true;
}
if (ltime->time_type == MYSQL_TIMESTAMP_DATE) {
/*
Generate a warning in case of DATE with fractional part:
20011231.1234 -> '2001-12-31'
unless the caller does not want the warning: for example, CAST does.
*/
if (lld.rem && !(flags & TIME_NO_DATE_FRAC_WARN))
*warnings |= MYSQL_TIME_WARN_TRUNCATED;
} else {
ltime->second_part = static_cast<ulong>(lld.rem / 1000);
return datetime_add_nanoseconds_adjust_frac(ltime, lld.rem % 1000, warnings,
(flags & TIME_FRAC_TRUNCATE));
}
return false;
}
/**
Convert decimal value to datetime.
@param decimal The value to convert from.
@param[out] ltime The variable to convert to.
@param flags Conversion flags.
@returns false on success, true if not convertible to datetime.
*/
bool decimal_to_datetime(const my_decimal *decimal, MYSQL_TIME *ltime,
my_time_flags_t flags) {
lldiv_t lld;
if (my_decimal2lldiv_t(0, decimal, &lld)) {
return true;
}
int warnings = 0;
return lldiv_t_to_datetime(lld, ltime, flags, &warnings);
}
/**
Convert decimal value to datetime value with a warning.
@param decimal The value to convert from.
@param[out] ltime The variable to convert to.
@param flags Conversion flags.
@return False on success, true on error.
*/
bool my_decimal_to_datetime_with_warn(const my_decimal *decimal,
MYSQL_TIME *ltime,
my_time_flags_t flags) {
lldiv_t lld;
int warnings = 0;
bool rc;
if ((rc = my_decimal2lldiv_t(0, decimal, &lld))) {
warnings |= MYSQL_TIME_WARN_TRUNCATED;
set_zero_time(ltime, MYSQL_TIMESTAMP_NONE);
} else
rc = propagate_datetime_overflow(
current_thd, &warnings,
lldiv_t_to_datetime(lld, ltime, flags, &warnings));
if (warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(decimal), ltime->time_type,
NullS))
return true;
}
return rc;
}
/**
Convert double value to datetime.
@param nr The value to convert from.
@param[out] ltime The variable to convert to.
@param flags Conversion flags.
@returns false on success, true if not convertible to datetime.
*/
bool double_to_datetime(double nr, MYSQL_TIME *ltime, my_time_flags_t flags) {
lldiv_t lld;
if (double2lldiv_t(nr, &lld)) {
return true;
}
int warnings = 0;
return lldiv_t_to_datetime(lld, ltime, flags, &warnings);
}
/**
Convert double value to datetime value with a warning.
@param nr The value to convert from.
@param[out] ltime The variable to convert to.
@param flags Conversion flags.
@return False on success, true on error.
*/
bool my_double_to_datetime_with_warn(double nr, MYSQL_TIME *ltime,
my_time_flags_t flags) {
lldiv_t lld;
int warnings = 0;
bool rc;
if ((rc = (double2lldiv_t(nr, &lld) != E_DEC_OK))) {
warnings |= MYSQL_TIME_WARN_TRUNCATED;
set_zero_time(ltime, MYSQL_TIMESTAMP_NONE);
} else
rc = propagate_datetime_overflow(
current_thd, &warnings,
lldiv_t_to_datetime(lld, ltime, flags, &warnings));
if (warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(nr), ltime->time_type,
NullS))
return true;
}
return rc;
}
/**
Convert longlong value to datetime value with a warning.
@param nr The value to convert from.
@param[out] ltime The variable to convert to.
@param flags Conversion flags
@return False on success, true on error.
*/
bool my_longlong_to_datetime_with_warn(longlong nr, MYSQL_TIME *ltime,
my_time_flags_t flags) {
int warnings = 0;
bool rc = propagate_datetime_overflow(
current_thd, &warnings,
number_to_datetime(nr, ltime, flags, &warnings)) == -1LL;
if (warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(nr), MYSQL_TIMESTAMP_NONE,
NullS))
return true;
}
return rc;
}
/**
Convert lldiv_t value to time with nanosecond rounding.
@param lld The value to convert from.
@param[out] ltime The variable to convert to,
@param[in,out] warnings Warning flags.
@return False on success, true on error.
*/
static bool lldiv_t_to_time(lldiv_t lld, MYSQL_TIME *ltime, int *warnings) {
if (number_to_time(lld.quot, ltime, warnings)) return true;
/*
Both lld.quot and lld.rem can give negative result value,
thus combine them using "|=".
*/
if ((ltime->neg |= (lld.rem < 0))) lld.rem = -lld.rem;
ltime->second_part = static_cast<ulong>(lld.rem / 1000);
return time_add_nanoseconds_adjust_frac(ltime, lld.rem % 1000, warnings,
current_thd->is_fsp_truncate_mode());
}
/**
Convert decimal number to TIME
@param decimal The number to convert from.
@param[out] ltime The variable to convert to.
@returns false on success, true if not convertible to time.
*/
bool decimal_to_time(const my_decimal *decimal, MYSQL_TIME *ltime) {
lldiv_t lld;
int warnings = 0;
if (my_decimal2lldiv_t(0, decimal, &lld)) {
return true;
}
return lldiv_t_to_time(lld, ltime, &warnings);
}
/**
Convert decimal number to TIME
@param decimal The number to convert from.
@param[out] ltime The variable to convert to.
@return False on success, true on error.
*/
bool my_decimal_to_time_with_warn(const my_decimal *decimal,
MYSQL_TIME *ltime) {
lldiv_t lld;
int warnings = 0;
bool rc;
if ((rc = my_decimal2lldiv_t(0, decimal, &lld))) {
warnings |= MYSQL_TIME_WARN_TRUNCATED;
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
} else
rc = propagate_datetime_overflow(current_thd, &warnings,
lldiv_t_to_time(lld, ltime, &warnings));
if (warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(decimal),
MYSQL_TIMESTAMP_TIME, NullS))
return true;
}
return rc;
}
/**
Convert double number to TIME
@param nr The number to convert from.
@param[out] ltime The variable to convert to.
@returns false on success, true if not convertible to time.
*/
bool double_to_time(double nr, MYSQL_TIME *ltime) {
lldiv_t lld;
if (double2lldiv_t(nr, &lld) != E_DEC_OK) {
return true;
}
int warnings = 0;
return lldiv_t_to_time(lld, ltime, &warnings);
}
/**
Convert double number to TIME
@param nr The number to convert from.
@param[out] ltime The variable to convert to.
@return False on success, true on error.
*/
bool my_double_to_time_with_warn(double nr, MYSQL_TIME *ltime) {
lldiv_t lld;
int warnings = 0;
bool rc;
if ((rc = (double2lldiv_t(nr, &lld) != E_DEC_OK))) {
warnings |= MYSQL_TIME_WARN_TRUNCATED;
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
} else
rc = propagate_datetime_overflow(current_thd, &warnings,
lldiv_t_to_time(lld, ltime, &warnings));
if (warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(nr), MYSQL_TIMESTAMP_TIME,
NullS))
return true;
}
return rc;
}
/**
Convert longlong number to TIME
@param nr The number to convert from.
@param[out] ltime The variable to convert to.
@return False on success, true on error.
*/
bool my_longlong_to_time_with_warn(longlong nr, MYSQL_TIME *ltime) {
int warnings = 0;
bool rc = propagate_datetime_overflow(current_thd, &warnings,
number_to_time(nr, ltime, &warnings));
if (warnings) {
if (make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
ErrConvString(nr), MYSQL_TIMESTAMP_TIME,
NullS))
return true;
}
return rc;
}
/**
Converts a datetime in MYSQL_TIME representation to corresponding `struct
timeval` value.
`ltime` must be previously checked for `TIME_NO_ZERO_IN_DATE`.
Things like '0000-01-01', '2000-00-01', '2000-01-00' are not allowed
and asserted.
Things like '0000-00-00 10:30:30' or '0000-00-00 00:00:00.123456'
(i.e. empty date with non-empty time) return error.
Zero datetime '0000-00-00 00:00:00.000000' is allowed and is mapped to
{tv_sec=0, tv_usec=0}.
@note In case of error, tm value is not initialized.
@note `warnings` is not initialized to zero, so new warnings are added to the
old ones. The caller must make sure to initialize `warnings`.
@param[in] ltime Datetime value
@param[in] tz Time zone to convert to.
@param[out] tm Timeval value
@param[out] warnings Pointer to warnings.
@return False on success, true on error.
*/
bool datetime_with_no_zero_in_date_to_timeval(const MYSQL_TIME *ltime,
const Time_zone &tz,
my_timeval *tm, int *warnings) {
if (!ltime->month) /* Zero date */
{
assert(!ltime->year && !ltime->day);
if (non_zero_time(*ltime)) {
/*
Return error for zero date with non-zero time, e.g.:
'0000-00-00 10:20:30' or '0000-00-00 00:00:00.123456'
*/
*warnings |= MYSQL_TIME_WARN_TRUNCATED;
return true;
}
tm->m_tv_sec = tm->m_tv_usec = 0; // '0000-00-00 00:00:00.000000'
return false;
}
bool is_in_dst_time_gap = false;
if (!(tm->m_tv_sec = tz.TIME_to_gmt_sec(ltime, &is_in_dst_time_gap))) {
/*
Date was outside of the supported timestamp range.
For example: '3001-01-01 00:00:00' or '1000-01-01 00:00:00'
*/
*warnings |= MYSQL_TIME_WARN_OUT_OF_RANGE;
return true;
}
if (is_in_dst_time_gap) {
/*
Set MYSQL_TIME_WARN_INVALID_TIMESTAMP warning to indicate
that date was fine but pointed to winter/summer time switch gap.
In this case tm is set to the fist second after gap.
For example: '2003-03-30 02:30:00 MSK' -> '2003-03-30 03:00:00 MSK'
*/
*warnings |= MYSQL_TIME_WARN_INVALID_TIMESTAMP;
}
tm->m_tv_usec = ltime->second_part;
return false;
}
/**
Convert a datetime MYSQL_TIME representation
to corresponding "struct timeval" value.
Things like '0000-01-01', '2000-00-01', '2000-01-00'
(i.e. incomplete date) return error.
Things like '0000-00-00 10:30:30' or '0000-00-00 00:00:00.123456'
(i.e. empty date with non-empty time) return error.
Zero datetime '0000-00-00 00:00:00.000000'
is allowed and is mapper to {tv_sec=0, tv_usec=0}.
Note: In case of error, tm value is not initialized.
Note: "warnings" is not initialized to zero,
so new warnings are added to the old ones.
Caller must make sure to initialize "warnings".
@param[in] ltime datetime value
@param[in] tz The time zone.
@param[out] tm timeval value
@param[out] warnings pointer to warnings vector
@return False on success, true on error.
*/
bool datetime_to_timeval(const MYSQL_TIME *ltime, const Time_zone &tz,
my_timeval *tm, int *warnings) {
return check_date(*ltime, non_zero_date(*ltime), TIME_NO_ZERO_IN_DATE,
warnings) ||
datetime_with_no_zero_in_date_to_timeval(ltime, tz, tm, warnings);
}
/**
Convert a time string to a MYSQL_TIME struct and produce a warning
if string was cut during conversion.
@note See str_to_time() for more info.
@return False on success, true on error.
*/
bool str_to_time_with_warn(String *str, MYSQL_TIME *l_time) {
MYSQL_TIME_STATUS status;
my_time_flags_t flags = 0;
THD *thd = current_thd;
if (current_thd->is_fsp_truncate_mode()) flags = TIME_FRAC_TRUNCATE;
bool ret_val = propagate_datetime_overflow(
current_thd, &status.warnings, str_to_time(str, l_time, flags, &status));
if (ret_val || status.warnings) {
if (make_truncated_value_warning(thd, Sql_condition::SL_WARNING,
ErrConvString(str), MYSQL_TIMESTAMP_TIME,
NullS))
return true;
}
check_deprecated_datetime_format(current_thd, str->charset(), status);
if (!ret_val)
if (convert_time_zone_displacement(thd->time_zone(), l_time)) return true;
return ret_val;
}
/**
Convert time to datetime.
The time value is added to the current datetime value.
@param thd Thread context
@param [in] ltime Time value to convert from.
@param [out] ltime2 Datetime value to convert to.
*/
void time_to_datetime(THD *thd, const MYSQL_TIME *ltime, MYSQL_TIME *ltime2) {
thd->variables.time_zone->gmt_sec_to_TIME(
ltime2, static_cast<my_time_t>(thd->query_start_in_secs()));
ltime2->hour = ltime2->minute = ltime2->second = ltime2->second_part = 0;
ltime2->time_type = MYSQL_TIMESTAMP_DATE;
mix_date_and_time(ltime2, *ltime);
}
/**
Return format string according format name.
If name is unknown, result is NULL
@returns format string according format name.
@retval NULL if name is unknown.
*/
const char *get_date_time_format_str(const Known_date_time_format *format,
enum_mysql_timestamp_type type) {
switch (type) {
case MYSQL_TIMESTAMP_DATE:
return format->date_format;
case MYSQL_TIMESTAMP_DATETIME:
return format->datetime_format;
case MYSQL_TIMESTAMP_TIME:
return format->time_format;
default:
assert(0); // Impossible
return nullptr;
}
}
/**
@ingroup SQL_TIME
@page DEFAULT_TIME_FUNCS Functions to create default time/date/datetime
strings
@note
For the moment the Date_time_format argument is ignored because
MySQL doesn't support comparing of date/time/datetime strings that
are not in arbutary order as dates are compared as strings in some
context)
This functions don't check that given MYSQL_TIME structure members are
in valid range. If they are not, return value won't reflect any
valid date either. Additionally, make_time doesn't take into
account time->day member: it's assumed that days have been converted
to hours already.
*/
/**
Convert TIME value to String.
@param format Format (unused, see comments above)
@param l_time TIME value
@param[out] str String to convert to
@param dec Number of fractional digits.
*/
void make_time(const Date_time_format *format [[maybe_unused]],
const MYSQL_TIME *l_time, String *str, uint dec) {
const uint length =
static_cast<uint>(my_time_to_str(*l_time, str->ptr(), dec));
str->length(length);
str->set_charset(&my_charset_numeric);
}
/**
Convert DATE value to String.
@param format Format (unused, see comments above)
@param l_time DATE value
@param[out] str String to convert to
*/
void make_date(const Date_time_format *format [[maybe_unused]],
const MYSQL_TIME *l_time, String *str) {
const uint length = static_cast<uint>(my_date_to_str(*l_time, str->ptr()));
str->length(length);
str->set_charset(&my_charset_numeric);
}
/**
Convert DATETIME value to String.
@param format Format (unused, see comments above)
@param l_time DATE value
@param[out] str String to convert to
@param dec Number of fractional digits.
*/
void make_datetime(const Date_time_format *format [[maybe_unused]],
const MYSQL_TIME *l_time, String *str, uint dec) {
const uint length =
static_cast<uint>(my_datetime_to_str(*l_time, str->ptr(), dec));
str->length(length);
str->set_charset(&my_charset_numeric);
}
/**
Convert TIME/DATE/DATETIME value to String.
@param ltime DATE value
@param[out] str String to convert to
@param dec Number of fractional digits.
*/
bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec) {
if (str->alloc(MAX_DATE_STRING_REP_LENGTH)) return true;
str->set_charset(&my_charset_numeric);
str->length(my_TIME_to_str(*ltime, str->ptr(), dec));
return false;
}
/**
Create and add a truncated value warning to the THD.
@returns value of thd->is_error() after adding the warning
*/
bool make_truncated_value_warning(THD *thd,
Sql_condition::enum_severity_level level,
const ErrConvString &val,
enum_mysql_timestamp_type time_type,
const char *field_name) {
char warn_buff[MYSQL_ERRMSG_SIZE];
const char *type_str;
CHARSET_INFO *cs = system_charset_info;
switch (time_type) {
case MYSQL_TIMESTAMP_DATE:
type_str = "date";
break;
case MYSQL_TIMESTAMP_TIME:
type_str = "time";
break;
case MYSQL_TIMESTAMP_DATETIME: // FALLTHROUGH
default:
type_str = "datetime";
break;
}
if (field_name)
cs->cset->snprintf(
cs, warn_buff, sizeof(warn_buff),
ER_THD(thd, ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), type_str, val.ptr(),
field_name,
static_cast<long>(thd->get_stmt_da()->current_row_for_condition()));
else {
if (time_type > MYSQL_TIMESTAMP_ERROR)
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
ER_THD(thd, ER_TRUNCATED_WRONG_VALUE), type_str,
val.ptr());
else
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
ER_THD(thd, ER_WRONG_VALUE), type_str, val.ptr());
}
push_warning(thd, level, ER_TRUNCATED_WRONG_VALUE, warn_buff);
// strict mode can convert warning to error. Check for error while returning.
return thd->is_error();
}
/**
Uses propagate_datetime_overflow() to handle and propagate any warnings from
date_add_interval() to the THD.
@return False on success, true on error.
*/
bool date_add_interval_with_warn(THD *thd, MYSQL_TIME *ltime,
interval_type int_type, Interval interval) {
return propagate_datetime_overflow(thd, [&](int *w) {
return date_add_interval(ltime, int_type, interval, w);
});
}
/**
Propagates a DATETIME_OVERFLOW warning from warnings bitfield to DA in thd.
@param thd thread context
@param[in,out] warnings bitfield of warnings set
*/
void propagate_datetime_overflow_helper(THD *thd, int *warnings) {
if (warnings && (*warnings & MYSQL_TIME_WARN_DATETIME_OVERFLOW) != 0) {
push_warning_printf(thd, Sql_condition::SL_WARNING,
ER_DATETIME_FUNCTION_OVERFLOW,
ER_THD(thd, ER_DATETIME_FUNCTION_OVERFLOW), "datetime");
*warnings &= ~(MYSQL_TIME_WARN_DATETIME_OVERFLOW);
}
}
/**
Unpack packed numeric temporal value to date/time value
and then convert to decimal representation.
@param [out] dec The variable to write to.
@param type MySQL field type.
@param packed_value Packed numeric temporal representation.
@return A decimal value in on of the following formats, depending
on type: YYYYMMDD, hhmmss.ffffff or YYMMDDhhmmss.ffffff.
*/
my_decimal *my_decimal_from_datetime_packed(my_decimal *dec,
enum enum_field_types type,
longlong packed_value) {
MYSQL_TIME ltime;
switch (type) {
case MYSQL_TYPE_TIME:
TIME_from_longlong_time_packed(<ime, packed_value);
return time2my_decimal(<ime, dec);
case MYSQL_TYPE_DATE:
TIME_from_longlong_date_packed(<ime, packed_value);
ulonglong2decimal(TIME_to_ulonglong_date(ltime), dec);
return dec;
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
TIME_from_longlong_datetime_packed(<ime, packed_value);
return date2my_decimal(<ime, dec);
default:
assert(0);
ulonglong2decimal(0, dec);
return dec;
}
}
/**
This function gets GMT time and adds value of time_zone to get
the local time. This function is used when server wants a timestamp
value from dictionary system.
@param gmt_time GMT time value.
@return time as ulonglong
*/
ulonglong gmt_time_to_local_time(ulonglong gmt_time) {
MYSQL_TIME time;
bool not_used;
THD *thd = current_thd;
Time_zone *tz = thd->variables.time_zone;
// Convert longlong time to MYSQL_TIME format
my_longlong_to_datetime_with_warn(gmt_time, &time, MYF(0));
// Convert MYSQL_TIME to epoc second according to GMT time_zone.
my_time_t timestamp;
timestamp = my_tz_OFFSET0->TIME_to_gmt_sec(&time, ¬_used);
// Convert epoc seconds to local time
tz->gmt_sec_to_TIME(&time, timestamp);
// Return ulonglong value from MYSQL_TIME
return TIME_to_ulonglong_datetime(time);
}
MYSQL_TIME my_time_set(uint y, uint m, uint d, uint h, uint mi, uint s,
unsigned long ms, bool negative,
enum_mysql_timestamp_type type) {
return {y, m, d, h, mi, s, ms, negative, type, 0};
}
uint actual_decimals(const MYSQL_TIME *ts) {
uint count = DATETIME_MAX_DECIMALS;
for (int i = 1; i <= DATETIME_MAX_DECIMALS; i++) {
if (ts->second_part % log_10_int[i] != 0) break;
count--;
}
return count;
}
size_t max_fraction(uint decimals) {
size_t res = 0;
for (uint i = 1; i <= DATETIME_MAX_DECIMALS; i++) {
res *= 10;
if (i <= decimals) res += 9;
}
return res;
}
/**
@} (end of defgroup SQL_TIME)
*/