MySQL  8.0.17
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 /** Allow only HH:MM:SS or MM:SS time formats */
103 constexpr const my_time_flags_t TIME_STRICT_COLON = 128;
104 
105 /** Conversion warnings */
106 constexpr const int MYSQL_TIME_WARN_TRUNCATED = 1;
107 constexpr const int MYSQL_TIME_WARN_OUT_OF_RANGE = 2;
108 constexpr const int MYSQL_TIME_WARN_INVALID_TIMESTAMP = 4;
109 constexpr const int MYSQL_TIME_WARN_ZERO_DATE = 8;
110 constexpr const int MYSQL_TIME_NOTE_TRUNCATED = 16;
111 constexpr const int MYSQL_TIME_WARN_ZERO_IN_DATE = 32;
112 constexpr const int MYSQL_TIME_WARN_DATETIME_OVERFLOW = 64;
113 
114 /** Usefull constants */
115 constexpr const long int SECONDS_IN_24H = 86400L;
116 
117 /** Limits for the TIME data type */
118 constexpr const int TIME_MAX_HOUR = 838;
119 constexpr const int TIME_MAX_MINUTE = 59;
120 constexpr const int TIME_MAX_SECOND = 59;
121 
122 /**
123  Note that this MUST be a signed type, as we use the unary - operator on it.
124  */
125 constexpr const int TIME_MAX_VALUE =
127 
128 constexpr const int TIME_MAX_VALUE_SECONDS =
130 
131 constexpr const int DATETIME_MAX_DECIMALS = 6;
132 
133 /** Flags for calc_week() function. */
134 constexpr const unsigned int WEEK_MONDAY_FIRST = 1;
135 constexpr const unsigned int WEEK_YEAR = 2;
136 constexpr const unsigned int WEEK_FIRST_WEEKDAY = 4;
137 
138 /**
139  Structure to return status from
140  str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
141  @note Implicit default constructor initializes all members to 0.
142 */
144  int warnings{0};
145  unsigned int fractional_digits{0};
146  unsigned int nanoseconds{0};
147 };
148 
149 /**
150  Struct representing a duration. Content is similar to MYSQL_TIME
151  but member variables are unsigned.
152  */
153 struct Interval {
154  unsigned long int year;
155  unsigned long int month;
156  unsigned long int day;
157  unsigned long int hour;
158  unsigned long long int minute;
159  unsigned long long int second;
160  unsigned long long int second_part;
161  bool neg;
162 };
163 
164 void my_init_time();
165 
166 long calc_daynr(unsigned int year, unsigned int month, unsigned int day);
167 unsigned int calc_days_in_year(unsigned int year);
168 unsigned int year_2000_handling(unsigned int year);
169 
170 void get_date_from_daynr(long daynr, unsigned int *year, unsigned int *month,
171  unsigned int *day);
172 int calc_weekday(long daynr, bool sunday_first_day_of_week);
173 bool valid_period(long long int period);
174 unsigned long int convert_period_to_month(unsigned long int period);
175 unsigned long int convert_month_to_period(unsigned long int month);
176 
177 /**
178  Check for valid times only if the range of time_t is greater than
179  the range of my_time_t. Which is almost always the case and even time_t
180  does have the same range, the compiler will optimize away
181  the unnecessary test (checked with compiler explorer).
182 */
183 inline bool is_time_t_valid_for_timestamp(time_t x) {
184  return x <= TIMESTAMP_MAX_VALUE && x >= TIMESTAMP_MIN_VALUE;
185 }
186 
187 unsigned int calc_week(const MYSQL_TIME &l_time, unsigned int week_behaviour,
188  unsigned int *year);
189 
190 bool check_date(const MYSQL_TIME &ltime, bool not_zero_date,
191  my_time_flags_t flags, int *was_cut);
192 bool str_to_datetime(const char *str, std::size_t length, MYSQL_TIME *l_time,
194 long long int number_to_datetime(long long int nr, MYSQL_TIME *time_res,
195  my_time_flags_t flags, int *was_cut);
196 bool number_to_time(long long int nr, MYSQL_TIME *ltime, int *warnings);
197 unsigned long long int TIME_to_ulonglong_datetime(const MYSQL_TIME &my_time);
198 unsigned long long int TIME_to_ulonglong_date(const MYSQL_TIME &my_time);
199 unsigned long long int TIME_to_ulonglong_time(const MYSQL_TIME &my_time);
200 unsigned long long int TIME_to_ulonglong(const MYSQL_TIME &my_time);
201 
202 unsigned long long int TIME_to_ulonglong_datetime_round(
203  const MYSQL_TIME &my_time, int *warnings);
204 unsigned long long int TIME_to_ulonglong_time_round(const MYSQL_TIME &my_time);
205 
206 /**
207  Round any MYSQL_TIME timepoint and convert to ulonglong.
208  @param my_time input
209  @param[out] warnings warning vector
210  @return time point as ulonglong
211  */
212 inline unsigned long long int TIME_to_ulonglong_round(const MYSQL_TIME &my_time,
213  int *warnings) {
214  switch (my_time.time_type) {
218  return TIME_to_ulonglong_datetime_round(my_time, warnings);
221  default:
222  assert(false);
223  return 0;
224  }
225 }
226 
227 /**
228  Extract the microsecond part of a MYSQL_TIME struct as an n *
229  (1/10^6) fraction as a double.
230 
231  @return microseconds part as double
232  */
233 inline double TIME_microseconds(const MYSQL_TIME &my_time) {
234  return static_cast<double>(my_time.second_part) / 1000000.0;
235 }
236 
237 /**
238  Convert a MYSQL_TIME datetime to double where the integral part is
239  the timepoint as an ulonglong, and the fractional part is the
240  fraction of the second.
241 
242  @param my_time datetime to convert
243  @return datetime as double
244  */
246  return static_cast<double>(TIME_to_ulonglong_datetime(my_time)) +
248 }
249 
250 /**
251  Convert a MYSQL_TIME time to double where the integral part is
252  the timepoint as an ulonglong, and the fractional part is the
253  fraction of the second.
254 
255  @param my_time time to convert
256  @return datetime as double
257  */
258 inline double TIME_to_double_time(const MYSQL_TIME &my_time) {
259  return static_cast<double>(TIME_to_ulonglong_time(my_time)) +
261 }
262 
263 /**
264  Convert a MYSQL_TIME to double where the integral part is the
265  timepoint as an ulonglong, and the fractional part is the fraction
266  of the second. The actual time type is extracted from
267  MYSQL_TIME::time_type.
268 
269  @param my_time MYSQL_TIME to convert
270  @return MYSQL_TIME as double
271  */
272 inline double TIME_to_double(const MYSQL_TIME &my_time) {
273  return static_cast<double>(TIME_to_ulonglong(my_time)) +
275 }
276 
277 /**
278  Return the fraction of the second as the number of microseconds.
279 
280  @param i timepoint as longlong
281  @return frational part of an timepoint represented as an (u)longlong
282 */
283 inline long long int my_packed_time_get_frac_part(long long int i) {
284  return (i % (1LL << 24));
285 }
286 
287 long long int year_to_longlong_datetime_packed(long year);
289 long long int TIME_to_longlong_date_packed(const MYSQL_TIME &my_time);
290 long long int TIME_to_longlong_time_packed(const MYSQL_TIME &my_time);
291 long long int TIME_to_longlong_packed(const MYSQL_TIME &my_time);
292 
293 void TIME_from_longlong_datetime_packed(MYSQL_TIME *ltime, long long int nr);
294 void TIME_from_longlong_time_packed(MYSQL_TIME *ltime, long long int nr);
295 void TIME_from_longlong_date_packed(MYSQL_TIME *ltime, long long int nr);
296 void TIME_set_yymmdd(MYSQL_TIME *ltime, unsigned int yymmdd);
297 void TIME_set_hhmmss(MYSQL_TIME *ltime, unsigned int hhmmss);
298 
299 void my_datetime_packed_to_binary(long long int nr, unsigned char *ptr,
300  unsigned int dec);
301 long long int my_datetime_packed_from_binary(const unsigned char *ptr,
302  unsigned int dec);
303 
304 void my_time_packed_to_binary(long long int nr, unsigned char *ptr,
305  unsigned int dec);
306 long long int my_time_packed_from_binary(const unsigned char *ptr,
307  unsigned int dec);
308 
309 void my_timestamp_to_binary(const struct timeval *tm, unsigned char *ptr,
310  unsigned int dec);
311 void my_timestamp_from_binary(struct timeval *tm, const unsigned char *ptr,
312  unsigned int dec);
313 
314 bool str_to_time(const char *str, std::size_t length, MYSQL_TIME *l_time,
316 
320 void adjust_time_range(MYSQL_TIME *, int *warning);
321 
322 /**
323  Function to check sanity of a TIMESTAMP value.
324 
325  Check if a given MYSQL_TIME value fits in TIMESTAMP range.
326  This function doesn't make precise check, but rather a rough
327  estimate.
328 
329  @param my_time timepoint to check
330  @retval true The value seems sane
331  @retval false The MYSQL_TIME value is definitely out of range
332 */
334  if ((my_time.year > TIMESTAMP_MAX_YEAR ||
335  my_time.year < TIMESTAMP_MIN_YEAR) ||
336  (my_time.year == TIMESTAMP_MAX_YEAR &&
337  (my_time.month > 1 || my_time.day > 19)) ||
338  (my_time.year == TIMESTAMP_MIN_YEAR &&
339  (my_time.month < 12 || my_time.day < 31)))
340  return false;
341 
342  return true;
343 }
344 
345 my_time_t my_system_gmt_sec(const MYSQL_TIME &my_time, long *my_timezone,
346  bool *in_dst_time_gap);
347 
348 void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type);
349 void set_max_time(MYSQL_TIME *tm, bool neg);
350 void set_max_hhmmss(MYSQL_TIME *tm);
351 
352 /**
353  Required buffer length for my_time_to_str, my_date_to_str,
354  my_datetime_to_str and TIME_to_string functions. Note, that the
355  caller is still responsible to check that given TIME structure
356  has values in valid ranges, otherwise size of the buffer could
357  be not enough. We also rely on the fact that even wrong values
358  sent using binary protocol fit in this buffer.
359 */
360 constexpr const std::size_t MAX_DATE_STRING_REP_LENGTH = 30;
361 
362 int my_time_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec);
363 int my_date_to_str(const MYSQL_TIME &my_time, char *to);
364 int my_datetime_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec);
365 int my_TIME_to_str(const MYSQL_TIME &my_time, char *to, unsigned int dec);
366 
367 void my_date_to_binary(const MYSQL_TIME *ltime, unsigned char *ptr);
368 int my_timeval_to_str(const struct timeval *tm, char *to, unsigned int dec);
369 
370 /**
371  Available interval types used in any statement.
372 
373  'interval_type' must be sorted so that simple intervals comes first,
374  ie year, quarter, month, week, day, hour, etc. The order based on
375  interval size is also important and the intervals should be kept in a
376  large to smaller order. (get_interval_value() depends on this)
377 
378  @note If you change the order of elements in this enum you should fix
379  order of elements in 'interval_type_to_name' and 'interval_names'
380  arrays
381 
382  @see interval_type_to_name, get_interval_value, interval_names
383 */
406 };
407 
408 bool date_add_interval(MYSQL_TIME *ltime, interval_type int_type,
409  Interval interval, int *warnings);
410 
411 /**
412  Round the input argument to the specified precision by computing
413  the remainder modulo log10 of the difference between max and
414  desired precison.
415 
416  @param nr number to round
417  @param decimals desired precision
418  @return nr rounded according to the desired precision.
419 */
420 inline long my_time_fraction_remainder(long nr, unsigned int decimals) {
421  assert(decimals <= DATETIME_MAX_DECIMALS);
422  return nr % static_cast<long>(log_10_int[DATETIME_MAX_DECIMALS - decimals]);
423 }
424 
425 /**
426  Truncate the number of microseconds in MYSQL_TIME::second_part to
427  the desired precision.
428 
429  @param ltime time point
430  @param decimals desired precision
431 */
432 inline void my_time_trunc(MYSQL_TIME *ltime, unsigned int decimals) {
433  ltime->second_part -=
434  my_time_fraction_remainder(ltime->second_part, decimals);
435 }
436 
437 /**
438  Alias for my_time_trunc.
439 
440  @param ltime time point
441  @param decimals desired precision
442  */
443 inline void my_datetime_trunc(MYSQL_TIME *ltime, unsigned int decimals) {
444  return my_time_trunc(ltime, decimals);
445 }
446 
447 /**
448  Truncate the tv_usec member of a posix timeval struct to the
449  specified number of decimals.
450 
451  @param tv timepoint/duration
452  @param decimals desired precision
453  */
454 inline void my_timeval_trunc(struct timeval *tv, unsigned int decimals) {
455  tv->tv_usec -= my_time_fraction_remainder(tv->tv_usec, decimals);
456 }
457 
458 /**
459  Predicate for fuzzyness of date.
460 
461  @param my_time time point to check
462  @param fuzzydate bitfield indicating if fuzzy dates are premitted
463  @retval true if TIME_FUZZY_DATE is unset and either month or day is 0
464  @retval false otherwise
465  */
467  my_time_flags_t fuzzydate) {
468  return !(fuzzydate & TIME_FUZZY_DATE) && (!my_time.month || !my_time.day);
469 }
470 
471 /**
472  Predicate which returns true if at least one of the date members are non-zero.
473 
474  @param my_time time point to check.
475  @retval false if all the date members are zero
476  @retval true otherwise
477  */
478 inline bool non_zero_date(const MYSQL_TIME &my_time) {
479  return my_time.year || my_time.month || my_time.day;
480 }
481 
482 /**
483  Predicate which returns true if at least one of the time members are non-zero.
484 
485  @param my_time time point to check.
486  @retval false if all the time members are zero
487  @retval true otherwise
488 */
489 inline bool non_zero_time(const MYSQL_TIME &my_time) {
490  return my_time.hour || my_time.minute || my_time.second ||
491  my_time.second_part;
492 }
493 
494 /**
495  "Casts" MYSQL_TIME datetime to a MYSQL_TIME time. Sets
496  MYSQL_TIME::time_type to MYSQL_TIMESTAMP_TIME and zeroes out the
497  date members.
498 
499  @param ltime timepoint to cast
500  */
501 inline void datetime_to_time(MYSQL_TIME *ltime) {
502  ltime->year = 0;
503  ltime->month = 0;
504  ltime->day = 0;
506 }
507 
508 /**
509  "Casts" MYSQL_TIME datetime to a MYSQL_TIME date. Sets
510  MYSQL_TIME::time_type to MYSQL_TIMESTAMP_DATE and zeroes out the
511  time members.
512 
513  @param ltime timepoint to cast
514 */
515 inline void datetime_to_date(MYSQL_TIME *ltime) {
516  ltime->hour = 0;
517  ltime->minute = 0;
518  ltime->second = 0;
519  ltime->second_part = 0;
521 }
522 
523 /**
524  "Casts" a MYSQL_TIME to datetime by setting MYSQL_TIME::time_type to
525  MYSQL_TIMESTAMP_DATETIME.
526  @note There is no check to ensure that the result is a valid datetime.
527 
528  @param ltime timpoint to cast
529 */
530 inline void date_to_datetime(MYSQL_TIME *ltime) {
532 }
533 
535  unsigned int nanoseconds,
536  int *warnings);
538  unsigned int nanoseconds);
540  unsigned int nanoseconds, int *warnings);
541 
543  unsigned int nanoseconds,
544  int *warnings);
545 
547  unsigned int nanoseconds, int *warnings,
548  bool truncate);
549 
551  unsigned int nanoseconds,
552  int *warnings, bool truncate);
553 
554 bool my_time_adjust_frac(MYSQL_TIME *ltime, unsigned int dec, bool truncate);
555 bool my_datetime_adjust_frac(MYSQL_TIME *ltime, unsigned int dec, int *warnings,
556  bool truncate);
557 bool my_timeval_round(struct timeval *tv, unsigned int decimals);
558 void mix_date_and_time(MYSQL_TIME *ldate, const MYSQL_TIME &my_time);
559 
560 void localtime_to_TIME(MYSQL_TIME *to, const struct tm *from);
561 void calc_time_from_sec(MYSQL_TIME *to, long long int seconds,
562  long microseconds);
563 bool calc_time_diff(const MYSQL_TIME &my_time1, const MYSQL_TIME &my_time2,
564  int l_sign, long long int *seconds_out,
565  long *microseconds_out);
566 int my_time_compare(const MYSQL_TIME &my_time_a, const MYSQL_TIME &my_time_b);
567 
568 long long int TIME_to_longlong_packed(const MYSQL_TIME &my_time,
569  enum enum_field_types type);
570 
572  long long int packed_value);
573 
575  long long int packed_value);
576 
578  long long int packed_value);
579 
580 /**
581  @} (end of ingroup MY_TIME)
582 */
583 #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:391
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:1000
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:2000
constexpr const int TIMESTAMP_MAX_VALUE
Definition: my_time.h:77
static int interval
Definition: mysqladmin.cc:63
constexpr const my_time_flags_t TIME_DATETIME_ONLY
Only allow full datetimes.
Definition: my_time.h:88
Definition: my_time.h:400
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:193
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:1304
Definition: my_time.h:401
bool neg
Definition: my_time.h:161
long long int TIME_to_longlong_datetime_packed(const MYSQL_TIME &my_time)
Convert datetime to packed numeric datetime representation.
Definition: my_time.cc:1773
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:2526
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:258
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:329
Definition: my_time.h:396
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:1192
void my_date_to_binary(const MYSQL_TIME *ltime, unsigned char *ptr)
Convert in-memory date representation to on-disk representation.
Definition: my_time.cc:1988
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:489
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:233
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:1447
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:2465
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:1330
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:2591
constexpr const int TIME_MAX_MINUTE
Definition: my_time.h:119
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:2062
Definition: my_time.h:402
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:148
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:2799
void adjust_time_range(MYSQL_TIME *, int *warning)
Adjust &#39;time&#39; value to lie in the MYSQL_TIME range.
Definition: my_time.cc:906
const uchar days_in_month[]
Definition: my_time.cc:94
unsigned long int day
Definition: my_time.h:156
[] 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:245
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:1893
unsigned int hour
Definition: mysql_time.h:65
unsigned long long int second
Definition: my_time.h:159
Definition: my_time.h:387
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:2550
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:515
Definition: my_time.h:389
int warnings
Definition: my_time.h:144
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:1524
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:360
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:222
Definition: my_time.h:388
Structure to return status from str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
Definition: my_time.h:143
Definition: my_time.h:399
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:478
unsigned long int month
Definition: my_time.h:155
constexpr const int MYSQL_TIME_WARN_OUT_OF_RANGE
Definition: my_time.h:107
void my_init_time()
Prepare offset of system time zone from UTC for my_system_gmt_sec() func.
Definition: my_time.cc:918
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:865
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:1687
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:1566
Definition: my_time.h:398
constexpr const int TIME_MAX_HOUR
Limits for the TIME data type.
Definition: my_time.h:118
interval_type
Available interval types used in any statement.
Definition: my_time.h:384
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:157
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:183
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:2769
constexpr const my_time_t MY_TIME_T_MIN
Definition: my_time.h:68
Definition: my_time.h:404
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:2642
Struct representing a duration.
Definition: my_time.h:153
Definition: my_time.h:390
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:963
constexpr const int TIME_MAX_VALUE_SECONDS
Definition: my_time.h:128
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:2696
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:454
constexpr const int MYSQL_TIME_WARN_INVALID_TIMESTAMP
Definition: my_time.h:108
constexpr const long int SECONDS_IN_24H
Usefull constants.
Definition: my_time.h:115
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
unsigned int fractional_digits
Definition: my_time.h:145
constexpr const int MYSQL_TIME_NOTE_TRUNCATED
Definition: my_time.h:110
constexpr const unsigned int WEEK_YEAR
Definition: my_time.h:135
long long int TIME_to_longlong_date_packed(const MYSQL_TIME &my_time)
Convert date to packed numeric date representation.
Definition: my_time.cc:1789
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:1474
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:1957
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:432
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:212
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:2104
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:106
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:1928
constexpr const int MYSQL_TIME_WARN_DATETIME_OVERFLOW
Definition: my_time.h:112
const ulonglong log_10_int[20]
Definition: my_time.cc:61
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:2413
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:2330
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:136
Definition: my_time.h:394
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:138
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:120
constexpr const int MYSQL_TIME_WARN_ZERO_DATE
Definition: my_time.h:109
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:2606
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:1647
void datetime_to_time(MYSQL_TIME *ltime)
"Casts" MYSQL_TIME datetime to a MYSQL_TIME time.
Definition: my_time.h:501
constexpr const my_time_flags_t TIME_STRICT_COLON
Allow only HH:MM:SS or MM:SS time formats.
Definition: my_time.h:103
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:125
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:2486
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:168
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:1843
Definition: my_time.h:395
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:2350
unsigned int year_2000_handling(unsigned int year)
Handle 2 digit year conversions.
Definition: my_time.cc:947
long long int TIME_to_longlong_time_packed(const MYSQL_TIME &my_time)
Convert time value to numeric packed representation.
Definition: my_time.cc:1602
Definition: my_time.h:397
void my_datetime_trunc(MYSQL_TIME *ltime, unsigned int decimals)
Alias for my_time_trunc.
Definition: my_time.h:443
Definition: my_time.h:393
unsigned long long int second_part
Definition: my_time.h:160
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:2020
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:2369
bool check_fuzzy_date(const MYSQL_TIME &my_time, my_time_flags_t fuzzydate)
Predicate for fuzzyness of date.
Definition: my_time.h:466
void TIME_from_longlong_time_packed(MYSQL_TIME *ltime, long long int nr)
Convert time packed numeric representation to time.
Definition: my_time.cc:1616
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:1461
#define L
Definition: ctype-tis620.cc:73
Definition: my_time.h:385
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:420
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:1862
bool check_time_range_quick(const MYSQL_TIME &my_time)
Check TIME range.
Definition: my_time.cc:239
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:1214
const char my_zero_datetime6[]
Definition: my_time.cc:82
unsigned long int hour
Definition: my_time.h:157
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:1811
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:1549
Definition: my_time.h:403
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:2194
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:1284
static STATUS status
Definition: mysql.cc:191
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:1366
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:2447
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:333
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:2508
constexpr const unsigned int WEEK_MONDAY_FIRST
Flags for calc_week() function.
Definition: my_time.h:134
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:272
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:257
long long int year_to_longlong_datetime_packed(long year)
Convert year to packed numeric date representation.
Definition: my_time.cc:1800
void TIME_set_hhmmss(MYSQL_TIME *ltime, unsigned int hhmmss)
Set hour, minute and secondr from a number.
Definition: my_time.cc:1497
unsigned int nanoseconds
Definition: my_time.h:146
constexpr const int MYSQL_TIME_WARN_ZERO_IN_DATE
Definition: my_time.h:111
Definition: my_time.h:392
constexpr const int DATETIME_MAX_DECIMALS
Definition: my_time.h:131
Definition: my_time.h:405
void TIME_set_yymmdd(MYSQL_TIME *ltime, unsigned int yymmdd)
Set day, month and year from a number.
Definition: my_time.cc:1485
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:158
bool str_to_time(const char *str, std::size_t length, MYSQL_TIME *l_time, MYSQL_TIME_STATUS *status, my_time_flags_t flags=0)
Convert a time string to a MYSQL_TIME struct.
Definition: my_time.cc:663
unsigned long int year
Definition: my_time.h:154
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:2740
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:283
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:530
Definition: my_time.h:386