1
- // The _PyTime_t API is written to use timestamp and timeout values stored in
2
- // various formats and to read clocks.
1
+ // Internal PyTime_t C API: see Doc/c-api/time.rst for the documentation.
3
2
//
4
- // The _PyTime_t type is an integer to support directly common arithmetic
5
- // operations like t1 + t2.
3
+ // The PyTime_t type is an integer to support directly common arithmetic
4
+ // operations such as t1 + t2.
6
5
//
7
- // The _PyTime_t API supports a resolution of 1 nanosecond. The _PyTime_t type
8
- // is signed to support negative timestamps. The supported range is around
9
- // [-292.3 years; +292.3 years]. Using the Unix epoch (January 1st, 1970), the
10
- // supported date range is around [1677-09-21; 2262-04-11].
6
+ // Time formats:
11
7
//
12
- // Formats:
13
- //
14
- // * seconds
15
- // * seconds as a floating pointer number (C double)
16
- // * milliseconds (10^-3 seconds)
17
- // * microseconds (10^-6 seconds)
18
- // * 100 nanoseconds (10^-7 seconds)
19
- // * nanoseconds (10^-9 seconds)
20
- // * timeval structure, 1 microsecond resolution (10^-6 seconds)
21
- // * timespec structure, 1 nanosecond resolution (10^-9 seconds)
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).
22
16
//
23
17
// Integer overflows are detected and raise OverflowError. Conversion to a
24
- // resolution worse than 1 nanosecond is rounded correctly with the requested
25
- // rounding mode. There are 4 rounding modes: floor (towards -inf), ceiling
26
- // (towards +inf), half even and up (away from zero).
18
+ // resolution larger than 1 nanosecond is rounded correctly with the requested
19
+ // rounding mode. Available rounding modes:
20
+ //
21
+ // * Round towards minus infinity (-inf). For example, used to read a clock.
22
+ // * Round towards infinity (+inf). For example, used for timeout to wait "at
23
+ // least" N seconds.
24
+ // * Round to nearest with ties going to nearest even integer. For example, used
25
+ // to round from a Python float.
26
+ // * Round away from zero. For example, used for timeout.
27
27
//
28
- // Some functions clamp the result in the range [_PyTime_MIN; _PyTime_MAX], so
29
- // the caller doesn't have to handle errors and doesn't need to hold the GIL.
30
- // For example, _PyTime_Add(t1, t2) computes t1+t2 and clamp the result on
31
- // overflow.
28
+ // Some functions clamp the result in the range [PyTime_MIN; PyTime_MAX]. The
29
+ // caller doesn't have to handle errors and so doesn't need to hold the GIL to
30
+ // handle exceptions. For example, _PyTime_Add(t1, t2) computes t1+t2 and
31
+ // clamps the result on overflow.
32
32
//
33
33
// Clocks:
34
34
//
35
35
// * System clock
36
36
// * Monotonic clock
37
37
// * Performance counter
38
38
//
39
- // Operations like (t * k / q) with integers are implemented in a way to reduce
40
- // the risk of integer overflow. Such operation is used to convert a clock
41
- // value expressed in ticks with a frequency to _PyTime_t, like
42
- // QueryPerformanceCounter() with QueryPerformanceFrequency().
39
+ // Internally, operations like (t * k / q) with integers are implemented in a
40
+ // way to reduce the risk of integer overflow. Such operation is used to convert a
41
+ // clock value expressed in ticks with a frequency to PyTime_t, like
42
+ // QueryPerformanceCounter() with QueryPerformanceFrequency() on Windows.
43
+
43
44
44
45
#ifndef Py_INTERNAL_TIME_H
45
46
#define Py_INTERNAL_TIME_H
@@ -56,14 +57,7 @@ extern "C" {
56
57
struct timeval ;
57
58
#endif
58
59
59
- // _PyTime_t: Python timestamp with subsecond precision. It can be used to
60
- // store a duration, and so indirectly a date (related to another date, like
61
- // UNIX epoch).
62
- typedef int64_t _PyTime_t ;
63
- // _PyTime_MIN nanoseconds is around -292.3 years
64
- #define _PyTime_MIN INT64_MIN
65
- // _PyTime_MAX nanoseconds is around +292.3 years
66
- #define _PyTime_MAX INT64_MAX
60
+ typedef PyTime_t _PyTime_t ;
67
61
#define _SIZEOF_PYTIME_T 8
68
62
69
63
typedef enum {
@@ -147,7 +141,7 @@ PyAPI_FUNC(_PyTime_t) _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t
147
141
PyAPI_FUNC (_PyTime_t ) _PyTime_FromNanoseconds (_PyTime_t ns );
148
142
149
143
// Create a timestamp from a number of microseconds.
150
- // Clamp to [_PyTime_MIN; _PyTime_MAX ] on overflow.
144
+ // Clamp to [PyTime_MIN; PyTime_MAX ] on overflow.
151
145
extern _PyTime_t _PyTime_FromMicrosecondsClamp (_PyTime_t us );
152
146
153
147
// Create a timestamp from nanoseconds (Python int).
@@ -169,10 +163,6 @@ PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t,
169
163
PyObject * obj ,
170
164
_PyTime_round_t round );
171
165
172
- // Convert a timestamp to a number of seconds as a C double.
173
- // Export for '_socket' shared extension.
174
- PyAPI_FUNC (double ) _PyTime_AsSecondsDouble (_PyTime_t t );
175
-
176
166
// Convert timestamp to a number of milliseconds (10^-3 seconds).
177
167
// Export for '_ssl' shared extension.
178
168
PyAPI_FUNC (_PyTime_t ) _PyTime_AsMilliseconds (_PyTime_t t ,
@@ -250,7 +240,7 @@ PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts);
250
240
#endif
251
241
252
242
253
- // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX ] on overflow.
243
+ // Compute t1 + t2. Clamp to [PyTime_MIN; PyTime_MAX ] on overflow.
254
244
extern _PyTime_t _PyTime_Add (_PyTime_t t1 , _PyTime_t t2 );
255
245
256
246
// Structure used by time.get_clock_info()
@@ -261,36 +251,13 @@ typedef struct {
261
251
double resolution ;
262
252
} _Py_clock_info_t ;
263
253
264
- // Get the current time from the system clock.
265
- //
266
- // If the internal clock fails, silently ignore the error and return 0.
267
- // On integer overflow, silently ignore the overflow and clamp the clock to
268
- // [_PyTime_MIN; _PyTime_MAX].
269
- //
270
- // Use _PyTime_GetSystemClockWithInfo() to check for failure.
271
- // Export for '_random' shared extension.
272
- PyAPI_FUNC (_PyTime_t ) _PyTime_GetSystemClock (void );
273
-
274
254
// Get the current time from the system clock.
275
255
// On success, set *t and *info (if not NULL), and return 0.
276
256
// On error, raise an exception and return -1.
277
257
extern int _PyTime_GetSystemClockWithInfo (
278
258
_PyTime_t * t ,
279
259
_Py_clock_info_t * info );
280
260
281
- // Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
282
- // The clock is not affected by system clock updates. The reference point of
283
- // the returned value is undefined, so that only the difference between the
284
- // results of consecutive calls is valid.
285
- //
286
- // If the internal clock fails, silently ignore the error and return 0.
287
- // On integer overflow, silently ignore the overflow and clamp the clock to
288
- // [_PyTime_MIN; _PyTime_MAX].
289
- //
290
- // Use _PyTime_GetMonotonicClockWithInfo() to check for failure.
291
- // Export for '_random' shared extension.
292
- PyAPI_FUNC (_PyTime_t ) _PyTime_GetMonotonicClock (void );
293
-
294
261
// Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
295
262
// The clock is not affected by system clock updates. The reference point of
296
263
// the returned value is undefined, so that only the difference between the
@@ -315,17 +282,6 @@ PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
315
282
// Export for '_datetime' shared extension.
316
283
PyAPI_FUNC (int ) _PyTime_gmtime (time_t t , struct tm * tm );
317
284
318
- // Get the performance counter: clock with the highest available resolution to
319
- // measure a short duration.
320
- //
321
- // If the internal clock fails, silently ignore the error and return 0.
322
- // On integer overflow, silently ignore the overflow and clamp the clock to
323
- // [_PyTime_MIN; _PyTime_MAX].
324
- //
325
- // Use _PyTime_GetPerfCounterWithInfo() to check for failure.
326
- // Export for '_lsprof' shared extension.
327
- PyAPI_FUNC (_PyTime_t ) _PyTime_GetPerfCounter (void );
328
-
329
285
// Get the performance counter: clock with the highest available resolution to
330
286
// measure a short duration.
331
287
//
@@ -336,14 +292,24 @@ extern int _PyTime_GetPerfCounterWithInfo(
336
292
_PyTime_t * t ,
337
293
_Py_clock_info_t * info );
338
294
295
+ // Alias for backward compatibility
296
+ #define _PyTime_MIN PyTime_MIN
297
+ #define _PyTime_MAX PyTime_MAX
298
+ #define _PyTime_AsSecondsDouble PyTime_AsSecondsDouble
299
+ #define _PyTime_GetMonotonicClock PyTime_Monotonic
300
+ #define _PyTime_GetPerfCounter PyTime_PerfCounter
301
+ #define _PyTime_GetSystemClock PyTime_Time
302
+
303
+
304
+ // --- _PyDeadline -----------------------------------------------------------
339
305
340
306
// Create a deadline.
341
- // Pseudo code: _PyTime_GetMonotonicClock () + timeout.
307
+ // Pseudo code: PyTime_Monotonic () + timeout.
342
308
// Export for '_ssl' shared extension.
343
309
PyAPI_FUNC (_PyTime_t ) _PyDeadline_Init (_PyTime_t timeout );
344
310
345
311
// Get remaining time from a deadline.
346
- // Pseudo code: deadline - _PyTime_GetMonotonicClock ().
312
+ // Pseudo code: deadline - PyTime_Monotonic ().
347
313
// Export for '_ssl' shared extension.
348
314
PyAPI_FUNC (_PyTime_t ) _PyDeadline_Get (_PyTime_t deadline );
349
315
0 commit comments