- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmy_time.h
654 lines (545 loc) · 23.5 KB
/
my_time.h
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
/* Copyright (c) 2004, 2025, 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 */
#ifndef MY_TIME_INCLUDED
#defineMY_TIME_INCLUDED
/**
@ingroup MY_TIME
@{
@file include/my_time.h
Interface for low level time utilities.
*/
#include"my_config.h"
#include<assert.h>// assert
#include<algorithm>
#include<cstddef>// std::size_t
#include<cstdint>// std::int32_t
#include<cstring>// strncpy
#include<limits>// std::numeric_limits
#ifdef HAVE_SYS_TIME_H
#include<sys/time.h>// struct timeval
#endif/* HAVE_SYS_TIME_H */
#ifdef _WIN32
#include<winsock2.h>// struct timeval
#endif/* _WIN32 */
#include"field_types.h"
#include"my_time_t.h"
#include"mysql_time.h"// struct MYSQL_TIME, shared with client code
externconstunsignedlonglongint log_10_int[20];
externconstunsignedchar days_in_month[];
externconstchar my_zero_datetime6[]; /* "0000-00-00 00:00:00.000000" */
constexprconstbool HAVE_64_BITS_TIME_T = sizeof(time_t) == sizeof(my_time_t);
/** Time handling defaults */
constexprconstint MYTIME_MAX_YEAR = HAVE_64_BITS_TIME_T ? 9999 : 2038;
constexprconstint TIMESTAMP_MAX_YEAR = 2038;
/** Two-digit years < this are 20XX; >= this are 19XX */
constexprconstint YY_PART_YEAR = 70;
constexprconstint MYTIME_MIN_YEAR = (1900 + YY_PART_YEAR - 1);
/** max seconds from epoch of host's time_t stored in my_time_t
Windows allows up to 3001-01-18 23:59:59 UTC for localtime_r, so
that is our effective limit, although Unixen allow higher time points.
Hence the magic constant 32536771199.
*/
constexprconstmy_time_t MYTIME_MAX_VALUE =
HAVE_64_BITS_TIME_T ? 32536771199
: std::numeric_limits<std::int32_t>::max();
/**
Zero represents the first time value we allow, i.e. UNIX epoch.
We do not allow times before UNIX epoch, except for inside computations,
hence we use a signed integer as the base type, cf. prepare_tz_info.
*/
constexprconstint MYTIME_MIN_VALUE = 0;
/** max seconds from epoch that can be stored in a column of type TIMESTAMP.
This also impacts the max value that can be given to SET TIMESTAMP
*/
constexprconst std::int64_t TYPE_TIMESTAMP_MAX_VALUE =
std::numeric_limits<std::int32_t>::max();
constexprconst std::int64_t TYPE_TIMESTAMP_MIN_VALUE = 1;
/** Flags to str_to_datetime and number_to_datetime */
usingmy_time_flags_t = unsignedint;
/** Allow zero day and zero month */
constexprconstmy_time_flags_t TIME_FUZZY_DATE = 1;
/** Only allow full datetimes. */
constexprconstmy_time_flags_t TIME_DATETIME_ONLY = 2;
constexprconstmy_time_flags_t TIME_FRAC_TRUNCATE = 4;
constexprconstmy_time_flags_t TIME_NO_DATE_FRAC_WARN = 8;
/** Don't allow zero day or zero month */
constexprconstmy_time_flags_t TIME_NO_ZERO_IN_DATE = 16;
/** Don't allow 0000-00-00 date */
constexprconstmy_time_flags_t TIME_NO_ZERO_DATE = 32;
/** Allow 2000-02-31 */
constexprconstmy_time_flags_t TIME_INVALID_DATES = 64;
/** Allow only HH:MM:SS or MM:SS time formats */
constexprconstmy_time_flags_t TIME_STRICT_COLON = 128;
/** Conversion warnings */
constexprconstint MYSQL_TIME_WARN_TRUNCATED = 1;
constexprconstint MYSQL_TIME_WARN_OUT_OF_RANGE = 2;
constexprconstint MYSQL_TIME_WARN_INVALID_TIMESTAMP = 4;
constexprconstint MYSQL_TIME_WARN_ZERO_DATE = 8;
constexprconstint MYSQL_TIME_NOTE_TRUNCATED = 16;
constexprconstint MYSQL_TIME_WARN_ZERO_IN_DATE = 32;
constexprconstint MYSQL_TIME_WARN_DATETIME_OVERFLOW = 64;
/** Useful constants */
constexprconstint64_t SECONDS_IN_24H = 86400LL;
/** Limits for the TIME data type */
constexprconstint TIME_MAX_HOUR = 838;
constexprconstint TIME_MAX_MINUTE = 59;
constexprconstint TIME_MAX_SECOND = 59;
/**
Note that this MUST be a signed type, as we use the unary - operator on it.
*/
constexprconstint TIME_MAX_VALUE =
TIME_MAX_HOUR * 10000 + TIME_MAX_MINUTE * 100 + TIME_MAX_SECOND;
constexprconstint TIME_MAX_VALUE_SECONDS =
TIME_MAX_HOUR * 3600 + TIME_MAX_MINUTE * 60 + TIME_MAX_SECOND;
constexprconstint DATETIME_MAX_DECIMALS = 6;
constexprconstint SECS_PER_MIN = 60;
constexprconstint MINS_PER_HOUR = 60;
constexprconstint HOURS_PER_DAY = 24;
constexprconstint DAYS_PER_WEEK = 7;
constexprconstint DAYS_PER_NYEAR = 365;
constexprconstint DAYS_PER_LYEAR = 366;
constexprconstint SECS_PER_HOUR = (SECS_PER_MIN * MINS_PER_HOUR);
constexprconstint SECS_PER_DAY = (SECS_PER_HOUR * HOURS_PER_DAY);
constexprconstint MONS_PER_YEAR = 12;
constexprconstint MAX_TIME_ZONE_HOURS = 14;
/** Flags for calc_week() function. */
constexprconstunsignedint WEEK_MONDAY_FIRST = 1;
constexprconstunsignedint WEEK_YEAR = 2;
constexprconstunsignedint WEEK_FIRST_WEEKDAY = 4;
/** Daynumber from year 0 to 9999-12-31 */
constexprconstint64_t MAX_DAY_NUMBER = 3652424;
/**
Structure to return status from
str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
@note Implicit default constructor initializes all members to 0.
*/
structMYSQL_TIME_STATUS {
int warnings{0};
unsignedint fractional_digits{0};
unsignedint nanoseconds{0};
structDEPRECATION { // We only report first offense
enum DEPR_KIND {
DP_NONE, // no deprecated delimiter seen yet
DP_WRONG_KIND, // seen a delimiter in correct position, but wrong one
DP_WRONG_SPACE, // seen a space delimiter which isn't 0x20 ' '.
DP_SUPERFLUOUS // seen a superfluous delimiter
} m_kind{DP_NONE};
char m_delim_seen;
bool m_colon; // for DP_WRONG_KIND: true if we expect ':', else '-'
int m_position; // 0-based in m_arg
char m_arg[40]; // the string argument we found a deprecation in
} m_deprecation;
///< Register wrong delimiter if it's the first we see for this value
///< @param kind what kind of deprecation did we see
///< @param arg the string we try to interpret as a datetime value
///< @param end points to the character after arg, usually a '\0'
///< @param delim what delimiter was used
///< @param colon used if kind==DP_WRONG_KIND. true: expect ':' else expect'-'
voidset_deprecation(DEPRECATION::DEPR_KIND kind, constchar *arg,
constchar *end, constchar *delim, bool colon = false) {
if (m_deprecation.m_kind == DEPRECATION::DP_NONE) {
m_deprecation.m_kind = kind;
m_deprecation.m_delim_seen = *delim;
m_deprecation.m_colon = colon;
constsize_t bufsize = sizeof(m_deprecation.m_arg) - 1; // -1: for '\0'
constsize_t argsize = end - arg;
constsize_t size = std::min(bufsize, argsize);
// The input string is not necessarily zero-terminated,
// so do not use snprintf().
std::strncpy(m_deprecation.m_arg, arg, size);
m_deprecation.m_arg[size] = '\0';
m_deprecation.m_position = delim - arg;
}
}
MYSQL_TIME_STATUS() = default;
MYSQL_TIME_STATUS(const MYSQL_TIME_STATUS &) = default;
/// Assignment: don't clobber an existing deprecation, first one wins
MYSQL_TIME_STATUS &operator=(const MYSQL_TIME_STATUS &b) {
warnings = b.warnings;
fractional_digits = b.fractional_digits;
nanoseconds = b.nanoseconds;
// keep first deprecation
if (m_deprecation.m_kind == DEPRECATION::DP_NONE) {
m_deprecation = b.m_deprecation;
}
return *this;
}
voidsquelch_deprecation() { m_deprecation.m_kind = DEPRECATION::DP_NONE; }
};
/**
Struct representing a duration. Content is similar to MYSQL_TIME
but member variables are unsigned.
*/
structInterval {
unsignedlongint year;
unsignedlongint month;
unsignedlongint day;
unsignedlongint hour;
unsignedlonglongint minute;
unsignedlonglongint second;
unsignedlonglongint second_part;
bool neg;
};
voidmy_init_time();
longcalc_daynr(unsignedint year, unsignedint month, unsignedint day);
unsignedintcalc_days_in_year(unsignedint year);
unsignedintyear_2000_handling(unsignedint year);
booltime_zone_displacement_to_seconds(constchar *str, size_t length,
int *result);
voidget_date_from_daynr(int64_t daynr, unsignedint *year, unsignedint *month,
unsignedint *day);
intcalc_weekday(long daynr, bool sunday_first_day_of_week);
boolvalid_period(longlongint period);
uint64_tconvert_period_to_month(uint64_t period);
uint64_tconvert_month_to_period(uint64_t month);
/**
Check for valid my_time_t value. Note: timestamp here pertains to seconds
since epoch, not the legacy MySQL type TIMESTAMP, which is limited to
32 bits even on 64 bit platforms.
*/
inlineboolis_time_t_valid_for_timestamp(time_t x) {
return (static_cast<int64_t>(x) <= static_cast<int64_t>(MYTIME_MAX_VALUE) &&
x >= MYTIME_MIN_VALUE);
}
unsignedintcalc_week(const MYSQL_TIME &l_time, unsignedint week_behaviour,
unsignedint *year);
boolcheck_date(const MYSQL_TIME <ime, bool not_zero_date,
my_time_flags_t flags, int *was_cut);
boolstr_to_datetime(constchar *str, std::size_t length, MYSQL_TIME *l_time,
my_time_flags_t flags, MYSQL_TIME_STATUS *status);
longlongintnumber_to_datetime(longlongint nr, MYSQL_TIME *time_res,
my_time_flags_t flags, int *was_cut);
boolnumber_to_time(longlongint nr, MYSQL_TIME *ltime, int *warnings);
unsignedlonglongintTIME_to_ulonglong_datetime(const MYSQL_TIME &my_time);
unsignedlonglongintTIME_to_ulonglong_date(const MYSQL_TIME &my_time);
unsignedlonglongintTIME_to_ulonglong_time(const MYSQL_TIME &my_time);
unsignedlonglongintTIME_to_ulonglong(const MYSQL_TIME &my_time);
unsignedlonglongintTIME_to_ulonglong_datetime_round(
const MYSQL_TIME &my_time, int *warnings);
unsignedlonglongintTIME_to_ulonglong_time_round(const MYSQL_TIME &my_time);
/**
Round any MYSQL_TIME timepoint and convert to ulonglong.
@param my_time input
@param[out] warnings warning vector
@return time point as ulonglong
*/
inlineunsignedlonglongintTIME_to_ulonglong_round(const MYSQL_TIME &my_time,
int *warnings) {
switch (my_time.time_type) {
case MYSQL_TIMESTAMP_TIME:
returnTIME_to_ulonglong_time_round(my_time);
case MYSQL_TIMESTAMP_DATETIME:
returnTIME_to_ulonglong_datetime_round(my_time, warnings);
case MYSQL_TIMESTAMP_DATE:
returnTIME_to_ulonglong_date(my_time);
default:
assert(false);
return0;
}
}
/**
Extract the microsecond part of a MYSQL_TIME struct as an n *
(1/10^6) fraction as a double.
@return microseconds part as double
*/
inlinedoubleTIME_microseconds(const MYSQL_TIME &my_time) {
returnstatic_cast<double>(my_time.second_part) / 1000000.0;
}
/**
Convert a MYSQL_TIME datetime to double where the integral part is
the timepoint as an ulonglong, and the fractional part is the
fraction of the second.
@param my_time datetime to convert
@return datetime as double
*/
inlinedoubleTIME_to_double_datetime(const MYSQL_TIME &my_time) {
returnstatic_cast<double>(TIME_to_ulonglong_datetime(my_time)) +
TIME_microseconds(my_time);
}
/**
Convert a MYSQL_TIME time to double where the integral part is
the timepoint as an ulonglong, and the fractional part is the
fraction of the second.
@param my_time time to convert
@return datetime as double
*/
inlinedoubleTIME_to_double_time(const MYSQL_TIME &my_time) {
returnstatic_cast<double>(TIME_to_ulonglong_time(my_time)) +
TIME_microseconds(my_time);
}
/**
Convert a MYSQL_TIME to double where the integral part is the
timepoint as an ulonglong, and the fractional part is the fraction
of the second. The actual time type is extracted from
MYSQL_TIME::time_type.
@param my_time MYSQL_TIME to convert
@return MYSQL_TIME as double
*/
inlinedoubleTIME_to_double(const MYSQL_TIME &my_time) {
returnstatic_cast<double>(TIME_to_ulonglong(my_time)) +
TIME_microseconds(my_time);
}
/**
Return the fraction of the second as the number of microseconds.
@param i timepoint as longlong
@return frational part of an timepoint represented as an (u)longlong
*/
inlinelonglongintmy_packed_time_get_frac_part(longlongint i) {
return (i % (1LL << 24));
}
longlongintyear_to_longlong_datetime_packed(long year);
longlongintTIME_to_longlong_datetime_packed(const MYSQL_TIME &my_time);
longlongintTIME_to_longlong_date_packed(const MYSQL_TIME &my_time);
longlongintTIME_to_longlong_time_packed(const MYSQL_TIME &my_time);
longlongintTIME_to_longlong_packed(const MYSQL_TIME &my_time);
voidTIME_from_longlong_datetime_packed(MYSQL_TIME *ltime, longlongint nr);
voidTIME_from_longlong_time_packed(MYSQL_TIME *ltime, longlongint nr);
voidTIME_from_longlong_date_packed(MYSQL_TIME *ltime, longlongint nr);
voidTIME_set_yymmdd(MYSQL_TIME *ltime, unsignedint yymmdd);
voidTIME_set_hhmmss(MYSQL_TIME *ltime, unsignedint hhmmss);
voidmy_datetime_packed_to_binary(longlongint nr, unsignedchar *ptr,
unsignedint dec);
longlongintmy_datetime_packed_from_binary(constunsignedchar *ptr,
unsignedint dec);
voidmy_time_packed_to_binary(longlongint nr, unsignedchar *ptr,
unsignedint dec);
longlongintmy_time_packed_from_binary(constunsignedchar *ptr,
unsignedint dec);
voidmy_timestamp_to_binary(const my_timeval *tm, unsignedchar *ptr,
unsignedint dec);
voidmy_timestamp_from_binary(my_timeval *tm, constunsignedchar *ptr,
unsignedint dec);
boolstr_to_time(constchar *str, std::size_t length, MYSQL_TIME *l_time,
MYSQL_TIME_STATUS *status, my_time_flags_t flags = 0);
boolcheck_time_mmssff_range(const MYSQL_TIME &my_time);
boolcheck_time_range_quick(const MYSQL_TIME &my_time);
boolcheck_datetime_range(const MYSQL_TIME &my_time);
voidadjust_time_range(MYSQL_TIME *, int *warning);
/**
Check whether the argument holds a valid UNIX time value
(seconds after epoch). This function doesn't make precise check, but rather a
rough estimate before time zone adjustments.
@param my_time timepoint to check
@returns true if value satisfies the check above, false otherwise.
*/
inlineboolvalidate_my_time(const MYSQL_TIME &my_time) {
if (my_time.year < MYTIME_MIN_YEAR || my_time.year > MYTIME_MAX_YEAR)
returnfalse;
returntrue;
}
my_time_tmy_system_gmt_sec(const MYSQL_TIME &my_time, my_time_t *my_timezone,
bool *in_dst_time_gap);
voidset_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type);
voidset_max_time(MYSQL_TIME *tm, bool neg);
voidset_max_hhmmss(MYSQL_TIME *tm);
/**
Required buffer length for my_time_to_str, my_date_to_str,
my_datetime_to_str and TIME_to_string functions. Note, that the
caller is still responsible to check that given TIME structure
has values in valid ranges, otherwise size of the buffer might
well be insufficient. We also rely on the fact that even incorrect values
sent using binary protocol fit in this buffer.
*/
constexprconst std::size_t MAX_DATE_STRING_REP_LENGTH =
sizeof("YYYY-MM-DD AM HH:MM:SS.FFFFFF+HH:MM");
intmy_time_to_str(const MYSQL_TIME &my_time, char *to, unsignedint dec);
intmy_date_to_str(const MYSQL_TIME &my_time, char *to);
intmy_datetime_to_str(const MYSQL_TIME &my_time, char *to, unsignedint dec);
intmy_TIME_to_str(const MYSQL_TIME &my_time, char *to, unsignedint dec);
voidmy_date_to_binary(const MYSQL_TIME *ltime, unsignedchar *ptr);
intmy_timeval_to_str(const my_timeval *tm, char *to, unsignedint dec);
/**
Available interval types used in any statement.
'interval_type' must be sorted so that simple intervals comes first,
ie year, quarter, month, week, day, hour, etc. The order based on
interval size is also important and the intervals should be kept in a
large to smaller order. (get_interval_value() depends on this)
@note If you change the order of elements in this enum you should fix
order of elements in 'interval_type_to_name' and 'interval_names'
arrays
@see interval_type_to_name, get_interval_value, interval_names
*/
enum interval_type {
INTERVAL_YEAR,
INTERVAL_QUARTER,
INTERVAL_MONTH,
INTERVAL_WEEK,
INTERVAL_DAY,
INTERVAL_HOUR,
INTERVAL_MINUTE,
INTERVAL_SECOND,
INTERVAL_MICROSECOND,
INTERVAL_YEAR_MONTH,
INTERVAL_DAY_HOUR,
INTERVAL_DAY_MINUTE,
INTERVAL_DAY_SECOND,
INTERVAL_HOUR_MINUTE,
INTERVAL_HOUR_SECOND,
INTERVAL_MINUTE_SECOND,
INTERVAL_DAY_MICROSECOND,
INTERVAL_HOUR_MICROSECOND,
INTERVAL_MINUTE_MICROSECOND,
INTERVAL_SECOND_MICROSECOND,
INTERVAL_LAST
};
booldate_add_interval(MYSQL_TIME *ltime, interval_type int_type,
Interval interval, int *warnings);
/**
Round the input argument to the specified precision by computing
the remainder modulo log10 of the difference between max and
desired precision.
@param nr number to round
@param decimals desired precision
@return nr rounded according to the desired precision.
*/
inlinelongmy_time_fraction_remainder(long nr, unsignedint decimals) {
assert(decimals <= DATETIME_MAX_DECIMALS);
return nr % static_cast<long>(log_10_int[DATETIME_MAX_DECIMALS - decimals]);
}
/**
Truncate the number of microseconds in MYSQL_TIME::second_part to
the desired precision.
@param ltime time point
@param decimals desired precision
*/
inlinevoidmy_time_trunc(MYSQL_TIME *ltime, unsignedint decimals) {
ltime->second_part -=
my_time_fraction_remainder(ltime->second_part, decimals);
}
/**
Alias for my_time_trunc.
@param ltime time point
@param decimals desired precision
*/
inlinevoidmy_datetime_trunc(MYSQL_TIME *ltime, unsignedint decimals) {
returnmy_time_trunc(ltime, decimals);
}
/**
Truncate the tv_usec member of a posix timeval struct to the
specified number of decimals.
@param tv timepoint/duration
@param decimals desired precision
*/
inlinevoidmy_timeval_trunc(my_timeval *tv, unsignedint decimals) {
tv->m_tv_usec -= my_time_fraction_remainder(tv->m_tv_usec, decimals);
}
/**
Predicate for fuzzyness of date.
@param my_time time point to check
@param fuzzydate bitfield indicating if fuzzy dates are premitted
@retval true if TIME_FUZZY_DATE is unset and either month or day is 0
@retval false otherwise
*/
inlineboolcheck_fuzzy_date(const MYSQL_TIME &my_time,
my_time_flags_t fuzzydate) {
return !(fuzzydate & TIME_FUZZY_DATE) && (!my_time.month || !my_time.day);
}
/**
Predicate which returns true if at least one of the date members are non-zero.
@param my_time time point to check.
@retval false if all the date members are zero
@retval true otherwise
*/
inlineboolnon_zero_date(const MYSQL_TIME &my_time) {
return my_time.year || my_time.month || my_time.day;
}
/**
Predicate which returns true if at least one of the time members are non-zero.
@param my_time time point to check.
@retval false if all the time members are zero
@retval true otherwise
*/
inlineboolnon_zero_time(const MYSQL_TIME &my_time) {
return my_time.hour || my_time.minute || my_time.second ||
my_time.second_part;
}
/**
"Casts" MYSQL_TIME datetime to a MYSQL_TIME time. Sets
MYSQL_TIME::time_type to MYSQL_TIMESTAMP_TIME and zeroes out the
date members.
@param ltime timepoint to cast
*/
inlinevoiddatetime_to_time(MYSQL_TIME *ltime) {
ltime->year = 0;
ltime->month = 0;
ltime->day = 0;
ltime->time_type = MYSQL_TIMESTAMP_TIME;
}
/**
"Casts" MYSQL_TIME datetime to a MYSQL_TIME date. Sets
MYSQL_TIME::time_type to MYSQL_TIMESTAMP_DATE and zeroes out the
time members.
@param ltime timepoint to cast
*/
inlinevoiddatetime_to_date(MYSQL_TIME *ltime) {
ltime->hour = 0;
ltime->minute = 0;
ltime->second = 0;
ltime->second_part = 0;
ltime->time_type = MYSQL_TIMESTAMP_DATE;
}
/**
"Casts" a MYSQL_TIME to datetime by setting MYSQL_TIME::time_type to
MYSQL_TIMESTAMP_DATETIME.
@note There is no check to ensure that the result is a valid datetime.
@param ltime timpoint to cast
*/
inlinevoiddate_to_datetime(MYSQL_TIME *ltime) {
ltime->time_type = MYSQL_TIMESTAMP_DATETIME;
}
booltime_add_nanoseconds_with_truncate(MYSQL_TIME *ltime,
unsignedint nanoseconds,
int *warnings);
booldatetime_add_nanoseconds_with_truncate(MYSQL_TIME *ltime,
unsignedint nanoseconds);
booltime_add_nanoseconds_with_round(MYSQL_TIME *ltime,
unsignedint nanoseconds, int *warnings);
booldatetime_add_nanoseconds_with_round(MYSQL_TIME *ltime,
unsignedint nanoseconds,
int *warnings);
booltime_add_nanoseconds_adjust_frac(MYSQL_TIME *ltime,
unsignedint nanoseconds, int *warnings,
bool truncate);
booldatetime_add_nanoseconds_adjust_frac(MYSQL_TIME *ltime,
unsignedint nanoseconds,
int *warnings, bool truncate);
boolmy_time_adjust_frac(MYSQL_TIME *ltime, unsignedint dec, bool truncate);
boolmy_datetime_adjust_frac(MYSQL_TIME *ltime, unsignedint dec, int *warnings,
bool truncate);
boolmy_timeval_round(my_timeval *tv, unsignedint decimals);
voidmix_date_and_time(MYSQL_TIME *ldate, const MYSQL_TIME &my_time);
voidlocaltime_to_TIME(MYSQL_TIME *to, conststructtm *from);
voidcalc_time_from_sec(MYSQL_TIME *to, longlongint seconds,
long microseconds);
boolcalc_time_diff(const MYSQL_TIME &my_time1, const MYSQL_TIME &my_time2,
int l_sign, longlongint *seconds_out,
long *microseconds_out);
intmy_time_compare(const MYSQL_TIME &my_time_a, const MYSQL_TIME &my_time_b);
longlongintTIME_to_longlong_packed(const MYSQL_TIME &my_time,
enum enum_field_types type);
voidTIME_from_longlong_packed(MYSQL_TIME *ltime, enum enum_field_types type,
longlongint packed_value);
longlongintlonglong_from_datetime_packed(enum enum_field_types type,
longlongint packed_value);
doubledouble_from_datetime_packed(enum enum_field_types type,
longlongint packed_value);
/**
@} (end of ingroup MY_TIME)
*/
#endif/* MY_TIME_INCLUDED */