• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Internal PyTime_t C API: see Doc/c-api/time.rst for the documentation.
2 //
3 // The PyTime_t type is an integer to support directly common arithmetic
4 // operations such as t1 + t2.
5 //
6 // Time formats:
7 //
8 // * Seconds.
9 // * Seconds as a floating-point number (C double).
10 // * Milliseconds (10^-3 seconds).
11 // * Microseconds (10^-6 seconds).
12 // * 100 nanoseconds (10^-7 seconds), used on Windows.
13 // * Nanoseconds (10^-9 seconds).
14 // * timeval structure, 1 microsecond (10^-6 seconds).
15 // * timespec structure, 1 nanosecond (10^-9 seconds).
16 //
17 // Note that PyTime_t is now specified as int64_t, in nanoseconds.
18 // (If we need to change this, we'll need new public API with new names.)
19 // Previously, PyTime_t was configurable (in theory); some comments and code
20 // might still allude to that.
21 //
22 // Integer overflows are detected and raise OverflowError. Conversion to a
23 // resolution larger than 1 nanosecond is rounded correctly with the requested
24 // rounding mode. Available rounding modes:
25 //
26 // * Round towards minus infinity (-inf). For example, used to read a clock.
27 // * Round towards infinity (+inf). For example, used for timeout to wait "at
28 //   least" N seconds.
29 // * Round to nearest with ties going to nearest even integer. For example, used
30 //   to round from a Python float.
31 // * Round away from zero. For example, used for timeout.
32 //
33 // Some functions clamp the result in the range [PyTime_MIN; PyTime_MAX]. The
34 // caller doesn't have to handle errors and so doesn't need to hold the GIL to
35 // handle exceptions. For example, _PyTime_Add(t1, t2) computes t1+t2 and
36 // clamps the result on overflow.
37 //
38 // Clocks:
39 //
40 // * System clock
41 // * Monotonic clock
42 // * Performance counter
43 //
44 // Internally, operations like (t * k / q) with integers are implemented in a
45 // way to reduce the risk of integer overflow. Such operation is used to convert a
46 // clock value expressed in ticks with a frequency to PyTime_t, like
47 // QueryPerformanceCounter() with QueryPerformanceFrequency() on Windows.
48 
49 
50 #ifndef Py_INTERNAL_TIME_H
51 #define Py_INTERNAL_TIME_H
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 #ifndef Py_BUILD_CORE
57 #  error "this header requires Py_BUILD_CORE define"
58 #endif
59 
60 
61 #ifdef __clang__
62 struct timeval;
63 #endif
64 
65 #define _SIZEOF_PYTIME_T 8
66 
67 typedef enum {
68     // Round towards minus infinity (-inf).
69     // For example, used to read a clock.
70     _PyTime_ROUND_FLOOR=0,
71 
72     // Round towards infinity (+inf).
73     // For example, used for timeout to wait "at least" N seconds.
74     _PyTime_ROUND_CEILING=1,
75 
76     // Round to nearest with ties going to nearest even integer.
77     // For example, used to round from a Python float.
78     _PyTime_ROUND_HALF_EVEN=2,
79 
80     // Round away from zero
81     // For example, used for timeout. _PyTime_ROUND_CEILING rounds
82     // -1e-9 to 0 milliseconds which causes bpo-31786 issue.
83     // _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps
84     // the timeout sign as expected. select.poll(timeout) must block
85     // for negative values.
86     _PyTime_ROUND_UP=3,
87 
88     // _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
89     // used for timeouts.
90     _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
91 } _PyTime_round_t;
92 
93 
94 // Convert a time_t to a PyLong.
95 // Export for '_testinternalcapi' shared extension
96 PyAPI_FUNC(PyObject*) _PyLong_FromTime_t(time_t sec);
97 
98 // Convert a PyLong to a time_t.
99 // Export for '_datetime' shared extension
100 PyAPI_FUNC(time_t) _PyLong_AsTime_t(PyObject *obj);
101 
102 // Convert a number of seconds, int or float, to time_t.
103 // Export for '_datetime' shared extension.
104 PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
105     PyObject *obj,
106     time_t *sec,
107     _PyTime_round_t);
108 
109 // Convert a number of seconds, int or float, to a timeval structure.
110 // usec is in the range [0; 999999] and rounded towards zero.
111 // For example, -1.2 is converted to (-2, 800000).
112 // Export for '_datetime' shared extension.
113 PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
114     PyObject *obj,
115     time_t *sec,
116     long *usec,
117     _PyTime_round_t);
118 
119 // Convert a number of seconds, int or float, to a timespec structure.
120 // nsec is in the range [0; 999999999] and rounded towards zero.
121 // For example, -1.2 is converted to (-2, 800000000).
122 // Export for '_testinternalcapi' shared extension.
123 PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
124     PyObject *obj,
125     time_t *sec,
126     long *nsec,
127     _PyTime_round_t);
128 
129 
130 // Create a timestamp from a number of seconds.
131 // Export for '_socket' shared extension.
132 PyAPI_FUNC(PyTime_t) _PyTime_FromSeconds(int seconds);
133 
134 // Create a timestamp from a number of seconds in double.
135 extern int _PyTime_FromSecondsDouble(
136     double seconds,
137     _PyTime_round_t round,
138     PyTime_t *result);
139 
140 // Macro to create a timestamp from a number of seconds, no integer overflow.
141 // Only use the macro for small values, prefer _PyTime_FromSeconds().
142 #define _PYTIME_FROMSECONDS(seconds) \
143             ((PyTime_t)(seconds) * (1000 * 1000 * 1000))
144 
145 // Create a timestamp from a number of microseconds.
146 // Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
147 extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);
148 
149 // Create a timestamp from a Python int object (number of nanoseconds).
150 // Export for '_lsprof' shared extension.
151 PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
152     PyObject *obj);
153 
154 // Convert a number of seconds (Python float or int) to a timestamp.
155 // Raise an exception and return -1 on error, return 0 on success.
156 // Export for '_socket' shared extension.
157 PyAPI_FUNC(int) _PyTime_FromSecondsObject(PyTime_t *t,
158     PyObject *obj,
159     _PyTime_round_t round);
160 
161 // Convert a number of milliseconds (Python float or int, 10^-3) to a timestamp.
162 // Raise an exception and return -1 on error, return 0 on success.
163 // Export for 'select' shared extension.
164 PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(PyTime_t *t,
165     PyObject *obj,
166     _PyTime_round_t round);
167 
168 // Convert timestamp to a number of milliseconds (10^-3 seconds).
169 // Export for '_ssl' shared extension.
170 PyAPI_FUNC(PyTime_t) _PyTime_AsMilliseconds(PyTime_t t,
171     _PyTime_round_t round);
172 
173 // Convert timestamp to a number of microseconds (10^-6 seconds).
174 // Export for '_queue' shared extension.
175 PyAPI_FUNC(PyTime_t) _PyTime_AsMicroseconds(PyTime_t t,
176     _PyTime_round_t round);
177 
178 #ifdef MS_WINDOWS
179 // Convert timestamp to a number of 100 nanoseconds (10^-7 seconds).
180 extern PyTime_t _PyTime_As100Nanoseconds(PyTime_t t,
181     _PyTime_round_t round);
182 #endif
183 
184 // Convert a timestamp (number of nanoseconds) as a Python int object.
185 // Export for '_testinternalcapi' shared extension.
186 PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);
187 
188 #ifndef MS_WINDOWS
189 // Create a timestamp from a timeval structure.
190 // Raise an exception and return -1 on overflow, return 0 on success.
191 extern int _PyTime_FromTimeval(PyTime_t *tp, struct timeval *tv);
192 #endif
193 
194 // Convert a timestamp to a timeval structure (microsecond resolution).
195 // tv_usec is always positive.
196 // Raise an exception and return -1 if the conversion overflowed,
197 // return 0 on success.
198 // Export for 'select' shared extension.
199 PyAPI_FUNC(int) _PyTime_AsTimeval(PyTime_t t,
200     struct timeval *tv,
201     _PyTime_round_t round);
202 
203 // Similar to _PyTime_AsTimeval() but don't raise an exception on overflow.
204 // On overflow, clamp tv_sec to PyTime_t min/max.
205 // Export for 'select' shared extension.
206 PyAPI_FUNC(void) _PyTime_AsTimeval_clamp(PyTime_t t,
207     struct timeval *tv,
208     _PyTime_round_t round);
209 
210 // Convert a timestamp to a number of seconds (secs) and microseconds (us).
211 // us is always positive. This function is similar to _PyTime_AsTimeval()
212 // except that secs is always a time_t type, whereas the timeval structure
213 // uses a C long for tv_sec on Windows.
214 // Raise an exception and return -1 if the conversion overflowed,
215 // return 0 on success.
216 // Export for '_datetime' shared extension.
217 PyAPI_FUNC(int) _PyTime_AsTimevalTime_t(
218     PyTime_t t,
219     time_t *secs,
220     int *us,
221     _PyTime_round_t round);
222 
223 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
224 // Create a timestamp from a timespec structure.
225 // Raise an exception and return -1 on overflow, return 0 on success.
226 extern int _PyTime_FromTimespec(PyTime_t *tp, const struct timespec *ts);
227 
228 // Convert a timestamp to a timespec structure (nanosecond resolution).
229 // tv_nsec is always positive.
230 // Raise an exception and return -1 on error, return 0 on success.
231 // Export for '_testinternalcapi' shared extension.
232 PyAPI_FUNC(int) _PyTime_AsTimespec(PyTime_t t, struct timespec *ts);
233 
234 // Similar to _PyTime_AsTimespec() but don't raise an exception on overflow.
235 // On overflow, clamp tv_sec to PyTime_t min/max.
236 // Export for '_testinternalcapi' shared extension.
237 PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(PyTime_t t, struct timespec *ts);
238 #endif
239 
240 
241 // Compute t1 + t2. Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
242 extern PyTime_t _PyTime_Add(PyTime_t t1, PyTime_t t2);
243 
244 // Structure used by time.get_clock_info()
245 typedef struct {
246     const char *implementation;
247     int monotonic;
248     int adjustable;
249     double resolution;
250 } _Py_clock_info_t;
251 
252 // Get the current time from the system clock.
253 // On success, set *t and *info (if not NULL), and return 0.
254 // On error, raise an exception and return -1.
255 extern int _PyTime_TimeWithInfo(
256     PyTime_t *t,
257     _Py_clock_info_t *info);
258 
259 // Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
260 // The clock is not affected by system clock updates. The reference point of
261 // the returned value is undefined, so that only the difference between the
262 // results of consecutive calls is valid.
263 //
264 // Fill info (if set) with information of the function used to get the time.
265 //
266 // Return 0 on success, raise an exception and return -1 on error.
267 // Export for '_testsinglephase' shared extension.
268 PyAPI_FUNC(int) _PyTime_MonotonicWithInfo(
269     PyTime_t *t,
270     _Py_clock_info_t *info);
271 
272 
273 // Converts a timestamp to the Gregorian time, using the local time zone.
274 // Return 0 on success, raise an exception and return -1 on error.
275 // Export for '_datetime' shared extension.
276 PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
277 
278 // Converts a timestamp to the Gregorian time, assuming UTC.
279 // Return 0 on success, raise an exception and return -1 on error.
280 // Export for '_datetime' shared extension.
281 PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);
282 
283 
284 // Get the performance counter: clock with the highest available resolution to
285 // measure a short duration.
286 //
287 // Fill info (if set) with information of the function used to get the time.
288 //
289 // Return 0 on success, raise an exception and return -1 on error.
290 extern int _PyTime_PerfCounterWithInfo(
291     PyTime_t *t,
292     _Py_clock_info_t *info);
293 
294 
295 // --- _PyDeadline -----------------------------------------------------------
296 
297 // Create a deadline.
298 // Pseudo code: return PyTime_MonotonicRaw() + timeout
299 // Export for '_ssl' shared extension.
300 PyAPI_FUNC(PyTime_t) _PyDeadline_Init(PyTime_t timeout);
301 
302 // Get remaining time from a deadline.
303 // Pseudo code: return deadline - PyTime_MonotonicRaw()
304 // Export for '_ssl' shared extension.
305 PyAPI_FUNC(PyTime_t) _PyDeadline_Get(PyTime_t deadline);
306 
307 
308 // --- _PyTimeFraction -------------------------------------------------------
309 
310 typedef struct {
311     PyTime_t numer;
312     PyTime_t denom;
313 } _PyTimeFraction;
314 
315 // Set a fraction.
316 // Return 0 on success.
317 // Return -1 if the fraction is invalid.
318 extern int _PyTimeFraction_Set(
319     _PyTimeFraction *frac,
320     PyTime_t numer,
321     PyTime_t denom);
322 
323 // Compute ticks * frac.numer / frac.denom.
324 // Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
325 extern PyTime_t _PyTimeFraction_Mul(
326     PyTime_t ticks,
327     const _PyTimeFraction *frac);
328 
329 // Compute a clock resolution: frac.numer / frac.denom / 1e9.
330 extern double _PyTimeFraction_Resolution(
331     const _PyTimeFraction *frac);
332 
333 
334 #ifdef __cplusplus
335 }
336 #endif
337 #endif   // !Py_INTERNAL_TIME_H
338