MySQL 8.0.40
Source Code Documentation
Collaboration diagram for Mysys time utilities:

Files

file  my_time.h
 Interface for low level time utilities.
 
file  my_time.cc
 Implementation of low level time utilities.
 

Macros

#define MAX_DATE_PARTS   8
 
#define TIMEF_OFS   0x800000000000LL
 On disk we convert from signed representation to unsigned representation using TIMEF_OFS, so all values become binary comparable. More...
 
#define TIMEF_INT_OFS   0x800000LL
 
#define DATETIMEF_INT_OFS   0x8000000000LL
 On disk we store as unsigned number with DATETIMEF_INT_OFS offset, for HA_KETYPE_BINARY compatibility purposes. More...
 

Functions

static longlong my_packed_time_get_int_part (longlong i)
 
static longlong my_packed_time_make (longlong i, longlong f)
 
static longlong my_packed_time_make_int (longlong i)
 
static int isspace_char (char ch)
 
static int isdigit_char (char ch)
 
static int ispunct_char (char ch)
 
uint calc_days_in_year (uint year)
 Calc days in one year. More...
 
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. More...
 
void set_max_hhmmss (MYSQL_TIME *tm)
 Set hour, minute and second of a MYSQL_TIME variable to maximum time value. More...
 
void set_max_time (MYSQL_TIME *tm, bool neg)
 Set MYSQL_TIME variable to maximum time value. More...
 
bool check_date (const MYSQL_TIME &my_time, bool not_zero_date, my_time_flags_t flags, int *was_cut)
 Check datetime value for validity according to flags. More...
 
bool check_time_mmssff_range (const MYSQL_TIME &my_time)
 Check if TIME fields can be adjusted to make the time value valid. More...
 
bool check_time_range_quick (const MYSQL_TIME &my_time)
 Check TIME range. More...
 
