MySQL 8.3.0
Source Code Documentation
sql_time.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2023, Oracle and/or its affiliates.
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 "my_dbug.h"
46#include "my_inttypes.h"
47#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
53class THD;
54class Time_zone;
55class my_decimal;
56
57/**
58 Representation of time formats.
59 */
63};
64
65/**
66 Collection of strings describing date/time formats.
67 */
69 const char *format_name;
70 const char *date_format;
71 const char *datetime_format;
72 const char *time_format;
73};
74
76 bool *not_exist);
78 const Time_zone &tz,
79 my_timeval *tm, int *warnings);
80bool datetime_to_timeval(const MYSQL_TIME *t, const Time_zone &tz,
81 my_timeval *tm, int *warnings);
84bool decimal_to_datetime(const my_decimal *decimal, MYSQL_TIME *ltime,
86bool decimal_to_time(const my_decimal *decimal, MYSQL_TIME *ltime);
87bool double_to_datetime(double nr, MYSQL_TIME *ltime, my_time_flags_t flags);
88bool double_to_time(double nr, MYSQL_TIME *ltime);
89
92bool my_double_to_datetime_with_warn(double nr, MYSQL_TIME *ltime,
96bool my_decimal_to_time_with_warn(const my_decimal *decimal, MYSQL_TIME *ltime);
97bool my_double_to_time_with_warn(double nr, MYSQL_TIME *ltime);
100void time_to_datetime(THD *thd, const MYSQL_TIME *tm, const Time_zone &tz,
101 MYSQL_TIME *dt);
102void time_to_datetime(THD *thd, const MYSQL_TIME *in, MYSQL_TIME *out);
103
106 const ErrConvString &val,
108 const char *field_name);
109
110const char *get_date_time_format_str(const Known_date_time_format *format,
112void make_date(const Date_time_format *format, const MYSQL_TIME *l_time,
113 String *str);
114void make_time(const Date_time_format *format, const MYSQL_TIME *l_time,
115 String *str, uint dec);
116void make_datetime(const Date_time_format *format, const MYSQL_TIME *l_time,
117 String *str, uint dec);
118bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec);
119
120void propagate_datetime_overflow_helper(THD *thd, int *warnings);
121
122/**
123 Wrapper function which will propagate any DATETIME_OVERFLOW warnings
124 to the THD. Overload for the case where a warnings bitset already
125 exists which can be inspected before the value of the mysys
126 function is passed on.
127
128 @param thd Thread context
129 @param warnings bitset used mysys function
130 @param t value mysys function which is passed on
131 @returns t
132 */
133template <class T>
134inline T propagate_datetime_overflow(THD *thd, int *warnings, T t) {
136 return t;
137}
138
139/**
140 Wrapper function which will propagate any DATETIME_OVERFLOW warnings
141 to the THD. Overload for the case when a warnings bitset must be
142 created and inspected to call a mysys function. The mysys function
143 must be invoked by the closure argument which invoked with a
144 pointer to the warnings bitset as argument.
145
146 @param thd Thread context
147 @param clos closure to call with empty warnings bitset
148 @return return value of closure
149 */
150template <class CLOS>
151inline auto propagate_datetime_overflow(THD *thd, CLOS &&clos) {
152 int warnings = 0;
153 return propagate_datetime_overflow(thd, &warnings, clos(&warnings));
154}
155
158
159bool str_to_time(const CHARSET_INFO *cs, const char *str, size_t length,
162
163/**
164 Converts a time String value to MYSQL_TIME. Forwards to the version
165 taking an explicit charset, c-string and length.
166
167 @return False on success, true on error.
168 */
169inline bool str_to_time(const String *str, MYSQL_TIME *ltime,
171 return str_to_time(str->charset(), str->ptr(), str->length(), ltime, flags,
172 status);
173}
174
175bool str_to_datetime(const CHARSET_INFO *cs, const char *str, size_t length,
178
179/**
180 Converts a datetime String value to MYSQL_TIME. Forwards to the
181 version taking an explicit charset, c-string and length.
182
183 @return False on success, true on error.
184*/
185inline bool str_to_datetime(const String *str, MYSQL_TIME *ltime,
187 return str_to_datetime(str->charset(), str->ptr(), str->length(), ltime,
188 flags, status);
189}
190
192
195 longlong packed_value);
196
197/**
198 Return the timstamp value corresponding the field type passed as argument.
199
200 @param type field type
201 @return corresponding timestamp type
202 */
204 enum enum_field_types type) {
205 switch (type) {
206 case MYSQL_TYPE_TIME:
208 case MYSQL_TYPE_DATE:
213 default:
215 }
216}
217
218/**
219 This function gets GMT time and adds value of time_zone to get
220 the local time. This function is used when server wants a timestamp
221 value from dictionary system.
222
223 @return time converted to local time
224*/
225
227
228/**
229 In lieu of a proper constructor for the C struct MYSQL_TIME, this method
230 initializes the struct.
231*/
232MYSQL_TIME my_time_set(uint y, uint m, uint d, uint h, uint mi, uint s,
233 unsigned long ms, bool negative,
235/**
236 Return the number of significant second fraction decimals in ts, e.g.
237 for ts->second_part == 120300, return 4.
238
239 @param ts the time value for which we want the number of decmals
240 @return the number of decimals
241*/
242uint actual_decimals(const MYSQL_TIME *ts);
243
244/**
245 For a time fraction with a given number of decimals, return maximum fraction,
246 if any, can be are present in a time value. For example, if 2 decimals are
247 specified, return 990000. If none, 0.
248
249 @param decimals the number of decimals
250 @return the maximum fraction
251*/
252size_t max_fraction(uint decimals);
253
254/**
255 @} (end of ingroup SQL_TIME)
256*/
257
258#endif /* SQL_TIME_INCLUDED */
Definition: sql_error.h:224
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:62
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
This class represents abstract time zone and provides basic interface for MYSQL_TIME <-> my_time_t co...
Definition: tztime.h:48
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:94
This file contains the field type.
enum_field_types
Column types for MySQL Note: Keep include/mysql/components/services/bits/stored_program_bits....
Definition: field_types.h:54
@ MYSQL_TYPE_TIME
Definition: field_types.h:66
@ MYSQL_TYPE_DATE
Definition: field_types.h:65
@ MYSQL_TYPE_TIMESTAMP
Definition: field_types.h:62
@ MYSQL_TYPE_DATETIME
Definition: field_types.h:67
static const std::string dec("DECRYPTION")
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:640
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:812
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:732
bool decimal_to_datetime(const my_decimal *decimal, MYSQL_TIME *ltime, my_time_flags_t flags)
Convert decimal value to datetime.
Definition: sql_time.cc:240
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:678
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:308
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:780
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:595
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:706
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:134
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:843
uint actual_decimals(const MYSQL_TIME *ts)
Return the number of significant second fraction decimals in ts, e.g.
Definition: sql_time.cc:870
bool my_double_to_time_with_warn(double nr, MYSQL_TIME *ltime)
Convert double number to TIME.
Definition: sql_time.cc:447
bool my_longlong_to_time_with_warn(longlong nr, MYSQL_TIME *ltime)
Convert longlong number to TIME.
Definition: sql_time.cc:475
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:793
const LEX_CSTRING interval_type_to_name[]
Name description of interval names used in statements.
Definition: sql_time.cc:69
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:203
size_t max_fraction(uint decimals)
For a time fraction with a given number of decimals, return maximum fraction, if any,...
Definition: sql_time.cc:879
bool double_to_datetime(double nr, MYSQL_TIME *ltime, my_time_flags_t flags)
Convert double value to datetime.
Definition: sql_time.cc:291
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:127
bool datetime_to_timeval(const MYSQL_TIME *t, const Time_zone &tz, my_timeval *tm, int *warnings)
Convert a datetime MYSQL_TIME representation to corresponding "struct timeval" value.
Definition: sql_time.cc:580
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:148
void make_date(const Date_time_format *format, const MYSQL_TIME *l_time, String *str)
Convert DATE value to String.
Definition: sql_time.cc:692
bool double_to_time(double nr, MYSQL_TIME *ltime)
Convert double number to TIME.
Definition: sql_time.cc:430
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:864
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:173
bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec)
Convert TIME/DATE/DATETIME value to String.
Definition: sql_time.cc:720
bool decimal_to_time(const my_decimal *decimal, MYSQL_TIME *ltime)
Convert decimal number to TIME.
Definition: sql_time.cc:383
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:339
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:258
bool datetime_with_no_zero_in_date_to_timeval(const MYSQL_TIME *t, const Time_zone &tz, my_timeval *tm, int *warnings)
Converts a datetime in MYSQL_TIME representation to corresponding struct timeval value.
Definition: sql_time.cc:514
bool my_decimal_to_time_with_warn(const my_decimal *decimal, MYSQL_TIME *ltime)
Convert decimal number to TIME.
Definition: sql_time.cc:400
my_time_t TIME_to_timestamp(const MYSQL_TIME *t, const Time_zone &tz, bool *not_exist)
void time_to_datetime(THD *thd, const MYSQL_TIME *tm, const Time_zone &tz, MYSQL_TIME *dt)
static int flags[50]
Definition: hp_test1.cc:39
A better implementation of the UNIX ctype(3) library.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
long long int longlong
Definition: my_inttypes.h:54
Interface for low level time utilities.
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:93
interval_type
Available interval types used in any statement.
Definition: my_time.h:454
int64_t my_time_t
Portable time_t replacement.
Definition: my_time_t.h:31
Time declarations shared between the server and client API: you should not add anything to this heade...
enum_mysql_timestamp_type
Definition: mysql_time.h:44
@ MYSQL_TIMESTAMP_TIME
Stores hour, minute, second and microsecond.
Definition: mysql_time.h:59
@ MYSQL_TIMESTAMP_DATETIME
Stores all date and time components.
Definition: mysql_time.h:56
@ MYSQL_TIMESTAMP_DATE
Stores year, month and day components.
Definition: mysql_time.h:49
@ MYSQL_TIMESTAMP_NONE
Definition: mysql_time.h:45
static int interval
Definition: mysqladmin.cc:69
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1065
Definition: commit_order_queue.h:33
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
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:60
required string type
Definition: replication_group_member_actions.proto:33
Our own string classes, used pervasively throughout the executor.
Definition: m_ctype.h:422
Representation of time formats.
Definition: sql_time.h:60
LEX_CSTRING format
Definition: sql_time.h:62
uchar positions[8]
Definition: sql_time.h:61
Struct representing a duration.
Definition: my_time.h:227
Collection of strings describing date/time formats.
Definition: sql_time.h:68
const char * time_format
Definition: sql_time.h:72
const char * datetime_format
Definition: sql_time.h:71
const char * date_format
Definition: sql_time.h:70
const char * format_name
Definition: sql_time.h:69
Definition: mysql_lex_string.h:39
Structure to return status from str_to_datetime(), str_to_time(), number_to_datetime(),...
Definition: my_time.h:169
Definition: mysql_time.h:81
Replacement of system's struct timeval to ensure we can carry 64 bit values even on a platform which ...
Definition: my_time_t.h:44
Include file for Sun RPC to compile out of the box.