MySQL 9.1.0
Source Code Documentation
my_systime.h File Reference

Defines for getting and processing the current system type programmatically. More...

#include <time.h>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <limits>
#include <thread>
#include "my_config.h"
#include "my_time_t.h"

Go to the source code of this file.

Typedefs

using UTC_clock = std::chrono::system_clock
 
using Timeout_type = std::uint64_t
 Type alias to reduce chance of conversion errors on timeout values. More...
 

Functions

void my_sleep (time_t m_seconds)
 Wait a given number of microseconds. More...
 
unsigned long long int my_getsystime ()
 Get high-resolution time. More...
 
void set_timespec_nsec (struct timespec *abstime, Timeout_type nsec)
 Set the value of a timespec object to the current time plus a number of nanosconds. More...
 
void set_timespec (struct timespec *abstime, Timeout_type sec)
 Set the value of a timespec object to the current time plus a number of seconds using seconds. More...
 
timespec timespec_now ()
 
int cmp_timespec (struct timespec *ts1, struct timespec *ts2)
 Compare two timespec structs. More...
 
unsigned long long int diff_timespec (struct timespec *ts1, struct timespec *ts2)
 Calculate the diff between two timespec values. More...
 
time_t my_time (int)
 Return current time. More...
 
unsigned long long int my_micro_time ()
 Return time in microseconds. More...
 
unsigned long long int my_milli_time ()
 Return time in milliseconds. More...
 
void my_micro_time_to_timeval (std::uint64_t micro_time, my_timeval *tm)
 Convert microseconds since epoch to my_timeval. More...
 
void my_micro_time_to_timeval (std::uint64_t micro_time, timeval *tm)
 Convert microseconds since epoch to timeval. More...
 
void get_date (char *to, int flag, time_t date)
 Store textual representation of date in a character array. More...
 

Variables

constexpr const int GETDATE_DATE_TIME = 1
 
constexpr const int GETDATE_SHORT_DATE = 2
 
constexpr const int GETDATE_HHMMSSTIME = 4
 
constexpr const int GETDATE_GMT = 8
 
constexpr const int GETDATE_FIXEDLENGTH = 16
 
constexpr const int GETDATE_T_DELIMITER = 32
 
constexpr const int GETDATE_SHORT_DATE_FULL_YEAR = 64
 
constexpr const timespec TIMESPEC_POSINF
 The maximum timespec value used to represent "inifinity" (as when requesting an "infinite" timeout. More...
 
constexpr const Timeout_type TIMEOUT_INF
 Value representing "infinite" timeout. More...
 

Detailed Description

Defines for getting and processing the current system type programmatically.

Typedef Documentation

◆ Timeout_type

using Timeout_type = std::uint64_t

Type alias to reduce chance of conversion errors on timeout values.

◆ UTC_clock

using UTC_clock = std::chrono::system_clock

Function Documentation

◆ cmp_timespec()

int cmp_timespec ( struct timespec *  ts1,
struct timespec *  ts2 
)
inline

Compare two timespec structs.

Return values
1If ts1 ends after ts2.
-1If ts1 ends before ts2.
0If ts1 is equal to ts2.

◆ diff_timespec()

unsigned long long int diff_timespec ( struct timespec *  ts1,
struct timespec *  ts2 
)
inline

Calculate the diff between two timespec values.

Returns
difference in nanoseconds between ts1 and ts2

◆ get_date()

void get_date ( char *  to,
int  flag,
time_t  date 
)

Store textual representation of date in a character array.

Parameters
[out]tocharacter array where date will be written
flagformat of date: If flag & GETDATE_TIME Return date and time If flag & GETDATE_SHORT_DATE Return short date format YYMMDD If flag & GETDATE_HHMMSSTIME Return time in HHMMDD format. If flag & GETDATE_GMT Date/time in GMT If flag & GETDATE_FIXEDLENGTH Return fixed length date/time
datetime_t value for conversion.
Note
If flag & GETDATE_T_DELIMITER Append 'T' between date and time. If flag & GETDATE_SHORT_DATE_FULL_YEAR Return compact date format YYYYMMDD

◆ my_getsystime()

unsigned long long int my_getsystime ( )
inline

Get high-resolution time.

Forwards to std::chrono.

Returns
current high-resolution time in multiples of 100 nanoseconds.

◆ my_micro_time()

unsigned long long int my_micro_time ( )
inline

Return time in microseconds.

Uses std::chrono::high_resolution_clock

Remarks
This function is to be used to measure performance in micro seconds.
Return values
Numberof microseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)