bool check_datetime_range (const MYSQL_TIME &my_time)
 Check datetime, date, or normalized time (i.e. More...
 
bool time_zone_displacement_to_seconds (const char *str, size_t length, int *result)
 Parses a time zone displacement string on the form {+-}HH:MM, converting to seconds. More...
 
bool str_to_datetime (const char *const str_arg, 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. More...
 
bool str_to_time (const char *str, std::size_t length, MYSQL_TIME *l_time, MYSQL_TIME_STATUS *status, my_time_flags_t flags)
 Convert a time string to a MYSQL_TIME struct. More...
 
bool number_to_time (longlong nr, MYSQL_TIME *ltime, int *warnings)
 Convert number to TIME. More...
 
void adjust_time_range (MYSQL_TIME *my_time, int *warning)
 Adjust 'time' value to lie in the MYSQL_TIME range. More...
 
void my_init_time ()
 Prepare offset of system time zone from UTC for my_system_gmt_sec() func. More...
 
uint year_2000_handling (uint year)
 Handle 2 digit year conversions. More...
 
long calc_daynr (uint year, uint month, uint day)
 Calculate nr of day since year 0 in new date-system (from 1615). More...
 
my_time_t my_system_gmt_sec (const MYSQL_TIME &my_time, my_time_t *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 seconds in UTC since beginning of Unix Epoch). More...
 
static char * format_two_digits (int value, char *to)
 Writes a two-digit number to a string, padded with zero if it is less than 10. More...
 
static int my_useconds_to_str (char *to, unsigned useconds, unsigned dec)
 Print the microsecond part with the specified precision. More...
 
int my_time_to_str (const MYSQL_TIME &my_time, char *to, uint dec)
 Converts a time value to a string with the format HH:MM:SS[.fraction]. More...
 
int my_date_to_str (const MYSQL_TIME &my_time, char *to)
 Converts a date value to a string with the format 'YYYY-MM-DD'. More...
 
static int TIME_to_datetime_str (const MYSQL_TIME &my_time, char *to)
 Convert datetime to a string 'YYYY-MM-DD hh:mm:ss'. More...
 
int my_datetime_to_str (const MYSQL_TIME &my_time, char *to, uint dec)
 Print a datetime value with an optional fractional part. More...
 
int my_TIME_to_str (const MYSQL_TIME &my_time, char *to, uint dec)
 Convert struct DATE/TIME/DATETIME value to string using built-in MySQL time conversion formats. More...
 
int my_timeval_to_str (const my_timeval *tm, char *to, uint dec)
 Print a timestamp with an oprional fractional part: XXXXX[.YYYYY]. More...
 
longlong number_to_datetime (longlong 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 DATETIME type as side-effect. More...
 
ulonglong TIME_to_ulonglong_datetime (const MYSQL_TIME &my_time)
 Convert time value to integer in YYYYMMDDHHMMSS. More...
 
ulonglong TIME_to_ulonglong_date (const MYSQL_TIME &my_time)
 Convert MYSQL_TIME value to integer in YYYYMMDD format. More...
 
ulonglong TIME_to_ulonglong_time (const MYSQL_TIME &my_time)
 Convert MYSQL_TIME value to integer in HHMMSS format. More...
 
void TIME_set_yymmdd (MYSQL_TIME *ltime, uint yymmdd)
 Set day, month and year from a number. More...
 
void TIME_set_hhmmss (MYSQL_TIME *ltime, uint hhmmss)
 Set hour, minute and secondr from a number. More...
 
ulonglong TIME_to_ulonglong (const MYSQL_TIME &my_time)
 Convert struct MYSQL_TIME (date and time split into year/month/day/hour/... to a number in format YYYYMMDDHHMMSS (DATETIME), YYYYMMDD (DATE) or HHMMSS (TIME). More...
 
ulonglong TIME_to_ulonglong_datetime_round (const MYSQL_TIME &my_time, int *warnings)
 Round MYSQL_TIME datetime value and convert to ulonglong representation. More...
 
ulonglong TIME_to_ulonglong_time_round (const MYSQL_TIME &my_time)
 Round MYSQL_TIME time value and convert to to ulonglong representation. More...
 
longlong TIME_to_longlong_time_packed (const MYSQL_TIME &my_time)
 Convert time value to numeric packed representation. More...
 
void TIME_from_longlong_time_packed (MYSQL_TIME *ltime, longlong tmp)
 Convert time packed numeric representation to time. More...
 
void my_time_packed_to_binary (longlong nr, uchar *ptr, uint dec)
 Convert in-memory numeric time representation to on-disk representation. More...
 
longlong my_time_packed_from_binary (const uchar *ptr, uint dec)
 Convert on-disk time representation to in-memory packed numeric representation. More...
 
longlong TIME_to_longlong_datetime_packed (const MYSQL_TIME &my_time)
 Convert datetime to packed numeric datetime representation. More...
 
longlong TIME_to_longlong_date_packed (const MYSQL_TIME &my_time)
 Convert date to packed numeric date representation. More...
 
longlong year_to_longlong_datetime_packed (long year)
 Convert year to packed numeric date representation. More...
 
void TIME_from_longlong_datetime_packed (MYSQL_TIME *ltime, longlong tmp)
 Convert packed numeric datetime representation to MYSQL_TIME. More...
 
void TIME_from_longlong_date_packed (MYSQL_TIME *ltime, longlong tmp)
 Convert packed numeric date representation to MYSQL_TIME. More...
 
longlong my_datetime_packed_from_binary (const uchar *ptr, uint dec)
 Convert on-disk datetime representation to in-memory packed numeric representation. More...
 
void my_datetime_packed_to_binary (longlong nr, uchar *ptr, uint dec)
 Store in-memory numeric packed datetime representation to disk. More...
 
void my_timestamp_from_binary (my_timeval *tm, const uchar *ptr, uint dec)
 Convert binary timestamp representation to in-memory representation. More...
 
void my_timestamp_to_binary (const my_timeval *tm, uchar *ptr, uint dec)
 Convert in-memory timestamp representation to on-disk representation. More...
 
void my_date_to_binary (const MYSQL_TIME *ltime, uchar *ptr)
 Convert in-memory date representation to on-disk representation. More...
 
longlong TIME_to_longlong_packed (const MYSQL_TIME &my_time)
 Convert a temporal value to packed numeric temporal representation, depending on its time_type. More...
 
void get_date_from_daynr (int64_t daynr, uint *ret_year, uint *ret_month, uint *ret_day)
 Change a daynr to year, month and day. More...
 
int calc_weekday (long daynr, bool sunday_first_day_of_week)
 Calc weekday from daynr. More...
 
uint calc_week (const MYSQL_TIME &my_time, uint week_behaviour, uint *year)
 Calculate the week number from a MYSQL_TIME value. More...
 
bool valid_period (long long period)
 Predicate for the validity of a period. More...
 
uint64_t convert_period_to_month (uint64_t period)
 Calculate month from period. More...
 
uint64_t convert_month_to_period (uint64_t month)
 Convert month to period. More...
 
bool date_add_interval (MYSQL_TIME *ltime, interval_type int_type, Interval interval, int *warnings)
 Add an interval to a MYSQL_TIME struct. More...
 
bool time_add_nanoseconds_with_truncate (MYSQL_TIME *ltime, uint nanoseconds, int *warnings)
 Add nanoseconds to a time value with truncation. More...
 
bool datetime_add_nanoseconds_with_truncate (MYSQL_TIME *ltime, uint nanoseconds)
 Add nanoseconds to a datetime value with truncation. More...
 
bool time_add_nanoseconds_with_round (MYSQL_TIME *ltime, uint nanoseconds, int *warnings)
 Add nanoseconds to a time value with rounding. More...
 
bool datetime_add_nanoseconds_with_round (MYSQL_TIME *ltime, uint nanoseconds, int *warnings)
 Add nanoseconds to a datetime value with rounding. More...
 
bool time_add_nanoseconds_adjust_frac (MYSQL_TIME *ltime, uint nanoseconds, int *warnings, bool truncate)
 Add nanoseconds to time and round or truncate as indicated by argument. More...
 
bool datetime_add_nanoseconds_adjust_frac (MYSQL_TIME *ltime, uint nanoseconds, int *warnings, bool truncate)
 Add nanoseconds to datetime and round or truncate as indicated by argument. More...
 
bool my_time_adjust_frac (MYSQL_TIME *ltime, uint dec, bool truncate)
 Round/Truncate time value to the given precision. More...
 
bool my_datetime_adjust_frac (MYSQL_TIME *ltime, uint dec, int *warnings, bool truncate)
 Round/Truncate datetime value to the given precision. More...
 
bool my_timeval_round (struct my_timeval *tv, uint decimals)
 Round timeval value to the given precision. More...
 
void mix_date_and_time (MYSQL_TIME *ldate, const MYSQL_TIME &my_time)
 Mix a date value and a time value. More...
 
void localtime_to_TIME (MYSQL_TIME *to, const struct tm *from)
 Converts a timepoint in a posix tm struct to a MYSQL_TIME struct. More...
 
void calc_time_from_sec (MYSQL_TIME *to, longlong seconds, long microseconds)
 Initialize MYSQL_TIME with MYSQL_TIMESTAMP_TIME from given number of seconds and microseconds. More...
 
bool calc_time_diff (const MYSQL_TIME &my_time1, const MYSQL_TIME &my_time2, int l_sign, longlong *seconds_out, long *microseconds_out)
 Calculate difference between two datetime values as seconds + microseconds. More...
 
int my_time_compare (const MYSQL_TIME &my_time_a, const MYSQL_TIME &my_time_b)
 Compare tow MYSQL_TIME objects. More...
 
longlong TIME_to_longlong_packed (const MYSQL_TIME &my_time, enum enum_field_types type)
 Convert MYSQL_TIME value to its packed numeric representation, using field type. More...
 
void TIME_from_longlong_packed (MYSQL_TIME *ltime, enum enum_field_types type, longlong packed_value)
 Convert packed numeric temporal representation to time, date or datetime, using field type. More...
 
longlong longlong_from_datetime_packed (enum enum_field_types type, longlong packed_value)
 Convert packed numeric representation to unpacked numeric representation. More...
 
double double_from_datetime_packed (enum enum_field_types type, longlong packed_value)
 Convert packed numeric temporal representation to unpacked numeric representation. More...
 

Variables

const ulonglong log_10_int [20]
 
const char my_zero_datetime6 [] = "0000-00-00 00:00:00.000000"
 
static constexpr const char time_separator = ':'
 
static constexpr ulong const days_at_timestart = 719528
 Day number with 1970-01-01 as base. More...
 
const uchar days_in_month []
 
static my_time_t my_time_zone = 0
 Offset of system time zone from UTC in seconds used to speed up work of my_system_gmt_sec() function. More...
 
static constexpr const uint msec_round_add [7]
 Rounding functions. More...
 

Detailed Description

Low-level memory and disk formats

TIME

In-memory format:
Bits Field Value range
1 sign (Used for sign, when on disk)
1 unused (Reserved for wider hour range, e.g. for intervals)
10 hour (0-838)
6 minute (0-59)
6 second (0-59)
24 microseconds (0-999999)
Total: 48 bits = 6 bytes
Format: Suhhhhhh.hhhhmmmm.mmssssss.ffffffff.ffffffff.ffffffff

DATETIME and DATE

Bits Field Value
1 sign (used when on disk)
17 year*13+month (year 0-9999, month 0-12)
5 day (0-31)
5 hour (0-23)
6 minute (0-59)
6 second (0-59)
24 microseconds (0-999999)
Total: 64 bits = 8 bytes
Format: SYYYYYYY.YYYYYYYY.YYdddddh.hhhhmmmm.mmssssss.ffffffff.ffffffff.ffffffff

Macro Definition Documentation

◆ DATETIMEF_INT_OFS

#define DATETIMEF_INT_OFS   0x8000000000LL

On disk we store as unsigned number with DATETIMEF_INT_OFS offset, for HA_KETYPE_BINARY compatibility purposes.

◆ MAX_DATE_PARTS

#define MAX_DATE_PARTS   8

◆ TIMEF_INT_OFS

#define TIMEF_INT_OFS   0x800000LL

◆ TIMEF_OFS

#define TIMEF_OFS   0x800000000000LL

On disk we convert from signed representation to unsigned representation using TIMEF_OFS, so all values become binary comparable.

Function Documentation

◆ adjust_time_range()

void adjust_time_range ( MYSQL_TIME my_time,
int *  warning 
)

Adjust 'time' value to lie in the MYSQL_TIME range.

If the time value lies outside of the range [-838:59:59, 838:59:59], set it to the closest endpoint of the range and set MYSQL_TIME_WARN_OUT_OF_RANGE flag in the 'warning' variable.

Parameters
[in,out]my_timepointer to MYSQL_TIME value
[out]warningset MYSQL_TIME_WARN_OUT_OF_RANGE flag if the value is out of range

◆ calc_daynr()

long calc_daynr ( uint  year,
uint  month,
uint  day 
)

Calculate nr of day since year 0 in new date-system (from 1615).

Parameters
yearYear (exact 4 digit year, no year conversions)
monthMonth
dayDay
Note
0000-00-00 is a valid date, and will return 0
Returns
Days since 0000-00-00

◆ calc_days_in_year()

uint calc_days_in_year ( uint  year)

Calc days in one year.

Note
Works with both two and four digit years.
Returns
number of days in that year

◆ calc_time_diff()

bool calc_time_diff ( const MYSQL_TIME my_time1,
const MYSQL_TIME my_time2,
int  l_sign,
longlong seconds_out,
long *  microseconds_out 
)

Calculate difference between two datetime values as seconds + microseconds.

Parameters
my_time1- TIME/DATE/DATETIME value
my_time2- TIME/DATE/DATETIME value
l_sign- 1 absolute values are subtracted, -1 absolute values are added.
[out]seconds_out- where difference between my_time1 and my_time2 in seconds is stored.
[out]microseconds_out- where microsecond part of difference between my_time1 and my_time2 is stored.
Note
This function calculates the difference between my_time1 and my_time2 absolute values. So one should set l_sign and correct the result if signs are to be taken into account (i.e. for MYSQL_TIME values).
Returns
Sign of difference.
Return values
1means negative result
0means positive result

◆ calc_time_from_sec()

void calc_time_from_sec ( MYSQL_TIME to,
longlong  seconds,
long  microseconds 
)

Initialize MYSQL_TIME with MYSQL_TIMESTAMP_TIME from given number of seconds and microseconds.

◆ calc_week()

uint calc_week ( const MYSQL_TIME my_time,
uint  week_behaviour,
uint year 
)

Calculate the week number from a MYSQL_TIME value.

The bits in week_format has the following meaning: WEEK_MONDAY_FIRST (0) If not set Sunday is first day of week If set Monday is first day of week WEEK_YEAR (1) If not set Week is in range 0-53

 Week 0 is returned for the the last week of the previous year (for
 a date at start of january) In this case one can get 53 for the
 first week of next year.  This flag ensures that the week is
 relevant for the given year. Note that this flag is only
 relevant if WEEK_JANUARY is not set.

                   If set   Week is in range 1-53.

 In this case one may get week 53 for a date in January (when
 the week is that last week of previous year) and week 1 for a
 date in December.

WEEK_FIRST_WEEKDAY (2) If not set Weeks are numbered according to ISO 8601:1988 If set The week that contains the first 'first-day-of-week' is week 1.

ISO 8601:1988 means that if the week containing January 1 has four or more days in the new year, then it is week 1; Otherwise it is the last week of the previous year, and the next week is week 1.

Parameters
my_timeSource time value
week_behaviourParameter controlling how weeks are counted
[out]yearThe year of the week number (which may be different from my_time.year as described above)
Returns
week number

◆ calc_weekday()

int calc_weekday ( long  daynr,
bool  sunday_first_day_of_week 
)

Calc weekday from daynr.

Return values
0for Monday
6for Sunday

◆ check_date()

bool check_date ( const MYSQL_TIME my_time,
bool  not_zero_date,
my_time_flags_t  flags,
int *  was_cut 
)

Check datetime value for validity according to flags.

Parameters
[in]my_timeDate to check.
[in]not_zero_datemy_time is not the zero date
[in]flagsflags to check (see str_to_datetime() flags in my_time.h)
[out]was_cutset to 2 if value was invalid according to flags. (Feb 29 in non-leap etc.). This remains unchanged if value is not invalid.

Here we assume that year and month is ok! If month is 0 we allow any date. (This only happens if we allow zero date parts in str_to_datetime()) Disallow dates with zero year and non-zero month and/or day.

Return values
falseOK
trueerror

◆ check_datetime_range()

bool check_datetime_range ( const MYSQL_TIME my_time)

Check datetime, date, or normalized time (i.e.

time without days) range.

Parameters
my_timeDatetime value.
Return values
falseon success
trueon error

◆ check_time_mmssff_range()

bool check_time_mmssff_range ( const MYSQL_TIME my_time)

Check if TIME fields can be adjusted to make the time value valid.

Parameters
my_timeTime value.
Return values
trueif the value cannot be made valid.
falseif the value is already valid or can be adjusted to become valid.

◆ check_time_range_quick()

bool check_time_range_quick ( const MYSQL_TIME my_time)

Check TIME range.

The value can include day part, for example: '1 10:20:30.123456'.

minute, second and second_part values are not checked unless hour is equal TIME_MAX_HOUR.

Parameters
my_timeTime value.
Returns
Test result.
Return values
falseif value is Ok.
trueif value is out of range.

◆ convert_month_to_period()

uint64_t convert_month_to_period ( uint64_t  month)

Convert month to period.

Returns
period

◆ convert_period_to_month()

uint64_t convert_period_to_month ( uint64_t  period)

Calculate month from period.

Returns
month

◆ date_add_interval()

bool date_add_interval ( MYSQL_TIME ltime,
interval_type  int_type,
Interval  interval,
int *  warnings 
)

Add an interval to a MYSQL_TIME struct.

Return values
trueif error
falseotherwise

◆ datetime_add_nanoseconds_adjust_frac()

bool datetime_add_nanoseconds_adjust_frac ( MYSQL_TIME ltime,
uint  nanoseconds,
int *  warnings,
bool  truncate 
)

Add nanoseconds to datetime and round or truncate as indicated by argument.

Parameters
[in,out]ltimeMYSQL_TIME variable to add to.
nanosecondsNanoseconds value.
[in,out]warningsWarning flag vector.
truncateDecides whether fractional part of seconds will be truncated/rounded.
Return values
Falseon success, true on error.

◆ datetime_add_nanoseconds_with_round()

bool datetime_add_nanoseconds_with_round ( MYSQL_TIME ltime,
uint  nanoseconds,
int *  warnings 
)

Add nanoseconds to a datetime value with rounding.

Parameters
[in,out]ltimeMYSQL_TIME variable to add to.
nanosecondsNanoseconds value.
[in,out]warningsWarning flag vector.
Return values
Falseon success, true on error.

◆ datetime_add_nanoseconds_with_truncate()

bool datetime_add_nanoseconds_with_truncate ( MYSQL_TIME ltime,
uint  nanoseconds 
)

Add nanoseconds to a datetime value with truncation.

Parameters
[in,out]ltimeMYSQL_TIME variable to add to.
nanosecondsNanoseconds value.
Return values
Falseon success. No real failure case here.

◆ double_from_datetime_packed()

double double_from_datetime_packed ( enum enum_field_types  type,
longlong  packed_value 
)

Convert packed numeric temporal representation to unpacked numeric representation.

Parameters
typeMySQL field type.
packed_valueNumeric packed temporal representation.
Returns
A double value in on of the following formats, depending on type: YYYYMMDD, hhmmss.ffffff or YYMMDDhhmmss.ffffff.

◆ format_two_digits()

static char * format_two_digits ( int  value,
char *  to 
)
static

Writes a two-digit number to a string, padded with zero if it is less than 10.

If the number is greater than or equal to 100, "00" is written to the string. The number should be less than 100 for valid temporal values, but the formatting functions need to handle invalid values too, since they are used for formatting the values in error/warning messages when invalid values have been given by the user.

◆ get_date_from_daynr()

void get_date_from_daynr ( int64_t  daynr,
uint ret_year,
uint ret_month,
uint ret_day 
)

Change a daynr to year, month and day.

Daynr 0 is returned as date 00.00.00

This function is called from mysqld's print_fatal_signal(). Do not make changes to this function that make that call unsafe.

◆ isdigit_char()

static int isdigit_char ( char  ch)
inlinestatic

◆ ispunct_char()

static int ispunct_char ( char  ch)
inlinestatic

◆ isspace_char()

static int isspace_char ( char  ch)
inlinestatic

◆ localtime_to_TIME()

void localtime_to_TIME ( MYSQL_TIME to,
const struct tm *  from 
)

Converts a timepoint in a posix tm struct to a MYSQL_TIME struct.

Parameters
[out]tostore converted timepoint here
fromposix tm struct holding a valid timepoint

◆ longlong_from_datetime_packed()

longlong longlong_from_datetime_packed ( enum enum_field_types  type,
longlong  packed_value 
)

Convert packed numeric representation to unpacked numeric representation.

Parameters
typeMySQL field type.
packed_valuePacked numeric temporal value.
Returns
Number in one of the following formats, depending on type: YYMMDD, YYMMDDhhmmss, hhmmss.

◆ mix_date_and_time()

void mix_date_and_time ( MYSQL_TIME ldate,
const MYSQL_TIME my_time 
)

Mix a date value and a time value.

Parameters
[in,out]ldateDate value.
my_timeTime value.

◆ my_date_to_binary()

void my_date_to_binary ( const MYSQL_TIME ltime,
uchar ptr 
)

Convert in-memory date representation to on-disk representation.

Parameters
ltimeThe value to convert.
[out]ptrThe pointer to store the value to.

◆ my_date_to_str()

int my_date_to_str ( const MYSQL_TIME my_time,
char *  to 
)

Converts a date value to a string with the format 'YYYY-MM-DD'.

This function doesn't check that the given MYSQL_TIME structure members are in the valid range. If they are not, the returned value won't reflect any valid date either.

Parameters
my_timeSource time value
[out]toDestination character array
Returns
number of characters written to 'to'

◆ my_datetime_adjust_frac()

bool my_datetime_adjust_frac ( MYSQL_TIME ltime,
uint  dec,
int *  warnings,
bool  truncate 
)

Round/Truncate datetime value to the given precision.

Parameters
[in,out]ltimeThe value to round.
decPrecision.
[in,out]warningsWarning flag vector
truncateDecides whether fractional part of seconds will be truncated/rounded.
Returns
False on success, true on error.

◆ my_datetime_packed_from_binary()

longlong my_datetime_packed_from_binary ( const uchar ptr,
uint  dec 
)

Convert on-disk datetime representation to in-memory packed numeric representation.

Parameters
ptrThe pointer to read value at.
decPrecision.
Returns
In-memory packed numeric datetime representation.

◆ my_datetime_packed_to_binary()

void my_datetime_packed_to_binary ( longlong  nr,
uchar ptr,
uint  dec 
)

Store in-memory numeric packed datetime representation to disk.

Parameters
nrIn-memory numeric packed datetime representation.
[out]ptrThe pointer to store at.
decPrecision, 1-6.

◆ my_datetime_to_str()

int my_datetime_to_str ( const MYSQL_TIME my_time,
char *  to,
uint  dec 
)

Print a datetime value with an optional fractional part.

Parameters
my_timeThe MYSQL_TIME value to print
[out]toThe string pointer to print at. This function is guaranteed not to write more than MAX_DATE_STRING_REP_LENGTH characters.
decPrecision, in the range 0..6
Returns
The length of the result string.

◆ my_init_time()

void my_init_time ( )

Prepare offset of system time zone from UTC for my_system_gmt_sec() func.

◆ my_packed_time_get_int_part()

static longlong my_packed_time_get_int_part ( longlong  i)
static

◆ my_packed_time_make()

static longlong my_packed_time_make ( longlong  i,
longlong  f 
)
static

◆ my_packed_time_make_int()

static longlong my_packed_time_make_int ( longlong  i)
static

◆ my_system_gmt_sec()

my_time_t my_system_gmt_sec ( const MYSQL_TIME my_time,
my_time_t 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 seconds in UTC since beginning of Unix Epoch).

Parameters
my_time- time value to be converted
my_timezone- pointer to a my_time_t where offset of system time zone from UTC will be stored for caching
in_dst_time_gap- set to true if time falls into spring time-gap
Note
The idea is to cache the time zone offset from UTC (including daylight saving time) for the next call to make things faster. But currently we just calculate this offset during startup (by calling my_init_time() function) and use it all the time. Time value provided should be legal time value (e.g. '2003-01-01 25:00:00' is not allowed).
Returns
Time in UTC seconds since Unix Epoch representation.

◆ my_time_adjust_frac()

bool my_time_adjust_frac ( MYSQL_TIME ltime,
uint  dec,
bool  truncate 
)

Round/Truncate time value to the given precision.

Parameters
[in,out]ltimeThe value to round.
decPrecision.
truncateDecides whether fractional part of seconds will be truncated/rounded.
Returns
False on success, true on error.

◆ my_time_compare()

int my_time_compare ( const MYSQL_TIME my_time_a,
const MYSQL_TIME my_time_b 
)

Compare tow MYSQL_TIME objects.

Return values
0if a and b are equal
-1if a comes before b
1if b comes before a

◆ my_time_packed_from_binary()

longlong my_time_packed_from_binary ( const uchar ptr,
uint  dec 
)

Convert on-disk time representation to in-memory packed numeric representation.

Parameters
ptrThe pointer to read the value at.
decPrecision.
Returns
Packed numeric time representation.

◆ my_time_packed_to_binary()

void my_time_packed_to_binary ( longlong  nr,
uchar ptr,
uint  dec 
)

Convert in-memory numeric time representation to on-disk representation.

Parameters
nrValue in packed numeric time format.
[out]ptrThe buffer to put value at.
decPrecision.

◆ my_time_to_str()

int my_time_to_str ( const MYSQL_TIME my_time,
char *  to,
uint  dec 
)

Converts a time value to a string with the format HH:MM:SS[.fraction].

This function doesn't check that the given MYSQL_TIME structure members are in the valid range. If they are not, the returned value won't reflect any valid time either. Additionally, it doesn't take into account time->day member: it's assumed that days have been converted to hours already.

Parameters
my_timeSource time value
[out]toDestnation char array
decPrecision, in the range 0..6
Returns
number of characters written to 'to'

◆ my_TIME_to_str()

int my_TIME_to_str ( const MYSQL_TIME my_time,
char *  to,
uint  dec 
)

Convert struct DATE/TIME/DATETIME value to string using built-in MySQL time conversion formats.

Note
The string must have at least MAX_DATE_STRING_REP_LENGTH bytes reserved.
Parameters
my_timeThe MYSQL_TIME value to print
[out]toThe string pointer to print at
decPrecision, in the range 0..6
Returns
number of bytes written

◆ my_timestamp_from_binary()

void my_timestamp_from_binary ( my_timeval tm,
const uchar ptr,
uint  dec 
)

Convert binary timestamp representation to in-memory representation.

Parameters
[out]tmThe variable to convert to.
ptrThe pointer to read the value from.
decPrecision.

◆ my_timestamp_to_binary()

void my_timestamp_to_binary ( const my_timeval tm,
uchar ptr,
uint  dec 
)

Convert in-memory timestamp representation to on-disk representation.

Parameters
tmThe value to convert.
[out]ptrThe pointer to store the value to.
decPrecision.

◆ my_timeval_round()

bool my_timeval_round ( struct my_timeval tv,
uint  decimals 
)

Round timeval value to the given precision.

Parameters
[in,out]tvThe value to round.
decimalsPrecision.
Returns
False on success, true on error.

◆ my_timeval_to_str()

int my_timeval_to_str ( const my_timeval tm,
char *  to,
uint  dec 
)

Print a timestamp with an oprional fractional part: XXXXX[.YYYYY].

Parameters
tmThe timestamp value to print.
[out]toThe string pointer to print at.
decPrecision, in the range 0..6.
Returns
The length of the result string.

◆ my_useconds_to_str()

static int my_useconds_to_str ( char *  to,
unsigned  useconds,
unsigned  dec 
)
static

Print the microsecond part with the specified precision.

Parameters
[out]toThe string pointer to print at
usecondsThe microseconds value
decPrecision, between 1 and 6
Returns
The length of the result string

◆ number_to_datetime()

longlong number_to_datetime ( longlong  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 DATETIME type as side-effect.

Convert a datetime value of formats YYMMDD, YYYYMMDD, YYMMDDHHMSS, YYYYMMDDHHMMSS to broken-down MYSQL_TIME representation. Return value in YYYYMMDDHHMMSS format as side-effect.

This function also checks if datetime value fits in DATETIME range.

Datetime value in YYYYMMDDHHMMSS format.

was_cut if return value -1: one of

  • MYSQL_TIME_WARN_OUT_OF_RANGE
  • MYSQL_TIME_WARN_ZERO_DATE
  • MYSQL_TIME_WARN_TRUNCATED otherwise 0.
Parameters
nrdatetime value as number
[in,out]time_respointer for structure for broken-down representation
flagsTIME_NO_ZERO_DATE and flags used by check_date()
[out]was_cut0 Value ok 1 If value was cut during conversion 2 check_date(date,flags) considers date invalid
Return values
-1Timestamp with wrong values, e.g. nr == 0 with TIME_NO_ZERO_DATE
anythingelse DATETIME as integer in YYYYMMDDHHMMSS format

◆ number_to_time()

bool number_to_time ( longlong  nr,
MYSQL_TIME ltime,
int *  warnings 
)

Convert number to TIME.

Parameters
nrNumber to convert.
[out]ltimeVariable to convert to.
[out]warningsWarning vector.
Return values
falseOK
trueNo. is out of range

◆ set_max_hhmmss()

void set_max_hhmmss ( MYSQL_TIME tm)

Set hour, minute and second of a MYSQL_TIME variable to maximum time value.

Unlike set_max_time(), does not touch the other structure members.

◆ set_max_time()

void set_max_time ( MYSQL_TIME tm,
bool  neg 
)

Set MYSQL_TIME variable to maximum time value.

Parameters
tmOUT The variable to set.
negSign: 1 if negative, 0 if positive.

◆ set_zero_time()

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.

Parameters
[out]tmThe value to set.
time_typeTimestasmp type

◆ str_to_datetime()

bool str_to_datetime ( const char *const  str_arg,
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.

DESCRIPTION At least the following formats are recognized (based on number of digits) YYMMDD, YYYYMMDD, YYMMDDHHMMSS, YYYYMMDDHHMMSS YY-MM-DD, YYYY-MM-DD, YY-MM-DD HH.MM.SS YYYYMMDDTHHMMSS where T is a the character T (ISO8601) Also dates where all parts are zero are allowed

The second part may have an optional .###### fraction part. The datetime value may be followed by a time zone displacement +/-HH:MM.

NOTES This function should work with a format position vector as long as the following things holds:

  • All date are kept together and all time parts are kept together
  • Date and time parts must be separated by blank
  • Second fractions must come after second part and be separated by a '.'. (The second fractions are optional)
  • AM/PM must come after second fractions (or after seconds if no fractions)
  • Year must always been specified.
  • If time is before date, then we will use datetime format only if the argument consist of two parts, separated by space. Otherwise we will assume the argument is a date.
  • The hour part must be specified in hour-minute-second order.

    status->warnings is set to: 0 Value OK MYSQL_TIME_WARN_TRUNCATED If value was cut during conversion MYSQL_TIME_WARN_OUT_OF_RANGE check_date(date,flags) considers date invalid

    l_time->time_type is set as follows: MYSQL_TIMESTAMP_NONE String wasn't a timestamp, like [DD [HH:[MM:[SS]]]].fraction. l_time is not changed. MYSQL_TIMESTAMP_DATE DATE string (YY MM and DD parts ok) MYSQL_TIMESTAMP_DATETIME Full timestamp MYSQL_TIMESTAMP_ERROR Timestamp with wrong values. All elements in l_time is set to 0

    flags is a bit field with the following possible values: TIME_FUZZY_DATE TIME_DATETIME_ONLY TIME_NO_ZERO_IN_DATE TIME_NO_ZERO_DATE TIME_INVALID_DATES

Parameters
str_argString to parse
lengthLength of string
[out]l_timeDate is stored here
flagsBitfield TIME_FUZZY_DATE|TIME_DATETIME_ONLY|TIME_NO_ZERO_IN_DATE|TIME_NO_ZERO_DATE|TIME_INVALID_DATES (described above)
statusConversion status and warnings
Return values
falseOk
trueError

◆ str_to_time()

bool str_to_time ( const char *  str,
std::size_t  length,
MYSQL_TIME l_time,
MYSQL_TIME_STATUS status,
my_time_flags_t  flags 
)

Convert a time string to a MYSQL_TIME struct.

status.warning is set to: MYSQL_TIME_WARN_TRUNCATED flag if the input string was cut during conversion, and/or MYSQL_TIME_WARN_OUT_OF_RANGE flag, if the value is out of range.

Note
Because of the extra days argument, this function can only work with times where the time arguments are in the above order.
Parameters
strA string in full TIMESTAMP format or [-] DAYS [H]H:MM:SS, [H]H:MM:SS, [M]M:SS, [H]HMMSS, [M]MSS or [S]S
lengthLength of str
[out]l_timeStore result here
[out]statusConversion status, including warnings.
flagsOptional flags to control conversion
Return values
falseOk
trueError

◆ time_add_nanoseconds_adjust_frac()

bool time_add_nanoseconds_adjust_frac ( MYSQL_TIME ltime,
uint  nanoseconds,
int *  warnings,
bool  truncate 
)

Add nanoseconds to time and round or truncate as indicated by argument.

Parameters
[in,out]ltimeMYSQL_TIME variable to add to.
nanosecondsNanosecons value.
[in,out]warningsWarning flag vector.
truncateDecides whether fractional part of seconds will be truncated/rounded.
Return values
Falseon success. No real failure case here.

◆ time_add_nanoseconds_with_round()

bool time_add_nanoseconds_with_round ( MYSQL_TIME ltime,
uint  nanoseconds,
int *  warnings 
)

Add nanoseconds to a time value with rounding.

Parameters
[in,out]ltimeMYSQL_TIME variable to add to.
nanosecondsNanoseconds value.
[in,out]warningsWarning flag vector.
Return values
Falseon success, true on error.

◆ time_add_nanoseconds_with_truncate()

bool time_add_nanoseconds_with_truncate ( MYSQL_TIME ltime,
uint  nanoseconds,
int *  warnings 
)

Add nanoseconds to a time value with truncation.

Parameters
[in,out]ltimeMYSQL_TIME variable to add to.
nanosecondsNanoseconds value.
[in,out]warningsWarning flag vector.
Return values
Falseon success. No real failure case here.

◆ TIME_from_longlong_date_packed()

void TIME_from_longlong_date_packed ( MYSQL_TIME ltime,
longlong  tmp 
)

Convert packed numeric date representation to MYSQL_TIME.

Parameters
[out]ltimeThe date variable to convert to.
tmpThe packed numeric date value.

◆ TIME_from_longlong_datetime_packed()

void TIME_from_longlong_datetime_packed ( MYSQL_TIME ltime,
longlong  tmp 
)

Convert packed numeric datetime representation to MYSQL_TIME.

Parameters
[out]ltimeThe datetime variable to convert to.
tmpThe packed numeric datetime value.

◆ TIME_from_longlong_packed()

void TIME_from_longlong_packed ( MYSQL_TIME ltime,
enum enum_field_types  type,
longlong  packed_value 
)

Convert packed numeric temporal representation to time, date or datetime, using field type.

Parameters
[out]ltimeThe variable to write to.
typeMySQL field type.
packed_valueNumeric datetype representation.

◆ TIME_from_longlong_time_packed()

void TIME_from_longlong_time_packed ( MYSQL_TIME ltime,
longlong  tmp 
)

Convert time packed numeric representation to time.

Parameters
[out]ltimeThe MYSQL_TIME variable to set.
tmpThe packed numeric representation.

◆ TIME_set_hhmmss()

void TIME_set_hhmmss ( MYSQL_TIME ltime,
uint  hhmmss 
)

Set hour, minute and secondr from a number.

Parameters
ltimeMYSQL_TIME variable
hhmmssNumber in HHMMSS format

◆ TIME_set_yymmdd()

void TIME_set_yymmdd ( MYSQL_TIME ltime,
uint  yymmdd 
)

Set day, month and year from a number.

Parameters
ltimeMYSQL_TIME variable
yymmddNumber in YYYYMMDD format

◆ TIME_to_datetime_str()

static int TIME_to_datetime_str ( const MYSQL_TIME my_time,
char *  to 
)
static

Convert datetime to a string 'YYYY-MM-DD hh:mm:ss'.

Open coded for better performance. This code previously resided in field.cc, in Field_timestamp::val_str().

Parameters
my_timeThe src MYSQL_TIME value.
[out]toThe string pointer to print at.
Returns
The length of the result string.

◆ TIME_to_longlong_date_packed()

longlong TIME_to_longlong_date_packed ( const MYSQL_TIME my_time)

Convert date to packed numeric date representation.

Numeric packed date format is similar to numeric packed datetime representation, with zero hhmmss part.

Parameters
my_timeThe value to convert.
Returns
Packed numeric representation of ltime.

◆ TIME_to_longlong_datetime_packed()

longlong TIME_to_longlong_datetime_packed ( const MYSQL_TIME my_time)

Convert datetime to packed numeric datetime representation.

Parameters
my_timeThe value to convert.
Returns
Packed numeric representation of my_time.

◆ TIME_to_longlong_packed() [1/2]

longlong TIME_to_longlong_packed ( const MYSQL_TIME my_time)

Convert a temporal value to packed numeric temporal representation, depending on its time_type.

Parameters
my_timeThe value to convert.
Returns
Packed numeric time/date/datetime representation.

◆ TIME_to_longlong_packed() [2/2]

longlong TIME_to_longlong_packed ( const MYSQL_TIME my_time,
enum enum_field_types  type 
)

Convert MYSQL_TIME value to its packed numeric representation, using field type.

Parameters
my_timeThe time value to convert.
typeMySQL field type.
Returns
Packed numeric representation.

◆ TIME_to_longlong_time_packed()

longlong TIME_to_longlong_time_packed ( const MYSQL_TIME my_time)

Convert time value to numeric packed representation.

Parameters
my_timeThe value to convert.
Returns
Numeric packed representation.

◆ TIME_to_ulonglong()

ulonglong TIME_to_ulonglong ( const MYSQL_TIME my_time)

Convert struct MYSQL_TIME (date and time split into year/month/day/hour/... to a number in format YYYYMMDDHHMMSS (DATETIME), YYYYMMDD (DATE) or HHMMSS (TIME).

The function is used when we need to convert value of time item to a number if it's used in numeric context, i. e.: SELECT NOW()+1, CURDATE()+0, CURTIMIE()+0; SELECT ?+1;

Parameters
my_timeSource time value
Return values
0in case of errors!
numberin format YYYYMMDDHHMMSS (DATETIME), YYYYMMDD (DATE) or HHMMSS (TIME), otherwise.
Note
This function doesn't check that given MYSQL_TIME structure members are in valid range. If they are not, return value won't reflect any valid date either.

◆ TIME_to_ulonglong_date()

ulonglong TIME_to_ulonglong_date ( const MYSQL_TIME my_time)

Convert MYSQL_TIME value to integer in YYYYMMDD format.

Parameters
my_timeThe MYSQL_TIME value to convert.
Returns
A number in format YYYYMMDD.

◆ TIME_to_ulonglong_datetime()

ulonglong TIME_to_ulonglong_datetime ( const MYSQL_TIME my_time)

Convert time value to integer in YYYYMMDDHHMMSS.

Parameters
my_timeThe MYSQL_TIME value to convert.
Returns
A number in format YYYYMMDDHHMMSS.

◆ TIME_to_ulonglong_datetime_round()

ulonglong TIME_to_ulonglong_datetime_round ( const MYSQL_TIME my_time,
int *  warnings 
)

Round MYSQL_TIME datetime value and convert to ulonglong representation.

Parameters
my_timeinput time
[out]warningswarning vector
Returns
time in (u)longlong format

◆ TIME_to_ulonglong_time()

ulonglong TIME_to_ulonglong_time ( const MYSQL_TIME my_time)

Convert MYSQL_TIME value to integer in HHMMSS format.

This function doesn't take into account time->day member: it's assumed that days have been converted to hours already.

Parameters
my_timeThe TIME value to convert.
Returns
The number in HHMMSS format.

◆ TIME_to_ulonglong_time_round()

ulonglong TIME_to_ulonglong_time_round ( const MYSQL_TIME my_time)

Round MYSQL_TIME time value and convert to to ulonglong representation.

Parameters
my_timeinput time
Returns
time in (u)longlong format

◆ time_zone_displacement_to_seconds()

bool time_zone_displacement_to_seconds ( const char *  str,
size_t  length,
int *  result 
)

Parses a time zone displacement string on the form {+-}HH:MM, converting to seconds.

Parameters
[in]strTime zone displacement string.
[in]lengthLength of said string.
[out]resultCalculated displacement in seconds.
Return values
falseOk.
trueNot a valid time zone displacement string.

◆ valid_period()

bool valid_period ( long long  period)

Predicate for the validity of a period.

◆ year_2000_handling()

uint year_2000_handling ( uint  year)

Handle 2 digit year conversions.

Parameters
year2 digit year
Returns
Year between 1970-2069

◆ year_to_longlong_datetime_packed()

longlong year_to_longlong_datetime_packed ( long  year)

Convert year to packed numeric date representation.

Packed value for YYYY is the same to packed value for date YYYY-00-00.

Returns
packed value for date YYYY-00-00.

Variable Documentation

◆ days_at_timestart

constexpr ulong const days_at_timestart = 719528
staticconstexpr

Day number with 1970-01-01 as base.

◆ days_in_month

const uchar days_in_month[]
Initial value:
= {31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31, 0}

◆ log_10_int

const ulonglong log_10_int[20]
Initial value:
= {1,
10,
100,
1000,
10000UL,
100000UL,
1000000UL,
10000000UL,
100000000ULL,
1000000000ULL,
10000000000ULL,
100000000000ULL,
1000000000000ULL,
10000000000000ULL,
100000000000000ULL,
1000000000000000ULL,
10000000000000000ULL,
100000000000000000ULL,
1000000000000000000ULL,
10000000000000000000ULL}

◆ msec_round_add

constexpr const uint msec_round_add[7]
staticconstexpr
Initial value:
= {
500000000, 50000000, 5000000, 500000, 50000, 5000, 0}

Rounding functions.

◆ my_time_zone

my_time_t my_time_zone = 0
static

Offset of system time zone from UTC in seconds used to speed up work of my_system_gmt_sec() function.

◆ my_zero_datetime6

const char my_zero_datetime6[] = "0000-00-00 00:00:00.000000"

◆ time_separator

constexpr const char time_separator = ':'
staticconstexpr