MySQL 9.7.0
Source Code Documentation
sql_time.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2026, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_TIME_INCLUDED
25#define SQL_TIME_INCLUDED
26
27/**
28 @ingroup SQL_TIME
29 @{
30
31 @file sql/sql_time.h
32
33 Interface for server time utilities.
34*/
35
36#include "my_config.h"
37
38#include <stddef.h>
39#ifdef HAVE_SYS_TIME_H
40#include <sys/time.h>
41#endif
42#include <sys/types.h>
43
44#include "field_types.h" // enum_field_types
45#include "lex_string.h"
46#include "my_dbug.h"
47#include "my_inttypes.h"
48#include "my_temporal.h"
49#include "my_time.h"
51#include "mysql_time.h" /* enum_mysql_timestamp_type */
52#include "sql/sql_error.h" /* Sql_condition */
53#include "sql_string.h"
54
55class THD;
56class Time_zone;
57class my_decimal;
58
59/**
60 Representation of time formats.
61 */
65};
66
67/**
68 Collection of strings describing date/time formats.
69 */
71 const char *format_name;
72 const char *date_format;
73 const char *datetime_format;
74 const char *time_format;
75};
76
78 bool *not_exist);
80 const Time_zone &tz,
81 my_timeval *tm, int *warnings);
82bool datetime_to_timeval(const MYSQL_TIME *t, const Time_zone &tz,
83 my_timeval *tm, int *warnings);
86bool decimal_to_time(const my_decimal *decimal, Time_val *time);
88 my_time_flags_t flags, int *warnings);
93bool double_to_time(double nr, Time_val *time);
104bool double_to_time_with_warn(double nr, Time_val *time);
109void time_to_date(THD *thd, const Time_val *in, Date_val *out);
110void time_to_datetime(THD *thd, const Time_val *in, Datetime_val *out);
111
114 const ErrConvString &val,
116 const char *field_name);
117
120void make_date(const Date_time_format *format, const MYSQL_TIME *l_time,
121 String *str);
122void make_time(const Date_time_format *format, const MYSQL_TIME *l_time,
123 String *str, uint dec);
124void make_datetime(const Date_time_format *format, const MYSQL_TIME *l_time,
125 String *str, uint dec);
126bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec);
127
129
130void propagate_datetime_overflow_helper(THD *thd, int *warnings);
131
132/**
133 Wrapper function which will propagate any DATETIME_OVERFLOW warnings
134 to the THD. Overload for the case where a warnings bitset already
135 exists which can be inspected before the value of the mysys
136 function is passed on.
137
138 @param thd Thread context
139 @param warnings bitset used mysys function
140 @param t value mysys function which is passed on
141 @returns t
142 */
143template <class T>
144inline T propagate_datetime_overflow(THD *thd, int *warnings, T t) {
146 return t;
147}
148
149/**
150 Wrapper function which will propagate any DATETIME_OVERFLOW warnings
151 to the THD. Overload for the case when a warnings bitset must be
152 created and inspected to call a mysys function. The mysys function
153 must be invoked by the closure argument which invoked with a
154 pointer to the warnings bitset as argument.
155
156 @param thd Thread context
157 @param clos closure to call with empty warnings bitset
158 @return return value of closure
159 */
160template <class CLOS>
161inline auto propagate_datetime_overflow(THD *thd, CLOS &&clos) {
162 int warnings = 0;
163 return propagate_datetime_overflow(thd, &warnings, clos(&warnings));
164}
165
168
169bool str_to_time(const CHARSET_INFO *cs, const char *str, size_t length,
172
173/**
174 Converts a time String value to MYSQL_TIME. Forwards to the version
175 taking an explicit charset, c-string and length.
176
177 @return False on success, true on error.
178 */
179inline bool str_to_time(const String *str, MYSQL_TIME *ltime,
181 return str_to_time(str->charset(), str->ptr(), str->length(), ltime, flags,
182 status);
183}
184
185bool str_to_datetime(const CHARSET_INFO *cs, const char *str, size_t length,
188
189/**
190 Converts a datetime String value to MYSQL_TIME. Forwards to the
191 version taking an explicit charset, c-string and length.
192
193 @return False on success, true on error.
194*/
195inline bool str_to_datetime(const String *str, MYSQL_TIME *ltime,
197 return str_to_datetime(str->charset(), str->ptr(), str->length(), ltime,
198 flags, status);
199}
200
202
205 longlong packed_value);
206
207/**
208 Return the timstamp value corresponding the field type passed as argument.
209
210 @param type field type
211 @return corresponding timestamp type
212 */
214 enum enum_field_types type) {
215 switch (type) {
216 case MYSQL_TYPE_TIME:
218 case MYSQL_TYPE_DATE:
223 default:
225 }
226}
227
228/**
229 This function gets GMT time and adds value of time_zone to get
230 the local time. This function is used when server wants a timestamp
231 value from dictionary system.
232
233 @return time converted to local time
234*/
235
237
238/**
239 In lieu of a proper constructor for the C struct MYSQL_TIME, this method
240 initializes the struct.
241*/
242MYSQL_TIME my_time_set(uint y, uint m, uint d, uint h, uint mi, uint s,
243 unsigned long ms, bool negative,
245/**
246 Return the number of significant second fraction decimals in ts, e.g.
247 for ts->second_part == 120300, return 4.
248
249 @param ts the time value for which we want the number of decmals
250 @return the number of decimals
251*/
252uint actual_decimals(const MYSQL_TIME *ts);
253
254/**
255 For a time fraction with a given number of decimals, return maximum fraction,
256 if any, can be are present in a time value. For example, if 2 decimals are
257 specified, return 990000. If none, 0.
258
259 @param decimals the number of decimals
260 @return the maximum fraction
261*/
262size_t max_fraction(uint decimals);
263
264/**
265 @} (end of ingroup SQL_TIME)
266*/
267
268#endif /* SQL_TIME_INCLUDED */
Date_val is a temporal type that represents dates within the range 0000-01-01 and 9999-12-31.
Definition: my_temporal.h:421
Definition: my_temporal.h:341
Definition: sql_error.h:228
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:66
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Time_val is a temporal type that represents only time.
Definition: my_temporal.h:57
This class represents abstract time zone and provides basic interface for MYSQL_TIME <-> my_time_t co...
Definition: tztime.h:49
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:97
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:55
@ MYSQL_TYPE_TIME
Definition: field_types.h:67
@ MYSQL_TYPE_DATE
Definition: field_types.h:66
@ MYSQL_TYPE_TIMESTAMP
Definition: field_types.h:63
@ MYSQL_TYPE_DATETIME
Definition: field_types.h:68
static const std::string dec("DECRYPTION")
bool str_to_datetime_with_warn(String *str, Datetime_val *dt, 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:196
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:889
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:1061
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:981
bool str_to_time_with_warn(String *str, Time_val *time)
Convert a time string to a time value and produce a warning if string was cut during conversion.
Definition: sql_time.cc:745
bool decimal_to_time(const my_decimal *decimal, Time_val *time)
Convert decimal number to time value.
Definition: sql_time.cc:489
bool int_to_date_with_warn(longlong val, Date_val *date, my_time_flags_t flags)
Convert integer value to date value with possible warning.
Definition: sql_time.cc:359
bool int_to_datetime_with_warn(longlong val, Datetime_val *dt, my_time_flags_t flags)
Convert integer value to datetime value with possible warning.
Definition: sql_time.cc:434
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:927
void time_to_date(THD *thd, const Time_val *in, Date_val *out)
Convert time to date.
Definition: sql_time.cc:854
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:1029
bool double_to_time(double nr, Time_val *time)
Convert double number to TIME.
Definition: sql_time.cc:553
bool decimal_to_datetime(const my_decimal *decimal, Datetime_val *dt, my_time_flags_t flags)
Convert decimal value to datetime.
Definition: sql_time.cc:263
bool decimal_to_datetime_with_warn(const my_decimal *decimal, Datetime_val *dt, my_time_flags_t flags)
Convert decimal value to datetime value with a warning.
Definition: sql_time.cc:281
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:955
bool double_to_datetime(double nr, Datetime_val *dt, my_time_flags_t flags)
Convert double value to datetime.
Definition: sql_time.cc:312
bool int_to_time_with_warn(longlong nr, Time_val *time)
Convert integer number to TIME.
Definition: sql_time.cc:614
bool double_to_datetime_with_warn(double nr, Datetime_val *dt, my_time_flags_t flags)
Convert double value to datetime value with a warning.
Definition: sql_time.cc:329
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:144
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:1087
uint actual_decimals(const MYSQL_TIME *ts)
Return the number of significant second fraction decimals in ts, e.g.
Definition: sql_time.cc:1114
bool decimal_to_date(const my_decimal *decimal, Date_val *date, my_time_flags_t flags, int *warnings)
Definition: sql_time.cc:408
bool decimal_to_time_with_warn(const my_decimal *decimal, Time_val *time)
Convert decimal number to TIME.
Definition: sql_time.cc:515
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:1042
const LEX_CSTRING interval_type_to_name[]
Name description of interval names used in statements.
Definition: sql_time.cc:70
void time_to_datetime(THD *thd, const Time_val *in, Datetime_val *out)
Convert time to datetime.
Definition: sql_time.cc:873
bool decimal_to_date_with_warn(const my_decimal *decimal, Date_val *date, my_time_flags_t flags)
Definition: sql_time.cc:394
bool double_to_time_with_warn(double nr, Time_val *time)
Convert double number to TIME.
Definition: sql_time.cc:577
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:213
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:1123
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:150
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:730
my_time_flags_t DatetimeConversionFlags(const THD *thd)
Generate flags to use when converting a string to a date or datetime value.
Definition: sql_time.cc:106
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:171
void make_date(const Date_time_format *format, const MYSQL_TIME *l_time, String *str)
Convert DATE value to String.
Definition: sql_time.cc:941
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:1108
bool my_TIME_to_str(const MYSQL_TIME *ltime, String *str, uint dec)
Convert TIME/DATE/DATETIME value to String.
Definition: sql_time.cc:969
bool double_to_date_with_warn(double nr, Date_val *date, my_time_flags_t flags)
Convert double value to date value with possible warning.
Definition: sql_time.cc:382
bool str_to_date_with_warn(String *str, Date_val *date, my_time_flags_t flags)
Definition: sql_time.cc:778
bool double_to_date(double nr, Date_val *date, my_time_flags_t flags)
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:662
my_time_t TIME_to_timestamp(const MYSQL_TIME *t, const Time_zone &tz, bool *not_exist)
static int flags[50]
Definition: hp_test1.cc:40
enum_mysql_timestamp_type
Definition: mysql_time.h:45
@ MYSQL_TIMESTAMP_TIME
Stores hour, minute, second and microsecond.
Definition: mysql_time.h:60
@ MYSQL_TIMESTAMP_DATETIME
Stores all date and time components.
Definition: mysql_time.h:57
@ MYSQL_TIMESTAMP_DATE
Stores year, month and day components.
Definition: mysql_time.h:50
@ MYSQL_TIMESTAMP_NONE
Definition: mysql_time.h:46
#define T
Definition: jit_executor_value.cc:373
A better implementation of the UNIX ctype(3) library.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
Server classes for temporal handling (DATE, TIME, DATETIME)
Interface for low level time utilities.
unsigned int my_time_flags_t
Flags to str_to_datetime and int_to_datetime.
Definition: my_time.h:87
interval_type
Available interval types used in any statement.
Definition: my_time.h:451
int64_t my_time_t
Portable time_t replacement.
Definition: my_time_t.h:32
static int interval
Definition: mysqladmin.cc:72
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1077
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
Definition: commit_order_queue.h:34
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:76
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61
required string type
Definition: replication_group_member_actions.proto:34
Our own string classes, used pervasively throughout the executor.
Definition: m_ctype.h:421
Representation of time formats.
Definition: sql_time.h:62
LEX_CSTRING format
Definition: sql_time.h:64
uchar positions[8]
Definition: sql_time.h:63
Struct representing a duration.
Definition: my_time.h:231
Collection of strings describing date/time formats.
Definition: sql_time.h:70
const char * time_format
Definition: sql_time.h:74
const char * datetime_format
Definition: sql_time.h:73
const char * date_format
Definition: sql_time.h:72
const char * format_name
Definition: sql_time.h:71
Definition: mysql_lex_string.h:40
Structure to return status from str_to_datetime(), str_to_time(), int_to_datetime(),...
Definition: my_time.h:173
Definition: mysql_time.h:82
A structure to store a decimal value together with its precision and number of decimals TODO: HCS-100...
Definition: protocol_local_v2.h:43
Replacement of system's struct timeval to ensure we can carry 64 bit values even on a platform which ...
Definition: my_time_t.h:45
Include file for Sun RPC to compile out of the box.