◆ my_micro_time_to_timeval() [1/2]

void my_micro_time_to_timeval ( std::uint64_t  micro_time,
my_timeval tm 
)
inline

Convert microseconds since epoch to my_timeval.

Parameters
micro_timeMicroseconds.
[out]tmA timeval variable to write to.

◆ my_micro_time_to_timeval() [2/2]

void my_micro_time_to_timeval ( std::uint64_t  micro_time,
timeval *  tm 
)
inline

Convert microseconds since epoch to timeval.

Prefer

my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm)

which is 64 bits safe on all platforms: Window's timeval's long members are only 32 bits. Unless you need to use the host system's struct timeval, of course.

Parameters
micro_timeMicroseconds.
[out]tmA timeval variable to write to.

◆ my_milli_time()

unsigned long long int my_milli_time ( )
inline

Return time in milliseconds.

Uses std::chrono::high_resolution_clock

Remarks
This function is to be used to measure time in millisecond.
Return values
Numberof milliseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)

◆ my_sleep()

void my_sleep ( time_t  m_seconds)
inline

Wait a given number of microseconds.

Parameters
m_secondsnumber of microseconds to wait.

◆ my_time()

time_t my_time ( int  )
inline

Return current time.

Takes an int argument for backward compatibility. This argument is ignored.

Deprecated:
New code should use std::time() directly.
Return values
currenttime.

◆ set_timespec()

void set_timespec ( struct timespec *  abstime,
Timeout_type  sec 
)

Set the value of a timespec object to the current time plus a number of seconds using seconds.

Note
the sec value is capped at std::chrono::seconds::max()
Parameters
[out]abstimetime value being modified
secnumber of seconds to add to current time

◆ set_timespec_nsec()

void set_timespec_nsec ( struct timespec *  abstime,
Timeout_type  nsec 
)

Set the value of a timespec object to the current time plus a number of nanosconds.

Note
the sec value is capped at std::chrono::nanoseconds::max()
Parameters
[out]abstimetime value being modified
nsecnumber of nanoseconds to add to current time

◆ timespec_now()

timespec timespec_now ( )

Variable Documentation

◆ GETDATE_DATE_TIME

constexpr const int GETDATE_DATE_TIME = 1
constexpr

◆ GETDATE_FIXEDLENGTH

constexpr const int GETDATE_FIXEDLENGTH = 16
constexpr

◆ GETDATE_GMT

constexpr const int GETDATE_GMT = 8
constexpr

◆ GETDATE_HHMMSSTIME

constexpr const int GETDATE_HHMMSSTIME = 4
constexpr

◆ GETDATE_SHORT_DATE

constexpr const int GETDATE_SHORT_DATE = 2
constexpr

◆ GETDATE_SHORT_DATE_FULL_YEAR

constexpr const int GETDATE_SHORT_DATE_FULL_YEAR = 64
constexpr

◆ GETDATE_T_DELIMITER

constexpr const int GETDATE_T_DELIMITER = 32
constexpr

◆ TIMEOUT_INF

constexpr const Timeout_type TIMEOUT_INF
constexpr
Initial value:
=
std::numeric_limits<Timeout_type>::max() - 1

Value representing "infinite" timeout.

◆ TIMESPEC_POSINF

constexpr const timespec TIMESPEC_POSINF
constexpr
Initial value:
= {
std::numeric_limits<decltype(timespec::tv_sec)>::max(), 999999999}

The maximum timespec value used to represent "inifinity" (as when requesting an "infinite" timeout.