MySQL 8.0.39
Source Code Documentation

Implementation of low level time utilities. More...

#include "my_time.h"
#include <assert.h>
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstring>
#include "field_types.h"
#include "integer_digits.h"
#include "my_byteorder.h"
#include "my_systime.h"
#include "myisampack.h"
#include "template_utils.h"

Namespaces

namespace  mysys_my_time
 (end of defgroup MY_TIME)
 

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...
 
longlong mysys_my_time::DRV_my_packed_time_get_int_part (longlong i)
 
longlong mysys_my_time::DRV_my_packed_time_make (longlong i, longlong f)
 
longlong mysys_my_time::DRV_my_packed_time_make_int (longlong i)
 

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

Implementation of low level time utilities.