MySQL  8.0.18
Source Code Documentation
sql_time.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 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 SQL_TIME_INCLUDED
24 #define SQL_TIME_INCLUDED
25 
26 /**
27  @ingroup SQL_TIME
28  @{
29 
30  @file sql/sql_time.h
31 
32  Interface for server time utilities.
33 */
34 
35 #include "my_config.h"
36 
37 #include <stddef.h>
38 #ifdef HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #include <sys/types.h>
42 
43 #include "field_types.h" // enum_field_types
44 #include "lex_string.h"
45 #include "m_ctype.h"
46 #include "my_dbug.h"
47 #include "my_inttypes.h"
48 #include "my_time.h"
49 #include "mysql_time.h" /* enum_mysql_timestamp_type */
50 #include "sql/sql_error.h" /* Sql_condition */
51 #include "sql_string.h"
52 
53 class THD;
54 class my_decimal;
55 
56 /**
57  Representation of time formats.
58  */
62 };
63 
64 /**
65  Collection of strings describing date/time formats.
66  */
68  const char *format_name;
69  const char *date_format;
70  const char *datetime_format;
71  const char *time_format;
72 };
73 
74 my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, bool *not_exist);
76  struct timeval *tm,
77  int *warnings);
78 bool datetime_to_timeval(THD *thd, const MYSQL_TIME *t, struct timeval *tm,
79  int *warnings);
84 bool my_double_to_datetime_with_warn(double nr, MYSQL_TIME *ltime,
88 bool my_decimal_to_time_with_warn(const my_decimal *decimal, MYSQL_TIME *ltime);
89 bool my_double_to_time_with_warn(double nr, MYSQL_TIME *ltime);
91 bool str_to_time_with_warn(String *str, MYSQL_TIME *l_time);
92 void time_to_datetime(THD *thd, const MYSQL_TIME *tm, MYSQL_TIME *dt);
93 
96  const ErrConvString &val,
97  enum_mysql_timestamp_type time_type,
98  const char *field_name);
99 
100 const char *get_date_time_format_str(const Known_date_time_format *format,
102 void make_date(const Date_time_format *format, const MYSQL_TIME *l_time,
103  String *str);
104 void make_time(const Date_time_format *format, const MYSQL_TIME *l_time,
105  String *str, uint dec);
106 void make_datetime(const Date_time_format *format, const MYSQL_TIME *l_time,
107  String *str, uint dec);
108 bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec);
109 
110 void propagate_datetime_overflow_helper(THD *thd, int *warnings);
111 
112 /**
113  Wrapper function which will propagate any DATETIME_OVERFLOW warnings
114  to the THD. Overload for the case where a warnings bitset already
115  exists which can be inspected before the value of the mysys
116  function is passed on.
117 
118  @param thd Thread context
119  @param warnings bitset used mysys function
120  @param t value mysys function which is passed on
121  @retval t
122  */
123 template <class T>
124 inline T propagate_datetime_overflow(THD *thd, int *warnings, T t) {
125  propagate_datetime_overflow_helper(thd, warnings);
126  return t;
127 }
128 
129 /**
130  Wrapper function which will propagate any DATETIME_OVERFLOW warnings
131  to the THD. Overload for the case when a warnings bitset must be
132  created and inspected to call a mysys function. The mysys function
133  must be invoked by the closure argument which invoked with a
134  pointer to the warnings bitset as argument.
135 
136  @param thd Thread context
137  @param clos closure to call with empty warnings bitset
138  @return return value of closure
139  */
140 template <class CLOS>
141 inline auto propagate_datetime_overflow(THD *thd, CLOS &&clos) {
142  int warnings = 0;
143  return propagate_datetime_overflow(thd, &warnings, clos(&warnings));
144 }
145 
147  interval_type int_type, Interval interval);
148 
149 bool str_to_time(const CHARSET_INFO *cs, const char *str, size_t length,
152 
153 /**
154  Converts a time String value to MYSQL_TIME. Forwards to the version
155  taking an explicit charset, c-string and length.
156 
157  @return False on success, true on error.
158  */
159 inline bool str_to_time(const String *str, MYSQL_TIME *ltime,
161  return str_to_time(str->charset(), str->ptr(), str->length(), ltime, flags,
162  status);
163 }
164 
165 bool str_to_datetime(const CHARSET_INFO *cs, const char *str, size_t length,
168 
169 /**
170  Converts a datetime String value to MYSQL_TIME. Forwards to the
171  version taking an explicit charset, c-string and length.
172 
173  @return False on success, true on error.
174 */
175 inline bool str_to_datetime(const String *str, MYSQL_TIME *ltime,
177  return str_to_datetime(str->charset(), str->ptr(), str->length(), ltime,
178  flags, status);
179 }
180 
181 extern const LEX_CSTRING interval_type_to_name[];
182 
184  enum enum_field_types type,
185  longlong packed_value);
186 
187 /**
188  Return the timstamp value corresponding the field type passed as argument.
189 
190  @param type field type
191  @return corresponding timestamp type
192  */
194  enum enum_field_types type) {
195  switch (type) {
196  case MYSQL_TYPE_TIME:
197  return MYSQL_TIMESTAMP_TIME;
198  case MYSQL_TYPE_DATE:
199  return MYSQL_TIMESTAMP_DATE;
201  case MYSQL_TYPE_DATETIME:
203  default:
204  return MYSQL_TIMESTAMP_NONE;
205  }
206 }
207 
208 /**
209  This function gets GMT time and adds value of time_zone to get
210  the local time. This function is used when server wants a timestamp
211  value from dictionary system.
212 
213  @return time converted to local time
214 */
215 
217 
218 /**
219  In lieu of a proper constructor for the C struct MYSQL_TIME, this method
220  initializes the struct.
221 */
222 MYSQL_TIME my_time_set(uint y, uint m, uint d, uint h, uint mi, uint s,
223  unsigned long ms, bool negative,
225 /**
226  Return the number of significant second fraction decimals in ts, e.g.
227  for ts->second_part == 120300, return 4.
228 
229  @param ts the time value for which we wnat the number of decmals
230  @return the number of decimals
231 */
232 uint actual_decimals(const MYSQL_TIME *ts);
233 
234 /**
235  For a time fraction with a given number of decimals, return maximum fraction,
236  if any, can be are present in a time value. For example, if 2 decimals are
237  specified, return 990000. If none, 0.
238 
239  @param decimals the number of decimals
240  @return the maximum fraction
241 */
242 size_t max_fraction(uint decimals);
243 
244 /**
245  @} (end of ingroup SQL_TIME)
246 */
247 
248 #endif /* SQL_TIME_INCLUDED */
unsigned long long int ulonglong
Definition: my_inttypes.h:55
This file contains the field type.
bool str_to_datetime_with_warn(String *str, MYSQL_TIME *l_time, my_time_flags_t flags)
Convert a timestamp string to a MYSQL_TIME value and produce a warning if string was truncated during...
Definition: sql_time.cc:171
unsigned char uchar
Definition: my_inttypes.h:51
Definition: sql_error.h:220
Our own string classes, used pervasively throughout the executor.
static int interval
Definition: mysqladmin.cc:64
bool my_longlong_to_datetime_with_warn(longlong nr, MYSQL_TIME *ltime, my_time_flags_t flags)
Convert longlong value to datetime value with a warning.
Definition: sql_time.cc:295
MYSQL_TIME my_time_set(uint y, uint m, uint d, uint h, uint mi, uint s, unsigned long ms, bool negative, enum_mysql_timestamp_type type)
In lieu of a proper constructor for the C struct MYSQL_TIME, this method initializes the struct...
Definition: sql_time.cc:808
const char * datetime_format
Definition: sql_time.h:70
bool datetime_with_no_zero_in_date_to_timeval(THD *thd, const MYSQL_TIME *t, struct timeval *tm, int *warnings)
Convert a datetime MYSQL_TIME representation to corresponding "struct timeval" value.
Definition: sql_time.cc:464
const char * time_format
Definition: sql_time.h:71
Include file for Sun RPC to compile out of the box.
Some integer typedefs for easier portability.
bool my_decimal_to_datetime_with_warn(const my_decimal *decimal, MYSQL_TIME *ltime, my_time_flags_t flags)
Convert decimal value to datetime value with a warning.
Definition: sql_time.cc:232
Definition: mysql_time.h:45
Definition: field_types.h:68
Definition: mysql_lex_string.h:39
[] long int my_time_t
Portable time_t replacement.
bool make_truncated_value_warning(THD *thd, Sql_condition::enum_severity_level level, const ErrConvString &val, enum_mysql_timestamp_type time_type, const char *field_name)
Create and add a truncated value warning to the THD.
Definition: sql_time.cc:676
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
bool my_double_to_datetime_with_warn(double nr, MYSQL_TIME *ltime, my_time_flags_t flags)
Convert double value to datetime value with a warning.
Definition: sql_time.cc:264
Collection of strings describing date/time formats.
Definition: sql_time.h:67
Structure to return status from str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
Definition: my_time.h:143
const char * date_format
Definition: sql_time.h:69
void make_time(const Date_time_format *format, const MYSQL_TIME *l_time, String *str, uint dec)
Convert TIME value to String.
Definition: sql_time.cc:624
const LEX_CSTRING interval_type_to_name[]
Name description of interval names used in statements.
Definition: sql_time.cc:67
bool date_add_interval_with_warn(THD *, MYSQL_TIME *ltime, interval_type int_type, Interval interval)
Uses propagate_datetime_overflow() to handle and propagate any warnings from date_add_interval() to t...
Definition: sql_time.cc:724
interval_type
Available interval types used in any statement.
Definition: my_time.h:384
bool my_double_to_time_with_warn(double nr, MYSQL_TIME *ltime)
Convert double number to TIME.
Definition: sql_time.cc:368
void make_datetime(const Date_time_format *format, const MYSQL_TIME *l_time, String *str, uint dec)
Convert DATETIME value to String.
Definition: sql_time.cc:651
my_decimal * my_decimal_from_datetime_packed(my_decimal *dec, enum enum_field_types type, longlong packed_value)
Unpack packed numeric temporal value to date/time value and then convert to decimal representation...
Definition: sql_time.cc:756
T propagate_datetime_overflow(THD *thd, int *warnings, T t)
Wrapper function which will propagate any DATETIME_OVERFLOW warnings to the THD.
Definition: sql_time.h:124
const char * get_date_time_format_str(const Known_date_time_format *format, enum_mysql_timestamp_type type)
Return format string according format name.
Definition: sql_time.cc:586
ulonglong gmt_time_to_local_time(ulonglong time)
This function gets GMT time and adds value of time_zone to get the local time.
Definition: sql_time.cc:787
Struct representing a duration.
Definition: my_time.h:153
size_t max_fraction(uint decimals)
For a time fraction with a given number of decimals, return maximum fraction, if any, can be are present in a time value.
Definition: sql_time.cc:823
void propagate_datetime_overflow_helper(THD *thd, int *warnings)
Propagates a DATETIME_OVERFLOW warning from warnings bitfield to DA in thd.
Definition: sql_time.cc:737
my_decimal class limits &#39;decimal_t&#39; type to what we need in MySQL.
Definition: my_decimal.h:91
void make_date(const Date_time_format *format, const MYSQL_TIME *l_time, String *str)
Convert DATE value to String.
Definition: sql_time.cc:637
bool my_decimal_to_time_with_warn(const my_decimal *decimal, MYSQL_TIME *ltime)
Convert decimal number to TIME.
Definition: sql_time.cc:338
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec)
Convert TIME/DATE/DATETIME value to String.
Definition: sql_time.cc:664
unsigned int uint
Definition: uca-dump.cc:29
long long int longlong
Definition: my_inttypes.h:54
const char * format_name
Definition: sql_time.h:68
Definition: m_ctype.h:359
enum_mysql_timestamp_type field_type_to_timestamp_type(enum enum_field_types type)
Return the timstamp value corresponding the field type passed as argument.
Definition: sql_time.h:193
bool str_to_time_with_warn(String *str, MYSQL_TIME *l_time)
Convert a time string to a MYSQL_TIME struct and produce a warning if string was cut during conversio...
Definition: sql_time.cc:544
LEX_CSTRING format
Definition: sql_time.h:61
Definition: mysql_time.h:49
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:82
uchar positions[8]
Definition: sql_time.h:60
Definition: mysql_time.h:64
void time_to_datetime(THD *thd, const MYSQL_TIME *tm, MYSQL_TIME *dt)
Convert time to datetime.
Definition: sql_time.cc:569
enum_mysql_timestamp_type
Definition: mysql_time.h:44
bool my_longlong_to_time_with_warn(longlong nr, MYSQL_TIME *ltime)
Convert longlong number to TIME.
Definition: sql_time.cc:396
Definition: mysql_time.h:47
int type
Definition: http_common.h:411
bool datetime_to_timeval(THD *thd, const MYSQL_TIME *t, struct timeval *tm, int *warnings)
Convert a datetime MYSQL_TIME representation to corresponding "struct timeval" value.
Definition: sql_time.cc:529
Representation of time formats.
Definition: sql_time.h:59
bool str_to_datetime(const CHARSET_INFO *cs, const char *str, size_t length, MYSQL_TIME *l_time, my_time_flags_t flags, MYSQL_TIME_STATUS *status)
Character set-aware version of str_to_datetime().
Definition: sql_time.cc:146
bool str_to_time(const CHARSET_INFO *cs, const char *str, size_t length, MYSQL_TIME *l_time, my_time_flags_t flags, MYSQL_TIME_STATUS *status)
Character set-aware version of str_to_time().
Definition: sql_time.cc:125
Definition: field_types.h:69
Definition: mysql_time.h:48
Time declarations shared between the server and client API: you should not add anything to this heade...
const CHARSET_INFO * charset() const
Definition: sql_string.h:234
A better implementation of the UNIX ctype(3) library.
static STATUS status
Definition: mysql.cc:193
Interface for low level time utilities.
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:58
Definition: field_types.h:64
const char * ptr() const
Definition: sql_string.h:243
Definition: field_types.h:67
static int flags[50]
Definition: hp_test1.cc:39
uint actual_decimals(const MYSQL_TIME *ts)
Return the number of significant second fraction decimals in ts, e.g.
Definition: sql_time.cc:814
my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, bool *not_exist)
Convert a datetime from broken-down MYSQL_TIME representation to corresponding TIMESTAMP value...
Definition: sql_time.cc:422
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
size_t length() const
Definition: sql_string.h:235
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778