MySQL  8.0.16
Source Code Documentation
my_time.h
Go to the documentation of this file.
1 /* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef MY_TIME_INCLUDED
24 #define MY_TIME_INCLUDED
25 
26 /**
27  @ingroup MY_TIME
28  @{
29 
30  @file include/my_time.h
31 
32  Interface for low level time utilities.
33 */
34 
35 #include "my_config.h"
36 
37 #include <assert.h> // assert
38 #include <cstddef> // std::size_t
39 #include <cstdint> // std::int32_t
40 #include <limits> // std::numeric_limits
41 
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h> // struct timeval
44 #endif /* HAVE_SYS_TIME_H */
45 #ifdef _WIN32
46 #include <winsock2.h> // struct timeval
47 #endif /* _WIN32 */
48 
49 #include "mysql_time.h" // struct MYSQL_TIME, shared with client code
50 
51 enum enum_field_types : int;
52 
53 extern const unsigned long long int log_10_int[20];
54 extern const unsigned char days_in_month[];
55 extern const char my_zero_datetime6[]; /* "0000-00-00 00:00:00.000000" */
56 
57 /**
58  Portable time_t replacement.
59  Should be signed and hold seconds for 1902 -- 2038-01-19 range
60  i.e at least a 32bit variable
61 
62  Using the system built in time_t is not an option as
63  we rely on the above requirements in the time functions
64 */
65 using my_time_t = long int;
66 
67 constexpr const my_time_t MY_TIME_T_MAX = std::numeric_limits<my_time_t>::max();
68 constexpr const my_time_t MY_TIME_T_MIN = std::numeric_limits<my_time_t>::min();
69 
70 /** Time handling defaults */
71 constexpr const int TIMESTAMP_MAX_YEAR = 2038;
72 
73 /** Two-digit years < this are 20XX; >= this are 19XX */
74 constexpr const int YY_PART_YEAR = 70;
75 constexpr const int TIMESTAMP_MIN_YEAR = (1900 + YY_PART_YEAR - 1);
76 
77 constexpr const int TIMESTAMP_MAX_VALUE =
78  std::numeric_limits<std::int32_t>::max();
79 constexpr const int TIMESTAMP_MIN_VALUE = 1;
80 
81 /** Flags to str_to_datetime and number_to_datetime */
82 using my_time_flags_t = unsigned int;
83 
84 /** Allow zero day and zero month */
85 constexpr const my_time_flags_t TIME_FUZZY_DATE = 1;
86 
87 /** Only allow full datetimes. */
88 constexpr const my_time_flags_t TIME_DATETIME_ONLY = 2;
89 
90 constexpr const my_time_flags_t TIME_FRAC_TRUNCATE = 4;
92 
93 /** Don't allow zero day or zero month */
95 
96 /** Don't allow 0000-00-00 date */
97 constexpr const my_time_flags_t TIME_NO_ZERO_DATE = 32;
98 
99 /** Allow 2000-02-31 */
100 constexpr const my_time_flags_t TIME_INVALID_DATES = 64;
101 
102 /** Conversion warnings */
103 constexpr const int MYSQL_TIME_WARN_TRUNCATED = 1;
104 constexpr const int MYSQL_TIME_WARN_OUT_OF_RANGE = 2;
105 constexpr const int MYSQL_TIME_WARN_INVALID_TIMESTAMP = 4;
106 constexpr const int MYSQL_TIME_WARN_ZERO_DATE = 8;
107 constexpr const int MYSQL_TIME_NOTE_TRUNCATED = 16;
108 constexpr const int MYSQL_TIME_WARN_ZERO_IN_DATE = 32;
109 constexpr const int MYSQL_TIME_WARN_DATETIME_OVERFLOW = 64;
110 
111 /** Usefull constants */
112 constexpr const long int SECONDS_IN_24H = 86400L;
113 
114 /** Limits for the TIME data type */
115 constexpr const int TIME_MAX_HOUR = 838;
116 constexpr const int TIME_MAX_MINUTE = 59;
117 constexpr const int TIME_MAX_SECOND = 59;
118 
119 /**
120  Note that this MUST be a signed type, as we use the unary - operator on it.
121  */
122 constexpr const int TIME_MAX_VALUE =
124 
125 constexpr const int TIME_MAX_VALUE_SECONDS =
127 
128 constexpr const int DATETIME_MAX_DECIMALS = 6;
129 
130 /** Flags for calc_week() function. */
131 constexpr const unsigned int WEEK_MONDAY_FIRST = 1;
132 constexpr const unsigned int WEEK_YEAR = 2;
133 constexpr const unsigned int WEEK_FIRST_WEEKDAY = 4;
134 
135 /**
136  Structure to return status from
137  str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
138  @note Implicit default constructor initializes all members to 0.
139 */
141  int warnings{0};
142  unsigned int fractional_digits{0};
143  unsigned int nanoseconds{0};
144 };
145 
146 /**
147  Struct representing a duration. Content is similar to MYSQL_TIME
148  but member variables are unsigned.
149  */
150 struct Interval {
151  unsigned long int year;
152  unsigned long int month;
153  unsigned long int day;
154  unsigned long int hour;
155  unsigned long long int minute;
156  unsigned long long int second;
157  unsigned long long int second_part;
158  bool neg;
159 };
160 
161 void my_init_time();
162 
163 long calc_daynr(unsigned int year, unsigned int month, unsigned int day);
164 unsigned int calc_days_in_year(unsigned int year);
165 unsigned int year_2000_handling(unsigned int year);
166 
167 void get_date_from_daynr(long daynr, unsigned int *year, unsigned int *month,
168  unsigned int *day);
169 int calc_weekday(long daynr, bool sunday_first_day_of_week);
170 bool valid_period(long long int period);
171 unsigned long int convert_period_to_month(unsigned long int period);
172 unsigned long int convert_month_to_period(unsigned long int month);
173 
174 /**
175  Check for valid times only if the range of time_t is greater than
176  the range of my_time_t. Which is almost always the case and even time_t
177  does have the same range, the compiler will optimize away
178  the unnecessary test (checked with compiler explorer).
179 */
180 inline bool is_time_t_valid_for_timestamp(time_t x) {
181  return x <= TIMESTAMP_MAX_VALUE && x >= TIMESTAMP_MIN_VALUE;
182 }
183 
184 unsigned int calc_week(const MYSQL_TIME &l_time, unsigned int week_behaviour,
185  unsigned int *year);
186 
187 bool check_date(const MYSQL_TIME &ltime, bool not_zero_date,
188  my_time_flags_t flags, int *was_cut);
189 bool str_to_datetime(const char *str, std::size_t length, MYSQL_TIME *l_time,
191 long long int number_to_datetime(long long int nr, MYSQL_TIME *time_res,
192  my_time_flags_t flags, int *was_cut);
193 bool number_to_time(long long int nr, MYSQL_TIME *ltime, int *warnings);
194 unsigned long long int TIME_to_ulonglong_datetime(const MYSQL_TIME &my_time);
195 unsigned long long int TIME_to_ulonglong_date(const MYSQL_TIME &my_time);
196 unsigned long long int TIME_to_ulonglong_time(const MYSQL_TIME &my_time);
197 unsigned long long int TIME_to_ulonglong(const MYSQL_TIME &my_time);
198 
199 unsigned long long int TIME_to_ulonglong_datetime_round(
200  const MYSQL_TIME &my_time, int *warnings);
201 unsigned long long int TIME_to_ulonglong_time_round(const MYSQL_TIME &my_time);
202 
203 /**
204  Round any MYSQL_TIME timepoint and convert to ulonglong.
205  @param my_time input
206  @param[out] warnings warning vector
207  @return time point as ulonglong
208  */
209 inline unsigned long long int TIME_to_ulonglong_round(const MYSQL_TIME &my_time,
210  int *warnings) {
211  switch (my_time.time_type) {
215  return TIME_to_ulonglong_datetime_round(my_time, warnings);
218  default:
219  assert(false);
220  return 0;
221  }
222 }
223 
224 /**
225  Extract the microsecond part of a MYSQL_TIME struct as an n *
226  (1/10^6) fraction as a double.
227 
228  @return microseconds part as double
229  */
230 inline double TIME_microseconds(const MYSQL_TIME &my_time) {
231  return static_cast<double>(my_time.second_part) / 1000000.0;
232 }
233 
234 /**
235  Convert a MYSQL_TIME datetime to double where the integral part is
236  the timepoint as an ulonglong, and the fractional part is the
237  fraction of the second.
238 
239  @param my_time datetime to convert
240  @return datetime as double
241  */
243  return static_cast<double>(TIME_to_ulonglong_datetime(my_time)) +
245 }
246 
247 /**
248  Convert a MYSQL_TIME time to double where the integral part is
249  the timepoint as an ulonglong, and the fractional part is the
250  fraction of the second.
251 
252  @param my_time time to convert
253  @return datetime as double
254  */
255 inline double TIME_to_double_time(const MYSQL_TIME &my_time) {
256  return static_cast<double>(TIME_to_ulonglong_time(my_time)) +
258 }
259 
260 /**
261  Convert a MYSQL_TIME to double where the integral part is the
262  timepoint as an ulonglong, and the fractional part is the fraction
263  of the second. The actual time type is extracted from
264  MYSQL_TIME::time_type.
265 
266  @param my_time MYSQL_TIME to convert
267  @return MYSQL_TIME as double
268  */
269 inline double TIME_to_double(const MYSQL_TIME &my_time) {
270  return static_cast<double>(TIME_to_ulonglong(my_time)) +
272 }
273 
274 /**
275  Return the fraction of the second as the number of microseconds.
276 
277  @param i timepoint as longlong
278  @return frational part of an timepoint represented as an (u)longlong
279 */
280 inline long long int my_packed_time_get_frac_part(long long int i) {
281  return (i % (1LL << 24));
282 }
283 
284 long long int year_to_longlong_datetime_packed(long year);
286 long long int TIME_to_longlong_date_packed(const MYSQL_TIME &my_time);
287 long long int TIME_to_longlong_time_packed(const MYSQL_TIME &my_time);
288 long long int TIME_to_longlong_packed(const MYSQL_TIME &my_time);
289 
290 void TIME_from_longlong_datetime_packed(MYSQL_TIME *ltime, long long int nr);
291 void TIME_from_longlong_time_packed(MYSQL_TIME *ltime, long long int nr);
292 void TIME_from_longlong_date_packed(MYSQL_TIME *ltime, long long int nr);
293 void TIME_set_yymmdd(MYSQL_TIME *ltime, unsigned int yymmdd);
294 void TIME_set_hhmmss(MYSQL_TIME *ltime, unsigned int hhmmss);
295 
296 void my_datetime_packed_to_binary(long long int nr, unsigned char *ptr,
297  unsigned int dec);
298 long long int my_datetime_packed_from_binary(const unsigned char *ptr,
299  unsigned int dec);
300 
301 void my_time_packed_to_binary(long long int nr, unsigned char *ptr,
302  unsigned int dec);
303 long long int my_time_packed_from_binary(const unsigned char *ptr,
304  unsigned int dec);
305 
306 void my_timestamp_to_binary(const struct timeval *tm, unsigned char *ptr,
307  unsigned int dec);
308 void my_timestamp_from_binary(struct timeval *tm, const unsigned char *ptr,
309  unsigned int dec);
310 
311 bool str_to_time(const char *str, std::size_t length, MYSQL_TIME *l_time,
313 
317 void adjust_time_range(MYSQL_TIME *, int *warning);
318 
319 /**
320  Function to check sanity of a TIMESTAMP value.
321 
322  Check if a given MYSQL_TIME value fits in TIMESTAMP range.
323  This function doesn't make precise check, but rather a rough
324  estimate.
325 
326  @param my_time timepoint to check
327  @retval true The value seems sane
328  @retval false The MYSQL_TIME value is definitely out of range
329 */
331  if ((my_time.year > TIMESTAMP_MAX_YEAR ||
332  my_time.year < TIMESTAMP_MIN_YEAR) ||
333  (my_time.year == TIMESTAMP_MAX_YEAR &&
334  (my_time.month > 1 || my_time.day > 19)) ||
335  (my_time.year == TIMESTAMP_MIN_YEAR &&
336  (my_time.month < 12 || my_time.day < 31)))
337  return false;
338 
339  return true;
340 }
341 
342 my_time_t my_system_gmt_sec(const MYSQL_TIME &my_time, long *my_timezone,
343  bool *in_dst_time_gap);
344 
345 void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type);
346 void set_max_time(MYSQL_TIME *tm, bool neg);
347 void set_max_hhmmss(MYSQL_TIME *tm);
348 
349 /**
350  Required buffer length for my_time_to_str, my_date_to_str,
351  my_datetime_to_str and TIME_to_string functions. Note, that the
352  caller is still responsible to check that given TIME structure
353  has values in valid ranges, otherwise size of the buffer could
354  be not enough. We also rely on the fact that even wrong values
355  sent using binary protocol fit in this buffer.
356 */
357 constexpr const std::size_t MAX_DATE_STRING_REP_LENGTH = 30;
358 
359 int my_time_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec);
360 int my_date_to_str(const MYSQL_TIME &my_time, char *to);
361 int my_datetime_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec);
362 int my_TIME_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec);
363 
364 int my_timeval_to_str(const struct timeval *tm, char *to, unsigned int dec);
365 
366 /**
367  Available interval types used in any statement.
368 
369  'interval_type' must be sorted so that simple intervals comes first,
370  ie year, quarter, month, week, day, hour, etc. The order based on
371  interval size is also important and the intervals should be kept in a
372  large to smaller order. (get_interval_value() depends on this)
373 
374  @note If you change the order of elements in this enum you should fix
375  order of elements in 'interval_type_to_name' and 'interval_names'
376  arrays
377 
378  @see interval_type_to_name, get_interval_value, interval_names
379 */
402 };
403 
404 bool date_add_interval(MYSQL_TIME *ltime, interval_type int_type,
405  Interval interval, int *warnings);
406 
407 /**
408  Round the input argument to the specified precision by computing
409  the remainder modulo log10 of the difference between max and
410  desired precison.
411 
412  @param nr number to round
413  @param decimals desired precision
414  @return nr rounded according to the desired precision.
415 */
416 inline long my_time_fraction_remainder(long nr, unsigned int decimals) {
417  assert(decimals <= DATETIME_MAX_DECIMALS);
418  return nr % static_cast<long>(log_10_int[DATETIME_MAX_DECIMALS - decimals]);
419 }
420 
421 /**
422  Truncate the number of microseconds in MYSQL_TIME::second_part to
423  the desired precision.
424 
425  @param ltime time point
426  @param decimals desired precision
427 */
428 inline void my_time_trunc(MYSQL_TIME *ltime, unsigned int decimals) {
429  ltime->second_part -=
430  my_time_fraction_remainder(ltime->second_part, decimals);
431 }
432 
433 /**
434  Alias for my_time_trunc.
435 
436  @param ltime time point
437  @param decimals desired precision
438  */
439 inline void my_datetime_trunc(MYSQL_TIME *ltime, unsigned int decimals) {
440  return my_time_trunc(ltime, decimals);
441 }
442 
443 /**
444  Truncate the tv_usec member of a posix timeval struct to the
445  specified number of decimals.
446 
447  @param tv timepoint/duration
448  @param decimals desired precision
449  */
450 inline void my_timeval_trunc(struct timeval *tv, unsigned int decimals) {
451  tv->tv_usec -= my_time_fraction_remainder(tv->tv_usec, decimals);
452 }
453 
454 /**
455  Predicate for fuzzyness of date.
456 
457  @param my_time time point to check
458  @param fuzzydate bitfield indicating if fuzzy dates are premitted
459  @retval true if TIME_FUZZY_DATE is unset and either month or day is 0
460  @retval false otherwise
461  */
463  my_time_flags_t fuzzydate) {
464  return !(fuzzydate & TIME_FUZZY_DATE) && (!my_time.month || !my_time.day);
465 }
466 
467 /**
468  Predicate which returns true if at least one of the date members are non-zero.
469 
470  @param my_time time point to check.
471  @retval false if all the date members are zero
472  @retval true otherwise
473  */
474 inline bool non_zero_date(const MYSQL_TIME &my_time) {
475  return my_time.year || my_time.month || my_time.day;
476 }
477 
478 /**
479  Predicate which returns true if at least one of the time members are non-zero.
480 
481  @param my_time time point to check.
482  @retval false if all the time members are zero
483  @retval true otherwise
484 */
485 inline bool non_zero_time(const MYSQL_TIME &my_time) {
486  return my_time.hour || my_time.minute || my_time.second ||
487  my_time.second_part;
488 }
489 
490 /**
491  "Casts" MYSQL_TIME datetime to a MYSQL_TIME time. Sets
492  MYSQL_TIME::time_type to MYSQL_TIMESTAMP_TIME and zeroes out the
493  date members.
494 
495  @param ltime timepoint to cast
496  */
497 inline void datetime_to_time(MYSQL_TIME *ltime) {
498  ltime->year = 0;
499  ltime->month = 0;
500  ltime->day = 0;
502 }
503 
504 /**
505  "Casts" MYSQL_TIME datetime to a MYSQL_TIME date. Sets
506  MYSQL_TIME::time_type to MYSQL_TIMESTAMP_DATE and zeroes out the
507  time members.
508 
509  @param ltime timepoint to cast
510 */
511 inline void datetime_to_date(MYSQL_TIME *ltime) {
512  ltime->hour = 0;
513  ltime->minute = 0;
514  ltime->second = 0;
515  ltime->second_part = 0;
517 }
518 
519 /**
520  "Casts" a MYSQL_TIME to datetime by setting MYSQL_TIME::time_type to
521  MYSQL_TIMESTAMP_DATETIME.
522  @note There is no check to ensure that the result is a valid datetime.
523 
524  @param ltime timpoint to cast
525 */
526 inline void date_to_datetime(MYSQL_TIME *ltime) {
528 }
529 
531  unsigned int nanoseconds,
532  int *warnings);
534  unsigned int nanoseconds);
536  unsigned int nanoseconds, int *warnings);
537 
539  unsigned int nanoseconds,
540  int *warnings);
541 
543  unsigned int nanoseconds, int *warnings,
544  bool truncate);
545 
547  unsigned int nanoseconds,
548  int *warnings, bool truncate);
549 
550 bool my_time_adjust_frac(MYSQL_TIME *ltime, unsigned int dec, bool truncate);
551 bool my_datetime_adjust_frac(MYSQL_TIME *ltime, unsigned int dec, int *warnings,
552  bool truncate);
553 bool my_timeval_round(struct timeval *tv, unsigned int decimals);
554 void mix_date_and_time(MYSQL_TIME *ldate, const MYSQL_TIME &my_time);
555 
556 void localtime_to_TIME(MYSQL_TIME *to, const struct tm *from);
557 void calc_time_from_sec(MYSQL_TIME *to, long long int seconds,
558  long microseconds);
559 bool calc_time_diff(const MYSQL_TIME &my_time1, const MYSQL_TIME &my_time2,
560  int l_sign, long long int *seconds_out,
561  long *microseconds_out);
562 int my_time_compare(const MYSQL_TIME &my_time_a, const MYSQL_TIME &my_time_b);
563 
564 long long int TIME_to_longlong_packed(const MYSQL_TIME &my_time,
565  enum enum_field_types type);
566 
568  long long int packed_value);
569 
571  long long int packed_value);
572 
574  long long int packed_value);
575 
576 /**
577  @} (end of ingroup MY_TIME)
578 */
579 #endif /* MY_TIME_INCLUDED */
constexpr const int TIMESTAMP_MIN_VALUE
Definition: my_time.h:79
enum enum_mysql_timestamp_type time_type
Definition: mysql_time.h:68
Definition: my_time.h:387
unsigned long second_part
microseconds
Definition: mysql_time.h:66
my_time_t my_system_gmt_sec(const MYSQL_TIME &my_time, long *my_timezone, bool *in_dst_time_gap)
Convert time in MYSQL_TIME representation in system time zone to its my_time_t form (number of second...
Definition: my_time.cc:989
long long int TIME_to_longlong_packed(const MYSQL_TIME &my_time)
Convert a temporal value to packed numeric temporal representation, depending on its time_type...
Definition: my_time.cc:1979
constexpr const int TIMESTAMP_MAX_VALUE
Definition: my_time.h:77
static int interval
Definition: mysqladmin.cc:62
constexpr const my_time_flags_t TIME_DATETIME_ONLY
Only allow full datetimes.
Definition: my_time.h:88
Definition: my_time.h:396
bool check_date(const MYSQL_TIME &ltime, bool not_zero_date, my_time_flags_t flags, int *was_cut)
Check datetime value for validity according to flags.
Definition: my_time.cc:192
unsigned int second
Definition: mysql_time.h:65
int my_TIME_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec)
Convert struct DATE/TIME/DATETIME value to string using built-in MySQL time conversion formats...
Definition: my_time.cc:1293
Definition: my_time.h:397
bool neg
Definition: my_time.h:158
bool str_to_time(const char *str, std::size_t length, MYSQL_TIME *l_time, MYSQL_TIME_STATUS *status)
Convert a time string to a MYSQL_TIME struct.
Definition: my_time.cc:661
long long int TIME_to_longlong_datetime_packed(const MYSQL_TIME &my_time)
Convert datetime to packed numeric datetime representation.
Definition: my_time.cc:1762
constexpr const my_time_flags_t TIME_NO_DATE_FRAC_WARN
Definition: my_time.h:91
bool my_timeval_round(struct timeval *tv, unsigned int decimals)
Round timeval value to the given precision.
Definition: my_time.cc:2505
double TIME_to_double_time(const MYSQL_TIME &my_time)
Convert a MYSQL_TIME time to double where the integral part is the timepoint as an ulonglong...
Definition: my_time.h:255
bool str_to_datetime(const char *str, std::size_t length, MYSQL_TIME *l_time, my_time_flags_t flags, MYSQL_TIME_STATUS *status)
Convert a timestamp string to a MYSQL_TIME value.
Definition: my_time.cc:328
Definition: my_time.h:392
int my_time_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec)
Functions to convert time value to a string, using default format.
Definition: my_time.cc:1181
bool non_zero_time(const MYSQL_TIME &my_time)
Predicate which returns true if at least one of the time members are non-zero.
Definition: my_time.h:485
double TIME_microseconds(const MYSQL_TIME &my_time)
Extract the microsecond part of a MYSQL_TIME struct as an n * (1/10^6) fraction as a double...
Definition: my_time.h:230
unsigned int minute
Definition: mysql_time.h:65
unsigned long long int TIME_to_ulonglong_datetime(const MYSQL_TIME &my_time)
Convert time value to integer in YYYYMMDDHHMMSS.
Definition: my_time.cc:1436
constexpr const my_time_flags_t TIME_FRAC_TRUNCATE
Definition: my_time.h:90
void warning(const char *format,...)
bool datetime_add_nanoseconds_adjust_frac(MYSQL_TIME *ltime, unsigned int nanoseconds, int *warnings, bool truncate)
Add nanoseconds to datetime and round or tuncate as indicated by argument.
Definition: my_time.cc:2444
int my_timeval_to_str(const struct timeval *tm, char *to, unsigned int dec)
Print a timestamp with an oprional fractional part: XXXXX[.YYYYY].
Definition: my_time.cc:1319
void localtime_to_TIME(MYSQL_TIME *to, const struct tm *from)
Convert a timepoint in a posix tm struct to a MSYQL_TIME struct.
Definition: my_time.cc:2570
constexpr const int TIME_MAX_MINUTE
Definition: my_time.h:116
constexpr const my_time_flags_t TIME_FUZZY_DATE
Allow zero day and zero month.
Definition: my_time.h:85
int calc_weekday(long daynr, bool sunday_first_day_of_week)
Calc weekday from daynr.
Definition: my_time.cc:2041
Definition: my_time.h:398
void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type)
Set MYSQL_TIME structure to 0000-00-00 00:00:00.000000.
Definition: my_time.cc:147
double double_from_datetime_packed(enum enum_field_types type, long long int packed_value)
Convert packed numeric temporal representation to unpacked numeric representation.
Definition: my_time.cc:2778
void adjust_time_range(MYSQL_TIME *, int *warning)
Adjust &#39;time&#39; value to lie in the MYSQL_TIME range.
Definition: my_time.cc:895
const uchar days_in_month[]
Definition: my_time.cc:93
unsigned long int day
Definition: my_time.h:153
[] long int my_time_t
Portable time_t replacement.
double TIME_to_double_datetime(const MYSQL_TIME &my_time)
Convert a MYSQL_TIME datetime to double where the integral part is the timepoint as an ulonglong...
Definition: my_time.h:242
void my_datetime_packed_to_binary(long long int nr, unsigned char *ptr, unsigned int dec)
Store in-memory numeric packed datetime representation to disk.
Definition: my_time.cc:1882
unsigned int hour
Definition: mysql_time.h:65
unsigned long long int second
Definition: my_time.h:156
Definition: my_time.h:383
void mix_date_and_time(MYSQL_TIME *ldate, const MYSQL_TIME &my_time)
Mix a date value and a time value.
Definition: my_time.cc:2529
unsigned int day
Definition: mysql_time.h:65
void datetime_to_date(MYSQL_TIME *ltime)
"Casts" MYSQL_TIME datetime to a MYSQL_TIME date.
Definition: my_time.h:511
Definition: my_time.h:385
int warnings
Definition: my_time.h:141
unsigned long long int TIME_to_ulonglong(const MYSQL_TIME &my_time)
Convert struct MYSQL_TIME (date and time split into year/month/day/hour/...
Definition: my_time.cc:1513
constexpr const std::size_t MAX_DATE_STRING_REP_LENGTH
Required buffer length for my_time_to_str, my_date_to_str, my_datetime_to_str and TIME_to_string func...
Definition: my_time.h:357
bool check_time_mmssff_range(const MYSQL_TIME &my_time)
Check if TIME fields can be adjusted to make the time value valid.
Definition: my_time.cc:221
Definition: my_time.h:384
Structure to return status from str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
Definition: my_time.h:140
Definition: my_time.h:395
bool non_zero_date(const MYSQL_TIME &my_time)
Predicate which returns true if at least one of the date members are non-zero.
Definition: my_time.h:474
unsigned long int month
Definition: my_time.h:152
constexpr const int MYSQL_TIME_WARN_OUT_OF_RANGE
Definition: my_time.h:104
void my_init_time()
Prepare offset of system time zone from UTC for my_system_gmt_sec() func.
Definition: my_time.cc:907
bool valid_period(long long int period)
bool number_to_time(long long int nr, MYSQL_TIME *ltime, int *warnings)
Convert number to TIME.
Definition: my_time.cc:854
long long int my_time_packed_from_binary(const unsigned char *ptr, unsigned int dec)
Convert on-disk time representation to in-memory packed numeric representation.
Definition: my_time.cc:1676
unsigned long long int TIME_to_ulonglong_time_round(const MYSQL_TIME &my_time)
Round MYSQL_TIME time value and convert to to ulonglong representation.
Definition: my_time.cc:1555
Definition: my_time.h:394
constexpr const int TIME_MAX_HOUR
Limits for the TIME data type.
Definition: my_time.h:115
interval_type
Available interval types used in any statement.
Definition: my_time.h:380
void set_max_hhmmss(MYSQL_TIME *tm)
Set hour, minute and second of a MYSQL_TIME variable to maximum time value.
Definition: my_time.cc:156
bool is_time_t_valid_for_timestamp(time_t x)
Check for valid times only if the range of time_t is greater than the range of my_time_t.
Definition: my_time.h:180
constexpr const int YY_PART_YEAR
Two-digit years < this are 20XX; >= this are 19XX.
Definition: my_time.h:74
long long int longlong_from_datetime_packed(enum enum_field_types type, long long int packed_value)
Convert packed numeric representation to unpacked numeric representation.
Definition: my_time.cc:2748
constexpr const my_time_t MY_TIME_T_MIN
Definition: my_time.h:68
Definition: my_time.h:400
bool calc_time_diff(const MYSQL_TIME &my_time1, const MYSQL_TIME &my_time2, int l_sign, long long int *seconds_out, long *microseconds_out)
Calculate difference between two datetime values as seconds + microseconds.
Definition: my_time.cc:2621
Struct representing a duration.
Definition: my_time.h:150
Definition: my_time.h:386
long calc_daynr(unsigned int year, unsigned int month, unsigned int day)
Calculate nr of day since year 0 in new date-system (from 1615).
Definition: my_time.cc:952
constexpr const int TIME_MAX_VALUE_SECONDS
Definition: my_time.h:125
constexpr const my_time_flags_t TIME_NO_ZERO_IN_DATE
Don&#39;t allow zero day or zero month.
Definition: my_time.h:94
int my_time_compare(const MYSQL_TIME &my_time_a, const MYSQL_TIME &my_time_b)
Compare tow MYSQL_TIME objects.
Definition: my_time.cc:2675
void my_timeval_trunc(struct timeval *tv, unsigned int decimals)
Truncate the tv_usec member of a posix timeval struct to the specified number of decimals.
Definition: my_time.h:450
constexpr const int MYSQL_TIME_WARN_INVALID_TIMESTAMP
Definition: my_time.h:105
constexpr const long int SECONDS_IN_24H
Usefull constants.
Definition: my_time.h:112
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
unsigned int fractional_digits
Definition: my_time.h:142
constexpr const int MYSQL_TIME_NOTE_TRUNCATED
Definition: my_time.h:107
constexpr const unsigned int WEEK_YEAR
Definition: my_time.h:132
long long int TIME_to_longlong_date_packed(const MYSQL_TIME &my_time)
Convert date to packed numeric date representation.
Definition: my_time.cc:1778
unsigned long long int TIME_to_ulonglong_time(const MYSQL_TIME &my_time)
Convert MYSQL_TIME value to integer in HHMMSS format.
Definition: my_time.cc:1463
constexpr const int TIMESTAMP_MAX_YEAR
Time handling defaults.
Definition: my_time.h:71
void my_timestamp_to_binary(const struct timeval *tm, unsigned char *ptr, unsigned int dec)
Convert in-memory timestamp representation to on-disk representation.
Definition: my_time.cc:1946
void my_time_trunc(MYSQL_TIME *ltime, unsigned int decimals)
Truncate the number of microseconds in MYSQL_TIME::second_part to the desired precision.
Definition: my_time.h:428
unsigned int year
Definition: mysql_time.h:65
unsigned long long int TIME_to_ulonglong_round(const MYSQL_TIME &my_time, int *warnings)
Round any MYSQL_TIME timepoint and convert to ulonglong.
Definition: my_time.h:209
unsigned int calc_week(const MYSQL_TIME &l_time, unsigned int week_behaviour, unsigned int *year)
Calculate the week number from a MYSQL_TIME value.
Definition: my_time.cc:2083
time_t my_time(int)
Return current time.
Definition: my_systime.h:169
constexpr const int MYSQL_TIME_WARN_TRUNCATED
Conversion warnings.
Definition: my_time.h:103
void my_timestamp_from_binary(struct timeval *tm, const unsigned char *ptr, unsigned int dec)
Convert binary timestamp representation to in-memory representation.
Definition: my_time.cc:1917
constexpr const int MYSQL_TIME_WARN_DATETIME_OVERFLOW
Definition: my_time.h:109
const ulonglong log_10_int[20]
Definition: my_time.cc:60
bool datetime_add_nanoseconds_with_round(MYSQL_TIME *ltime, unsigned int nanoseconds, int *warnings)
Add nanoseconds to a datetime value with rounding.
Definition: my_time.cc:2392
bool time_add_nanoseconds_with_truncate(MYSQL_TIME *ltime, unsigned int nanoseconds, int *warnings)
Add nanoseconds to a time value with truncation.
Definition: my_time.cc:2309
constexpr const my_time_t MY_TIME_T_MAX
Definition: my_time.h:67
constexpr const my_time_flags_t TIME_INVALID_DATES
Allow 2000-02-31.
Definition: my_time.h:100
constexpr const unsigned int WEEK_FIRST_WEEKDAY
Definition: my_time.h:133
Definition: my_time.h:390
constexpr const int TIMESTAMP_MIN_YEAR
Definition: my_time.h:75
Definition: mysql_time.h:49
unsigned int calc_days_in_year(unsigned int year)
Calc days in one year.
Definition: my_time.cc:137
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:82
constexpr const int TIME_MAX_SECOND
Definition: my_time.h:117
constexpr const int MYSQL_TIME_WARN_ZERO_DATE
Definition: my_time.h:106
void calc_time_from_sec(MYSQL_TIME *to, long long int seconds, long microseconds)
Initialize MYSQL_TIME with MYSQL_TIMESTAMP_TIME from given number of seconds and microseconds.
Definition: my_time.cc:2585
void my_time_packed_to_binary(long long int nr, unsigned char *ptr, unsigned int dec)
Convert in-memory numeric time representation to on-disk representation.
Definition: my_time.cc:1636
void datetime_to_time(MYSQL_TIME *ltime)
"Casts" MYSQL_TIME datetime to a MYSQL_TIME time.
Definition: my_time.h:497
constexpr const int TIME_MAX_VALUE
Note that this MUST be a signed type, as we use the unary - operator on it.
Definition: my_time.h:122
bool my_time_adjust_frac(MYSQL_TIME *ltime, unsigned int dec, bool truncate)
Round/Truncate time value to the given precision.
Definition: my_time.cc:2465
Definition: mysql_time.h:64
void set_max_time(MYSQL_TIME *tm, bool neg)
Set MYSQL_TIME variable to maximum time value.
Definition: my_time.cc:167
enum_mysql_timestamp_type
Definition: mysql_time.h:44
void TIME_from_longlong_date_packed(MYSQL_TIME *ltime, long long int nr)
Convert packed numeric date representation to MYSQL_TIME.
Definition: my_time.cc:1832
Definition: my_time.h:391
Definition: mysql_time.h:47
int type
Definition: http_common.h:411
bool datetime_add_nanoseconds_with_truncate(MYSQL_TIME *ltime, unsigned int nanoseconds)
Add nanoseconds to a datetime value with truncation.
Definition: my_time.cc:2329
unsigned int year_2000_handling(unsigned int year)
Handle 2 digit year conversions.
Definition: my_time.cc:936
long long int TIME_to_longlong_time_packed(const MYSQL_TIME &my_time)
Convert time value to numeric packed representation.
Definition: my_time.cc:1591
Definition: my_time.h:393
void my_datetime_trunc(MYSQL_TIME *ltime, unsigned int decimals)
Alias for my_time_trunc.
Definition: my_time.h:439
Definition: my_time.h:389
unsigned long long int second_part
Definition: my_time.h:157
void get_date_from_daynr(long daynr, unsigned int *year, unsigned int *month, unsigned int *day)
Change a daynr to year, month and day.
Definition: my_time.cc:1999
Definition: mysql_time.h:48
Time declarations shared between the server and client API: you should not add anything to this heade...
bool time_add_nanoseconds_with_round(MYSQL_TIME *ltime, unsigned int nanoseconds, int *warnings)
Add nanoseconds to a time value with rounding.
Definition: my_time.cc:2348
bool check_fuzzy_date(const MYSQL_TIME &my_time, my_time_flags_t fuzzydate)
Predicate for fuzzyness of date.
Definition: my_time.h:462
void TIME_from_longlong_time_packed(MYSQL_TIME *ltime, long long int nr)
Convert time packed numeric representation to time.
Definition: my_time.cc:1605
unsigned long long int TIME_to_ulonglong_date(const MYSQL_TIME &my_time)
Convert MYSQL_TIME value to integer in YYYYMMDD format.
Definition: my_time.cc:1450
#define L
Definition: ctype-tis620.cc:73
Definition: my_time.h:381
long my_time_fraction_remainder(long nr, unsigned int decimals)
Round the input argument to the specified precision by computing the remainder modulo log10 of the di...
Definition: my_time.h:416
constexpr const my_time_flags_t TIME_NO_ZERO_DATE
Don&#39;t allow 0000-00-00 date.
Definition: my_time.h:97
long long int my_datetime_packed_from_binary(const unsigned char *ptr, unsigned int dec)
Convert on-disk datetime representation to in-memory packed numeric representation.
Definition: my_time.cc:1851
bool check_time_range_quick(const MYSQL_TIME &my_time)
Check TIME range.
Definition: my_time.cc:238
int my_date_to_str(const MYSQL_TIME &my_time, char *to)
Function to convert date value to a string, using default format.
Definition: my_time.cc:1203
const char my_zero_datetime6[]
Definition: my_time.cc:81
unsigned long int hour
Definition: my_time.h:154
void TIME_from_longlong_datetime_packed(MYSQL_TIME *ltime, long long int nr)
Convert packed numeric datetime representation to MYSQL_TIME.
Definition: my_time.cc:1800
unsigned long long int TIME_to_ulonglong_datetime_round(const MYSQL_TIME &my_time, int *warnings)
Round MYSQL_TIME datetime value and convert to ulonglong representation.
Definition: my_time.cc:1538
Definition: my_time.h:399
bool date_add_interval(MYSQL_TIME *ltime, interval_type int_type, Interval interval, int *warnings)
Add an interval to a MYSQL_TIME struct.
Definition: my_time.cc:2173
int my_datetime_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec)
Print a datetime value with an optional fractional part.
Definition: my_time.cc:1273
static STATUS status
Definition: mysql.cc:188
long long int number_to_datetime(long long int nr, MYSQL_TIME *time_res, my_time_flags_t flags, int *was_cut)
Convert datetime value specified as number to broken-down TIME representation and form value of DATET...
Definition: my_time.cc:1355
bool time_add_nanoseconds_adjust_frac(MYSQL_TIME *ltime, unsigned int nanoseconds, int *warnings, bool truncate)
Add nanoseconds to time and round or tuncate as indicated by argument.
Definition: my_time.cc:2426
unsigned long int convert_month_to_period(unsigned long int month)
bool validate_timestamp_range(const MYSQL_TIME &my_time)
Function to check sanity of a TIMESTAMP value.
Definition: my_time.h:330
bool my_datetime_adjust_frac(MYSQL_TIME *ltime, unsigned int dec, int *warnings, bool truncate)
Round/Truncate datetime value to the given precision.
Definition: my_time.cc:2487
constexpr const unsigned int WEEK_MONDAY_FIRST
Flags for calc_week() function.
Definition: my_time.h:131
double TIME_to_double(const MYSQL_TIME &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.
Definition: my_time.h:269
static int flags[50]
Definition: hp_test1.cc:39
bool check_datetime_range(const MYSQL_TIME &my_time)
Check datetime, date, or normalized time (i.e.
Definition: my_time.cc:256
long long int year_to_longlong_datetime_packed(long year)
Convert year to packed numeric date representation.
Definition: my_time.cc:1789
void TIME_set_hhmmss(MYSQL_TIME *ltime, unsigned int hhmmss)
Set hour, minute and secondr from a number.
Definition: my_time.cc:1486
unsigned int nanoseconds
Definition: my_time.h:143
constexpr const int MYSQL_TIME_WARN_ZERO_IN_DATE
Definition: my_time.h:108
Definition: my_time.h:388
constexpr const int DATETIME_MAX_DECIMALS
Definition: my_time.h:128
Definition: my_time.h:401
void TIME_set_yymmdd(MYSQL_TIME *ltime, unsigned int yymmdd)
Set day, month and year from a number.
Definition: my_time.cc:1474
unsigned int month
Definition: mysql_time.h:65
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
unsigned long long int minute
Definition: my_time.h:155
unsigned long int year
Definition: my_time.h:151
void TIME_from_longlong_packed(MYSQL_TIME *ltime, enum enum_field_types type, long long int packed_value)
Convert packed numeric temporal representation to time, date or datetime, using field type...
Definition: my_time.cc:2719
unsigned long int convert_period_to_month(unsigned long int period)
long long int my_packed_time_get_frac_part(long long int i)
Return the fraction of the second as the number of microseconds.
Definition: my_time.h:280
void date_to_datetime(MYSQL_TIME *ltime)
"Casts" a MYSQL_TIME to datetime by setting MYSQL_TIME::time_type to MYSQL_TIMESTAMP_DATETIME.
Definition: my_time.h:526
Definition: my_time.h:382