MySQL 9.3.0
Source Code Documentation
my_systime.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef MY_SYSTIME_INCLUDED
26#define MY_SYSTIME_INCLUDED
27
28/**
29 @file include/my_systime.h
30 Defines for getting and processing the current system type programmatically.
31*/
32
33#include <cassert> // assert
34#include <cstdint> // std::int64_t
35#include <ctime> // time_t, struct timespec (C11/C++17)
36#include <limits> // std::numeric_limits
37#include "my_config.h"
38
39#ifdef HAVE_SYS_TIME_H
40#include <sys/time.h> // clock_gettime()
41#endif /* HAVE_SYS_TIME_H */
42
43#ifdef _WIN32
44#include <winsock2.h> // struct timeval
45#endif // _WIN32
46
47#if !defined(HAVE_CLOCK_GETTIME) || !defined(HAVE_GETTIMEOFDAY)
48#include <chrono>
49using UTC_clock = std::chrono::system_clock;
50#endif
51
52#include "my_time_t.h"
53
54struct timeval;
55
56/* Bits for get_date timeflag */
57constexpr const int GETDATE_DATE_TIME = 1;
58constexpr const int GETDATE_SHORT_DATE = 2;
59constexpr const int GETDATE_HHMMSSTIME = 4;
60constexpr const int GETDATE_GMT = 8;
61constexpr const int GETDATE_FIXEDLENGTH = 16;
62constexpr const int GETDATE_T_DELIMITER = 32;
63constexpr const int GETDATE_SHORT_DATE_FULL_YEAR = 64;
64
65/**
66 Wait a given number of microseconds.
67
68 @param micro_seconds number of microseconds to wait.
69*/
70void my_sleep(int64_t micro_seconds);
71
72#ifdef _WIN32
73
74#include <windows.h>
75
76/****************************************************************************
77** Replacements for localtime_r and gmtime_r
78****************************************************************************/
79
80inline struct tm *localtime_r(const time_t *timep, struct tm *tmp) {
81 localtime_s(tmp, timep);
82 return tmp;
83}
84
85inline struct tm *gmtime_r(const time_t *clock, struct tm *res) {
86 gmtime_s(res, clock);
87 return res;
88}
89
90/**
91 Sleep the given number of seconds. POSIX compatibility.
92
93 @param seconds number of seconds to wait
94*/
95void sleep(unsigned long seconds);
96
97#endif /* _WIN32 */
98
99/**
100 Get high-resolution time. Forwards to std::chrono on Windows.
101
102 @return current high-resolution time in multiples of 100 nanoseconds.
103*/
104inline unsigned long long int my_getsystime() {
105#ifdef HAVE_CLOCK_GETTIME
106 // Performance regression testing showed this to be preferable
107 struct timespec tp;
108 clock_gettime(CLOCK_REALTIME, &tp);
109 return (static_cast<unsigned long long int>(tp.tv_sec) * 10000000 +
110 static_cast<unsigned long long int>(tp.tv_nsec) / 100);
111#else
112 return std::chrono::duration_cast<
113 std::chrono::duration<std::int64_t, std::ratio<1, 10000000>>>(
114 UTC_clock::now().time_since_epoch())
115 .count();
116#endif /* HAVE_CLOCK_GETTIME */
117}
118
119/**
120 The maximum timespec value used to represent "inifinity" (as when
121 requesting an "infinite" timeout.
122 */
123constexpr const timespec TIMESPEC_POSINF = {
124 std::numeric_limits<decltype(timespec::tv_sec)>::max(), 999999999};
125
126/** Type alias to reduce chance of conversion errors on timeout values. */
127using Timeout_type = std::uint64_t;
128
129/** Value representing "infinite" timeout. */
130constexpr const Timeout_type TIMEOUT_INF =
132
133void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec);
134void set_timespec(struct timespec *abstime, Timeout_type sec);
135
136/**
137 Compare two timespec structs.
138
139 @retval 1 If ts1 ends after ts2.
140 @retval -1 If ts1 ends before ts2.
141 @retval 0 If ts1 is equal to ts2.
142*/
143inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
144 if (ts1->tv_sec > ts2->tv_sec ||
145 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
146 return 1;
147 if (ts1->tv_sec < ts2->tv_sec ||
148 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
149 return -1;
150 return 0;
151}
152
153/**
154 Calculate the diff between two timespec values.
155
156 @return difference in nanoseconds between ts1 and ts2
157*/
158inline unsigned long long int diff_timespec(struct timespec *ts1,
159 struct timespec *ts2) {
160 return (ts1->tv_sec - ts2->tv_sec) * 1000000000ULL + ts1->tv_nsec -
161 ts2->tv_nsec;
162}
163
164/**
165 Return current time. Takes an int argument
166 for backward compatibility. This argument is ignored.
167
168 @deprecated New code should use std::time() directly.
169
170 @retval current time.
171*/
172[[deprecated]] inline time_t my_time(int) { return time(nullptr); }
173
174/**
175 Return time in microseconds. Uses std::chrono::high_resolution_clock
176
177 @remark This function is to be used to measure performance in
178 micro seconds.
179
180 @retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000
181 (UTC)
182*/
183inline unsigned long long int my_micro_time() {
184#ifdef HAVE_GETTIMEOFDAY
185 struct timeval t;
186 /*
187 The following loop is here because gettimeofday may fail on some systems
188 */
189 while (gettimeofday(&t, nullptr) != 0) {
190 }
191 return (static_cast<unsigned long long int>(t.tv_sec) * 1000000 + t.tv_usec);
192#else
193 return std::chrono::duration_cast<std::chrono::microseconds>(
194 UTC_clock::now().time_since_epoch())
195 .count();
196#endif // HAVE_GETTIMEOFDAY
197}
198
199/**
200 Convert microseconds since epoch to my_timeval.
201 @param micro_time Microseconds.
202 @param[out] tm A timeval variable to write to.
203*/
204inline void my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm) {
205 tm->m_tv_sec = static_cast<int64_t>(micro_time / 1000000);
206 tm->m_tv_usec = static_cast<int64_t>(micro_time % 1000000);
207}
208
209/**
210
211 Convert microseconds since epoch to timeval. Prefer
212
213 my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm)
214
215 which is 64 bits safe on all platforms: Window's timeval's long
216 members are only 32 bits. Unless you need to use the host system's
217 struct timeval, of course.
218
219 @param micro_time Microseconds.
220 @param[out] tm A timeval variable to write to.
221 */
222inline void my_micro_time_to_timeval(std::uint64_t micro_time, timeval *tm) {
223 assert(static_cast<std::int64_t>(micro_time / 1000000) <=
225 tm->tv_sec = static_cast<long>(micro_time / 1000000);
226 tm->tv_usec = static_cast<long>(micro_time % 1000000);
227}
228
229void get_date(char *to, int flag, time_t date);
230
231#endif // MY_SYSTIME_INCLUDED
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:168
static int flag
Definition: hp_test1.cc:40
constexpr const int GETDATE_T_DELIMITER
Definition: my_systime.h:62
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.
Definition: my_systime.cc:67
void my_sleep(int64_t micro_seconds)
Wait a given number of microseconds.
Definition: my_systime.cc:48
time_t my_time(int)
Return current time.
Definition: my_systime.h:172
void get_date(char *to, int flag, time_t date)
Store textual representation of date in a character array.
Definition: my_systime.cc:118
int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
Compare two timespec structs.
Definition: my_systime.h:143
constexpr const timespec TIMESPEC_POSINF
The maximum timespec value used to represent "inifinity" (as when requesting an "infinite" timeout.
Definition: my_systime.h:123
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:104
constexpr const int GETDATE_GMT
Definition: my_systime.h:60
void my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm)
Convert microseconds since epoch to my_timeval.
Definition: my_systime.h:204
constexpr const int GETDATE_SHORT_DATE_FULL_YEAR
Definition: my_systime.h:63
constexpr const int GETDATE_FIXEDLENGTH
Definition: my_systime.h:61
unsigned long long int diff_timespec(struct timespec *ts1, struct timespec *ts2)
Calculate the diff between two timespec values.
Definition: my_systime.h:158
constexpr const int GETDATE_DATE_TIME
Definition: my_systime.h:57
constexpr const int GETDATE_HHMMSSTIME
Definition: my_systime.h:59
unsigned long long int my_micro_time()
Return time in microseconds.
Definition: my_systime.h:183
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.
Definition: my_systime.cc:93
constexpr const int GETDATE_SHORT_DATE
Definition: my_systime.h:58
constexpr const Timeout_type TIMEOUT_INF
Value representing "infinite" timeout.
Definition: my_systime.h:130
std::uint64_t Timeout_type
Type alias to reduce chance of conversion errors on timeout values.
Definition: my_systime.h:127
static int count
Definition: myisam_ftdump.cc:45
std::chrono::seconds seconds
Definition: authorize_manager.cc:69
ValueType max(X &&first)
Definition: gtid.h:103
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
int64_t m_tv_sec
Definition: my_time_t.h:46
int64_t m_tv_usec
Definition: my_time_t.h:47
Include file for Sun RPC to compile out of the box.