MySQL 8.4.2
Source Code Documentation
item_timefunc.cc File Reference

This file defines all time functions. More...

#include "sql/item_timefunc.h"
#include "my_config.h"
#include <sys/time.h>
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "decimal.h"
#include "lex_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_sys.h"
#include "my_systime.h"
#include "mysql/strings/int2str.h"
#include "mysql/strings/m_ctype.h"
#include "mysql/strings/my_strtoll10.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "nulls.h"
#include "sql-common/my_decimal.h"
#include "sql/current_thd.h"
#include "sql/dd/info_schema/table_stats.h"
#include "sql/dd/object_id.h"
#include "sql/derror.h"
#include "sql/parse_tree_node_base.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_locale.h"
#include "sql/sql_time.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/tztime.h"
#include "string_with_len.h"
#include "strmake.h"
#include "template_utils.h"
#include "typelib.h"

Functions

static bool adjust_time_range_with_warn (MYSQL_TIME *ltime, uint8 decimals)
 Check and adjust a time value with a warning. More...
 
static bool sec_to_time (lldiv_t seconds, MYSQL_TIME *ltime)
 
static bool extract_date_time (const Date_time_format *format, const char *val, size_t length, MYSQL_TIME *l_time, enum_mysql_timestamp_type cached_timestamp_type, const char **sub_pattern_end, const char *date_time_type, enum_field_types data_type, my_time_flags_t flags)
 Extract datetime value to MYSQL_TIME struct from string value according to format string. More...
 
bool make_date_time (Date_time_format *format, MYSQL_TIME *l_time, enum_mysql_timestamp_type type, String *str)
 Create a formatted date/time value in a string. More...
 
static bool get_interval_info (Item *args, String *str_value, bool *is_negative, uint count, ulonglong *values, bool transform_msec)
 
static uint week_mode (uint mode)
 
bool get_interval_value (Item *args, interval_type int_type, String *str_value, Interval *interval)
 Convert a string to a interval value. More...
 
static bool date_should_be_null (enum_field_types target_type, const MYSQL_TIME &time, my_time_flags_t fuzzy_date)
 Determines whether this date should be NULL (and a warning raised) under the given sql_mode. More...
 

Variables

static constexpr const Known_date_time_format known_date_time_formats [6]
 Array of known date_time formats. More...
 
static const Date_time_format time_ampm_format = {{0}, {"%I:%i:%S %p", 11}}
 
static const Date_time_format time_24hrs_format = {{0}, {"%H:%i:%S", 8}}
 
const char * interval_names []
 

Detailed Description

This file defines all time functions.

Function Documentation

◆ adjust_time_range_with_warn()

static bool adjust_time_range_with_warn ( MYSQL_TIME ltime,
uint8  decimals 
)
static

Check and adjust a time value with a warning.

Parameters
ltimeTime variable.
decimalsPrecision.
Return values
Trueon error, false of success.

◆ date_should_be_null()

static bool date_should_be_null ( enum_field_types  target_type,
const MYSQL_TIME time,
my_time_flags_t  fuzzy_date 
)
static

Determines whether this date should be NULL (and a warning raised) under the given sql_mode.

Zeroes are allowed in the date if the data type is TIME.

Parameters
target_typeThe data type of the time/date.
timeDate and time data
fuzzy_dateWhat sql_mode dictates.
Returns
Whether the result is valid or NULL.

◆ extract_date_time()

static bool extract_date_time ( const Date_time_format format,
const char *  val,
size_t  length,
MYSQL_TIME l_time,
enum_mysql_timestamp_type  cached_timestamp_type,
const char **  sub_pattern_end,
const char *  date_time_type,
enum_field_types  data_type,
my_time_flags_t  flags 
)
static

Extract datetime value to MYSQL_TIME struct from string value according to format string.

Parameters
formatdate/time format specification
valString to decode
lengthLength of string
l_timeStore result here
cached_timestamp_typeIt uses to get an appropriate warning in the case when the value is truncated.
sub_pattern_endif non-zero then we are parsing string which should correspond compound specifier (like T or r) and this parameter is pointer to place where pointer to end of string matching this specifier should be stored.
date_time_type"time" or "datetime", used for the error/warning msg
data_typeType of data
flagsflags used by check_date()
Note
Possibility to parse strings matching to patterns equivalent to compound specifiers is mainly intended for use from inside of this function in order to understand T and r conversion specifiers, so number of conversion specifiers that can be used in such sub-patterns is limited. Also most of checks are skipped in this case.
If one adds new format specifiers to this function he should also consider adding them to Item_func_str_to_date::fix_from_format().
Return values
0ok
1error

◆ get_interval_info()

static bool get_interval_info ( Item args,
String str_value,
bool *  is_negative,
uint  count,
ulonglong values,
bool  transform_msec 
)
static

Get a array of positive numbers from a string object. Each number is separated by 1 non digit character Return error if there is too many numbers. If there is too few numbers, assume that the numbers are left out from the high end. This allows one to give: DAY_TO_SECOND as "D MM:HH:SS", "MM:HH:SS" "HH:SS" or as seconds.

Parameters
argsitem expression which we convert to an ASCII string
str_valuestring buffer
is_negativeset to true if interval is prefixed by '-'
countcount of elements in result array
valuesarray of results
transform_msecif value is true we suppose that the last part of string value is microseconds and we should transform value to six digit value. For example, '1.1' -> '1.100000'

◆ get_interval_value()

bool get_interval_value ( Item args,
interval_type  int_type,
String str_value,
Interval interval 
)

Convert a string to a interval value.

To make code easy, allow interval objects without separators.

◆ make_date_time()

bool make_date_time ( Date_time_format format,
MYSQL_TIME l_time,
enum_mysql_timestamp_type  type,
String str 
)

Create a formatted date/time value in a string.

◆ sec_to_time()

static bool sec_to_time ( lldiv_t  seconds,
MYSQL_TIME ltime 
)
static

◆ week_mode()

static uint week_mode ( uint  mode)
static

Variable Documentation

◆ interval_names

const char* interval_names[]
Initial value:
= {"year",
"quarter",
"month",
"week",
"day",
"hour",
"minute",
"second",
"microsecond",
"year_month",
"day_hour",
"day_minute",
"day_second",
"hour_minute",
"hour_second",
"minute_second",
"day_microsecond",
"hour_microsecond",
"minute_microsecond",
"second_microsecond"}

◆ known_date_time_formats

constexpr const Known_date_time_format known_date_time_formats[6]
staticconstexpr
Initial value:
= {
{"USA", "%m.%d.%Y", "%Y-%m-%d %H.%i.%s", "%h:%i:%s %p"},
{"JIS", "%Y-%m-%d", "%Y-%m-%d %H:%i:%s", "%H:%i:%s"},
{"ISO", "%Y-%m-%d", "%Y-%m-%d %H:%i:%s", "%H:%i:%s"},
{"EUR", "%d.%m.%Y", "%Y-%m-%d %H.%i.%s", "%H.%i.%s"},
{"INTERNAL", "%Y%m%d", "%Y%m%d%H%i%s", "%H%i%s"},
{nullptr, nullptr, nullptr, nullptr}}

Array of known date_time formats.

◆ time_24hrs_format

const Date_time_format time_24hrs_format = {{0}, {"%H:%i:%S", 8}}
static

◆ time_ampm_format

const Date_time_format time_ampm_format = {{0}, {"%I:%i:%S %p", 11}}
static