forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PyTime_t API: * PyTime_t type. * PyTime_MIN and PyTime_MAX constants. * PyTime_AsSecondsDouble(), PyTime_Monotonic(), PyTime_PerfCounter() and PyTime_GetSystemClock() functions. Changes: * Add Include/cpython/pytime.h header. * Add Modules/_testcapi/time.c and Lib/test/test_capi/test_time.py tests on these new functions. * Rename _PyTime_GetMonotonicClock() to PyTime_Monotonic(). * Rename _PyTime_GetPerfCounter() to PyTime_PerfCounter(). * Rename _PyTime_GetSystemClock() to PyTime_Time(). * Rename _PyTime_AsSecondsDouble() to PyTime_AsSecondsDouble(). * Rename _PyTime_MIN to PyTime_MIN. * Rename _PyTime_MAX to PyTime_MAX.
- Loading branch information
Showing
19 changed files
with
417 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
.. highlight:: c | ||
|
||
PyTime C API | ||
============ | ||
|
||
.. versionadded:: 3.13 | ||
|
||
PyTime API | ||
---------- | ||
|
||
The PyTime_t API is written to use timestamp and timeout values stored in | ||
various formats and to read clocks with a resolution of one nanosecond. | ||
|
||
The :c:type:`PyTime_t` type is signed to support negative timestamps. The | ||
supported range is around [-292.3 years; +292.3 years]. Using the Unix epoch | ||
(January 1st, 1970) as reference, the supported date range is around | ||
[1677-09-21; 2262-04-11]. | ||
|
||
|
||
Types | ||
----- | ||
|
||
.. c:type:: PyTime_t | ||
Timestamp type with subsecond precision: 64-bit signed integer. | ||
|
||
This type can be used to store a duration. Indirectly, it can be used to | ||
store a date relative to a reference date, such as the UNIX epoch. | ||
|
||
|
||
Constants | ||
--------- | ||
|
||
.. c:var:: PyTime_t PyTime_MIN | ||
Minimum value of the :c:type:`PyTime_t` type. | ||
|
||
:c:var:`PyTime_MIN` nanoseconds is around -292.3 years. | ||
|
||
.. c:var:: PyTime_t PyTime_MAX | ||
Maximum value of the :c:type:`PyTime_t` type. | ||
|
||
:c:var:`PyTime_MAX` nanoseconds is around +292.3 years. | ||
|
||
|
||
Functions | ||
--------- | ||
|
||
.. c:function:: double PyTime_AsSecondsDouble(PyTime_t t) | ||
Convert a timestamp to a number of seconds as a C :c:expr:`double`. | ||
The function cannot fail. | ||
.. c:function:: PyTime_t PyTime_Monotonic(void) | ||
Get the monotonic clock: clock that cannot go backwards. | ||
The monotonic clock is not affected by system clock updates. The reference | ||
point of the returned value is undefined, so that only the difference | ||
between the results of consecutive calls is valid. | ||
If reading the clock fails, silently ignore the error and return 0. | ||
On integer overflow, silently ignore the overflow and clamp the clock to | ||
the ``[PyTime_MIN; PyTime_MAX]`` range. | ||
See also the :func:`time.monotonic` function. | ||
|
||
|
||
.. c:function:: PyTime_t PyTime_PerfCounter(void) | ||
Get the performance counter: clock with the highest available resolution to | ||
measure a short duration. | ||
The performance counter does include time elapsed during sleep and is | ||
system-wide. The reference point of the returned value is undefined, so that | ||
only the difference between the results of two calls is valid. | ||
If reading the clock fails, silently ignore the error and return 0. | ||
On integer overflow, silently ignore the overflow and clamp the clock to | ||
the ``[PyTime_MIN; PyTime_MAX]`` range. | ||
See also the :func:`time.perf_counter` function. | ||
|
||
|
||
.. c:function:: PyTime_t PyTime_Time(void) | ||
Get the system clock. | ||
The system clock can be changed automatically (e.g. by a NTP daemon) or | ||
manually by the system administrator. So it can also go backward. Use | ||
:c:func:`PyTime_Monotonic` to use a monotonic clock. | ||
If reading the clock fails, silently ignore the error and return ``0``. | ||
On integer overflow, silently ignore the overflow and clamp the clock to | ||
the ``[PyTime_MIN; PyTime_MAX]`` range. | ||
See also the :func:`time.time` function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// PyTime_t C API: see Doc/c-api/time.rst for the documentation. | ||
|
||
#ifndef Py_LIMITED_API | ||
#ifndef Py_PYTIME_H | ||
#define Py_PYTIME_H | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef int64_t PyTime_t; | ||
#define PyTime_MIN INT64_MIN | ||
#define PyTime_MAX INT64_MAX | ||
|
||
PyAPI_FUNC(double) PyTime_AsSecondsDouble(PyTime_t t); | ||
PyAPI_FUNC(PyTime_t) PyTime_Monotonic(void); | ||
PyAPI_FUNC(PyTime_t) PyTime_PerfCounter(void); | ||
PyAPI_FUNC(PyTime_t) PyTime_Time(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif /* Py_PYTIME_H */ | ||
#endif /* Py_LIMITED_API */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.