Repository navigation
gh-110850: Add PyTime_t C API #115215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-110850: Add PyTime_t C API #115215
Changes from 10 commits
4a8657c
3f60d67
a0c69bc
8397a05
793eb58
069eb79
744f53d
38028a3
74946d2
8f7992e
ac94566
f4fc849
3d43b73
ba1c30b
9fb5430
47f5d07
2b04579
7fad97d
960c779
988340f
fbd801b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| .. highlight:: c | ||
|
|
||
| PyTime C API | ||
| ============ | ||
|
|
||
| .. versionadded:: 3.13 | ||
|
|
||
| The clock C API provides access to system clocks and time conversion functions. | ||
| It is similar to the Python :mod:`time` module. | ||
|
|
||
| For C API related to the :mod:`datetime` module, see :ref:`datetimeobjects`. | ||
|
|
||
|
|
||
| Types | ||
| ----- | ||
|
|
||
| .. c:type:: PyTime_t | ||
|
|
||
| A timestamp or duration. | ||
|
|
||
| The reference point for timestamps depends on the clock used. For example, | ||
| :c:func:`PyTime_Time` returns timestamps relative to the UNIX epoch. | ||
|
|
||
| ``PyTime_t`` is a signed integral type. It is possible to add and | ||
| subtract such values directly. | ||
| Keep in mind that C overflow rules do apply. | ||
|
|
||
| ``PyTime_t`` can represent time with nanosecond or better resolution. | ||
| The unit used is an internal implementation detail; use | ||
| :c:func:`PyTime_AsNanoseconds` or :c:func:`PyTime_AsSecondsDouble` | ||
| to convert to a given unit. | ||
|
|
||
| The supported range is at least around [-292.3 years; +292.3 years]. | ||
| Using the Unix epoch (January 1st, 1970) as reference, the supported date | ||
| range is at least around [1677-09-21; 2262-04-11]. | ||
| The exact limits are exposed as constants. | ||
|
|
||
| .. c:var:: PyTime_t PyTime_MIN | ||
|
|
||
| Minimum value of :c:type:`PyTime_t`. | ||
|
|
||
| .. c:var:: PyTime_t PyTime_MAX | ||
|
|
||
| Maximum value of :c:type:`PyTime_t`. | ||
|
|
||
|
|
||
| Clock Functions | ||
| --------------- | ||
|
|
||
| The following functions take a pointer to a :c:expr:`PyTime_t` that they | ||
| set to the value of a particular clock. | ||
| Details of each clock are given in the documentation of the corresponding | ||
| Python function. | ||
|
|
||
| The functions return 0 on success, or -1 (with an exception set) on failure. | ||
|
encukou marked this conversation as resolved.
Outdated
|
||
|
|
||
| On integer overflow, they set the :c:data:`PyExc_OverflowError` exception and | ||
| set :c:expr:`*result` to the value clamped to the ``[PyTime_MIN; PyTime_MAX]`` | ||
| range. | ||
| (On current systems, integer overflows are likely caused by misconfigured | ||
| system time.) | ||
|
|
||
| .. c:function:: int PyTime_Monotonic(PyTime_t *result) | ||
|
|
||
| Read the monotonic clock. | ||
| See :func:`time.monotonic` for important details on this clock. | ||
|
|
||
| .. c:function:: int PyTime_PerfCounter(PyTime_t *result) | ||
|
|
||
| Read the performance counter. | ||
| See :func:`time.perf_counter` for important details on this clock. | ||
|
|
||
| .. c:function:: int PyTime_Time(PyTime_t *result) | ||
|
|
||
| Read the “wall clock” time. | ||
| See :func:`time.time` for details important on this clock. | ||
|
|
||
|
|
||
| Conversion 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, but note that :c:expr:`double` has limited | ||
| accuracy for large values. | ||
|
|
||
| .. c:function:: int PyTime_AsNanoseconds(PyTime_t t, int64_t *result) | ||
|
encukou marked this conversation as resolved.
Outdated
|
||
|
|
||
| Convert a timestamp to a number of nanoseconds as a 64-bit signed integer. | ||
|
|
||
| Returns 0 on success, or -1 (with an exception set) on failure. | ||
|
encukou marked this conversation as resolved.
Outdated
encukou marked this conversation as resolved.
Outdated
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||
| // 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 PY_INT64_T PyTime_t; | ||||||
|
encukou marked this conversation as resolved.
Outdated
|
||||||
| #define PyTime_MIN INT64_MIN | ||||||
| #define PyTime_MAX INT64_MAX | ||||||
|
|
||||||
| PyAPI_FUNC(double) PyTime_AsSecondsDouble(PyTime_t t); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: i suggest to first declare functions "creating" PyTime_t values, and then in second just (just add an empty line) functions to "convert" PyTime_t values. |
||||||
| PyAPI_FUNC(int) PyTime_AsNanoseconds(PyTime_t t, PY_INT64_T *result); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| PyAPI_FUNC(int) PyTime_Monotonic(PyTime_t *result); | ||||||
| PyAPI_FUNC(int) PyTime_PerfCounter(PyTime_t *result); | ||||||
| PyAPI_FUNC(int) PyTime_Time(PyTime_t *result); | ||||||
|
|
||||||
| static inline int | ||||||
| _PyTime_AsNanoseconds_impl(PyTime_t t, PY_INT64_T *result) | ||||||
| { | ||||||
| // Implementation detail: PyTime_t is already a int64_t in nanoseconds. | ||||||
| // This may change. We might e.g. extend PyTime_t to 128 bits, then | ||||||
| // this function can start raising OverflowError. | ||||||
| *result = t; | ||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| #define PyTime_AsNanoseconds _PyTime_AsNanoseconds_impl | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that it's worth it to add such static inline function which is an optimization. |
||||||
|
|
||||||
| #ifdef __cplusplus | ||||||
| } | ||||||
| #endif | ||||||
| #endif /* Py_PYTIME_H */ | ||||||
| #endif /* Py_LIMITED_API */ | ||||||
Uh oh!
There was an error while loading. Please reload this page.