-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Merged
Merged
gh-110850: Add PyTime_t C API #115215
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4a8657c
gh-110850: Add PyTime_t C API
vstinner 3f60d67
Address Sam's review: update doc
vstinner a0c69bc
Avoid reference to epoch_
vstinner 8397a05
Make the docs more concise & rename to "Clock C API"
encukou 793eb58
Doc: Make nanoseconds a detail; add PyTime_AsNanoseconds
encukou 069eb79
Implement the fallible API and PyTime_AsNanoseconds
encukou 744f53d
Use PY_INT64_T in the public header
encukou 38028a3
Adjust What's New & News
encukou 74946d2
Test PyTime_AsNanoseconds
encukou 8f7992e
Add (indirect) tests for PyTime_AsNanoseconds
encukou ac94566
Remove PyTime_AsNanoseconds
encukou f4fc849
Update docs to mention PyTime_t size and resolution, remove PyTime_As…
encukou 3d43b73
Dedent docs for PyTime_MIN & MAX constants
encukou ba1c30b
Use int64_t rather than PY_INT64_T
encukou 9fb5430
Add back full implementations of infallible functions
encukou 47f5d07
Add notes about the GIL
encukou 2b04579
Merge in the main branch
encukou 7fad97d
Remove PyTime_AsNanoseconds also from What's New & NEWS
encukou 960c779
Note that PyTime_t is fixed now.
encukou 988340f
Reorder the functions to reduce the diff
encukou fbd801b
Fix ReST warning
encukou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,83 @@ | ||
.. highlight:: c | ||
|
||
PyTime C API | ||
============ | ||
|
||
.. versionadded:: 3.13 | ||
|
||
The clock C API provides access to system clocks. | ||
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 in nanoseconds, represented as a signed 64-bit | ||
integer. | ||
|
||
The reference point for timestamps depends on the clock used. For example, | ||
:c:func:`PyTime_Time` returns timestamps relative to the UNIX epoch. | ||
|
||
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]. | ||
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. | ||
|
||
On integer overflow, they set the :c:data:`PyExc_OverflowError` exception and | ||
set ``*result`` to the value clamped to the ``[PyTime_MIN; PyTime_MAX]`` | ||
range. | ||
(On current systems, integer overflows are likely caused by misconfigured | ||
system time.) | ||
|
||
As any other C API (unless otherwise specified), the functions must be called | ||
with the :term:`GIL` held. | ||
|
||
.. 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. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(int) PyTime_Monotonic(PyTime_t *result); | ||
PyAPI_FUNC(int) PyTime_PerfCounter(PyTime_t *result); | ||
PyAPI_FUNC(int) PyTime_Time(PyTime_t *result); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif /* Py_PYTIME_H */ | ||
#endif /* Py_LIMITED_API */ |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.