Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:50

0001 // Internal PyTime_t C API: see Doc/c-api/time.rst for the documentation.
0002 //
0003 // The PyTime_t type is an integer to support directly common arithmetic
0004 // operations such as t1 + t2.
0005 //
0006 // Time formats:
0007 //
0008 // * Seconds.
0009 // * Seconds as a floating-point number (C double).
0010 // * Milliseconds (10^-3 seconds).
0011 // * Microseconds (10^-6 seconds).
0012 // * 100 nanoseconds (10^-7 seconds), used on Windows.
0013 // * Nanoseconds (10^-9 seconds).
0014 // * timeval structure, 1 microsecond (10^-6 seconds).
0015 // * timespec structure, 1 nanosecond (10^-9 seconds).
0016 //
0017 // Note that PyTime_t is now specified as int64_t, in nanoseconds.
0018 // (If we need to change this, we'll need new public API with new names.)
0019 // Previously, PyTime_t was configurable (in theory); some comments and code
0020 // might still allude to that.
0021 //
0022 // Integer overflows are detected and raise OverflowError. Conversion to a
0023 // resolution larger than 1 nanosecond is rounded correctly with the requested
0024 // rounding mode. Available rounding modes:
0025 //
0026 // * Round towards minus infinity (-inf). For example, used to read a clock.
0027 // * Round towards infinity (+inf). For example, used for timeout to wait "at
0028 //   least" N seconds.
0029 // * Round to nearest with ties going to nearest even integer. For example, used
0030 //   to round from a Python float.
0031 // * Round away from zero. For example, used for timeout.
0032 //
0033 // Some functions clamp the result in the range [PyTime_MIN; PyTime_MAX]. The
0034 // caller doesn't have to handle errors and so doesn't need to hold the GIL to
0035 // handle exceptions. For example, _PyTime_Add(t1, t2) computes t1+t2 and
0036 // clamps the result on overflow.
0037 //
0038 // Clocks:
0039 //
0040 // * System clock
0041 // * Monotonic clock
0042 // * Performance counter
0043 //
0044 // Internally, operations like (t * k / q) with integers are implemented in a
0045 // way to reduce the risk of integer overflow. Such operation is used to convert a
0046 // clock value expressed in ticks with a frequency to PyTime_t, like
0047 // QueryPerformanceCounter() with QueryPerformanceFrequency() on Windows.
0048 
0049 
0050 #ifndef Py_INTERNAL_TIME_H
0051 #define Py_INTERNAL_TIME_H
0052 #ifdef __cplusplus
0053 extern "C" {
0054 #endif
0055 
0056 #ifndef Py_BUILD_CORE
0057 #  error "this header requires Py_BUILD_CORE define"
0058 #endif
0059 
0060 
0061 #ifdef __clang__
0062 struct timeval;
0063 #endif
0064 
0065 #define _SIZEOF_PYTIME_T 8
0066 
0067 typedef enum {
0068     // Round towards minus infinity (-inf).
0069     // For example, used to read a clock.
0070     _PyTime_ROUND_FLOOR=0,
0071 
0072     // Round towards infinity (+inf).
0073     // For example, used for timeout to wait "at least" N seconds.
0074     _PyTime_ROUND_CEILING=1,
0075 
0076     // Round to nearest with ties going to nearest even integer.
0077     // For example, used to round from a Python float.
0078     _PyTime_ROUND_HALF_EVEN=2,
0079 
0080     // Round away from zero
0081     // For example, used for timeout. _PyTime_ROUND_CEILING rounds
0082     // -1e-9 to 0 milliseconds which causes bpo-31786 issue.
0083     // _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps
0084     // the timeout sign as expected. select.poll(timeout) must block
0085     // for negative values.
0086     _PyTime_ROUND_UP=3,
0087 
0088     // _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
0089     // used for timeouts.
0090     _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
0091 } _PyTime_round_t;
0092 
0093 
0094 // Convert a time_t to a PyLong.
0095 // Export for '_testinternalcapi' shared extension
0096 PyAPI_FUNC(PyObject*) _PyLong_FromTime_t(time_t sec);
0097 
0098 // Convert a PyLong to a time_t.
0099 // Export for '_datetime' shared extension
0100 PyAPI_FUNC(time_t) _PyLong_AsTime_t(PyObject *obj);
0101 
0102 // Convert a number of seconds, int or float, to time_t.
0103 // Export for '_datetime' shared extension.
0104 PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
0105     PyObject *obj,
0106     time_t *sec,
0107     _PyTime_round_t);
0108 
0109 // Convert a number of seconds, int or float, to a timeval structure.
0110 // usec is in the range [0; 999999] and rounded towards zero.
0111 // For example, -1.2 is converted to (-2, 800000).
0112 // Export for '_datetime' shared extension.
0113 PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
0114     PyObject *obj,
0115     time_t *sec,
0116     long *usec,
0117     _PyTime_round_t);
0118 
0119 // Convert a number of seconds, int or float, to a timespec structure.
0120 // nsec is in the range [0; 999999999] and rounded towards zero.
0121 // For example, -1.2 is converted to (-2, 800000000).
0122 // Export for '_testinternalcapi' shared extension.
0123 PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
0124     PyObject *obj,
0125     time_t *sec,
0126     long *nsec,
0127     _PyTime_round_t);
0128 
0129 
0130 // Create a timestamp from a number of seconds.
0131 // Export for '_socket' shared extension.
0132 PyAPI_FUNC(PyTime_t) _PyTime_FromSeconds(int seconds);
0133 
0134 // Create a timestamp from a number of seconds in double.
0135 extern int _PyTime_FromSecondsDouble(
0136     double seconds,
0137     _PyTime_round_t round,
0138     PyTime_t *result);
0139 
0140 // Macro to create a timestamp from a number of seconds, no integer overflow.
0141 // Only use the macro for small values, prefer _PyTime_FromSeconds().
0142 #define _PYTIME_FROMSECONDS(seconds) \
0143             ((PyTime_t)(seconds) * (1000 * 1000 * 1000))
0144 
0145 // Create a timestamp from a number of microseconds.
0146 // Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
0147 extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);
0148 
0149 // Create a timestamp from a Python int object (number of nanoseconds).
0150 // Export for '_lsprof' shared extension.
0151 PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
0152     PyObject *obj);
0153 
0154 // Convert a number of seconds (Python float or int) to a timestamp.
0155 // Raise an exception and return -1 on error, return 0 on success.
0156 // Export for '_socket' shared extension.
0157 PyAPI_FUNC(int) _PyTime_FromSecondsObject(PyTime_t *t,
0158     PyObject *obj,
0159     _PyTime_round_t round);
0160 
0161 // Convert a number of milliseconds (Python float or int, 10^-3) to a timestamp.
0162 // Raise an exception and return -1 on error, return 0 on success.
0163 // Export for 'select' shared extension.
0164 PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(PyTime_t *t,
0165     PyObject *obj,
0166     _PyTime_round_t round);
0167 
0168 // Convert timestamp to a number of milliseconds (10^-3 seconds).
0169 // Export for '_ssl' shared extension.
0170 PyAPI_FUNC(PyTime_t) _PyTime_AsMilliseconds(PyTime_t t,
0171     _PyTime_round_t round);
0172 
0173 // Convert timestamp to a number of microseconds (10^-6 seconds).
0174 // Export for '_queue' shared extension.
0175 PyAPI_FUNC(PyTime_t) _PyTime_AsMicroseconds(PyTime_t t,
0176     _PyTime_round_t round);
0177 
0178 #ifdef MS_WINDOWS
0179 // Convert timestamp to a number of 100 nanoseconds (10^-7 seconds).
0180 extern PyTime_t _PyTime_As100Nanoseconds(PyTime_t t,
0181     _PyTime_round_t round);
0182 #endif
0183 
0184 // Convert a timestamp (number of nanoseconds) as a Python int object.
0185 // Export for '_testinternalcapi' shared extension.
0186 PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);
0187 
0188 #ifndef MS_WINDOWS
0189 // Create a timestamp from a timeval structure.
0190 // Raise an exception and return -1 on overflow, return 0 on success.
0191 extern int _PyTime_FromTimeval(PyTime_t *tp, struct timeval *tv);
0192 #endif
0193 
0194 // Convert a timestamp to a timeval structure (microsecond resolution).
0195 // tv_usec is always positive.
0196 // Raise an exception and return -1 if the conversion overflowed,
0197 // return 0 on success.
0198 // Export for 'select' shared extension.
0199 PyAPI_FUNC(int) _PyTime_AsTimeval(PyTime_t t,
0200     struct timeval *tv,
0201     _PyTime_round_t round);
0202 
0203 // Similar to _PyTime_AsTimeval() but don't raise an exception on overflow.
0204 // On overflow, clamp tv_sec to PyTime_t min/max.
0205 // Export for 'select' shared extension.
0206 PyAPI_FUNC(void) _PyTime_AsTimeval_clamp(PyTime_t t,
0207     struct timeval *tv,
0208     _PyTime_round_t round);
0209 
0210 // Convert a timestamp to a number of seconds (secs) and microseconds (us).
0211 // us is always positive. This function is similar to _PyTime_AsTimeval()
0212 // except that secs is always a time_t type, whereas the timeval structure
0213 // uses a C long for tv_sec on Windows.
0214 // Raise an exception and return -1 if the conversion overflowed,
0215 // return 0 on success.
0216 // Export for '_datetime' shared extension.
0217 PyAPI_FUNC(int) _PyTime_AsTimevalTime_t(
0218     PyTime_t t,
0219     time_t *secs,
0220     int *us,
0221     _PyTime_round_t round);
0222 
0223 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
0224 // Create a timestamp from a timespec structure.
0225 // Raise an exception and return -1 on overflow, return 0 on success.
0226 extern int _PyTime_FromTimespec(PyTime_t *tp, const struct timespec *ts);
0227 
0228 // Convert a timestamp to a timespec structure (nanosecond resolution).
0229 // tv_nsec is always positive.
0230 // Raise an exception and return -1 on error, return 0 on success.
0231 // Export for '_testinternalcapi' shared extension.
0232 PyAPI_FUNC(int) _PyTime_AsTimespec(PyTime_t t, struct timespec *ts);
0233 
0234 // Similar to _PyTime_AsTimespec() but don't raise an exception on overflow.
0235 // On overflow, clamp tv_sec to PyTime_t min/max.
0236 // Export for '_testinternalcapi' shared extension.
0237 PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(PyTime_t t, struct timespec *ts);
0238 #endif
0239 
0240 
0241 // Compute t1 + t2. Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
0242 extern PyTime_t _PyTime_Add(PyTime_t t1, PyTime_t t2);
0243 
0244 // Structure used by time.get_clock_info()
0245 typedef struct {
0246     const char *implementation;
0247     int monotonic;
0248     int adjustable;
0249     double resolution;
0250 } _Py_clock_info_t;
0251 
0252 // Get the current time from the system clock.
0253 // On success, set *t and *info (if not NULL), and return 0.
0254 // On error, raise an exception and return -1.
0255 extern int _PyTime_TimeWithInfo(
0256     PyTime_t *t,
0257     _Py_clock_info_t *info);
0258 
0259 // Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
0260 // The clock is not affected by system clock updates. The reference point of
0261 // the returned value is undefined, so that only the difference between the
0262 // results of consecutive calls is valid.
0263 //
0264 // Fill info (if set) with information of the function used to get the time.
0265 //
0266 // Return 0 on success, raise an exception and return -1 on error.
0267 // Export for '_testsinglephase' shared extension.
0268 PyAPI_FUNC(int) _PyTime_MonotonicWithInfo(
0269     PyTime_t *t,
0270     _Py_clock_info_t *info);
0271 
0272 
0273 // Converts a timestamp to the Gregorian time, using the local time zone.
0274 // Return 0 on success, raise an exception and return -1 on error.
0275 // Export for '_datetime' shared extension.
0276 PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm);
0277 
0278 // Converts a timestamp to the Gregorian time, assuming UTC.
0279 // Return 0 on success, raise an exception and return -1 on error.
0280 // Export for '_datetime' shared extension.
0281 PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);
0282 
0283 
0284 // Get the performance counter: clock with the highest available resolution to
0285 // measure a short duration.
0286 //
0287 // Fill info (if set) with information of the function used to get the time.
0288 //
0289 // Return 0 on success, raise an exception and return -1 on error.
0290 extern int _PyTime_PerfCounterWithInfo(
0291     PyTime_t *t,
0292     _Py_clock_info_t *info);
0293 
0294 
0295 // --- _PyDeadline -----------------------------------------------------------
0296 
0297 // Create a deadline.
0298 // Pseudo code: return PyTime_MonotonicRaw() + timeout
0299 // Export for '_ssl' shared extension.
0300 PyAPI_FUNC(PyTime_t) _PyDeadline_Init(PyTime_t timeout);
0301 
0302 // Get remaining time from a deadline.
0303 // Pseudo code: return deadline - PyTime_MonotonicRaw()
0304 // Export for '_ssl' shared extension.
0305 PyAPI_FUNC(PyTime_t) _PyDeadline_Get(PyTime_t deadline);
0306 
0307 
0308 // --- _PyTimeFraction -------------------------------------------------------
0309 
0310 typedef struct {
0311     PyTime_t numer;
0312     PyTime_t denom;
0313 } _PyTimeFraction;
0314 
0315 // Set a fraction.
0316 // Return 0 on success.
0317 // Return -1 if the fraction is invalid.
0318 extern int _PyTimeFraction_Set(
0319     _PyTimeFraction *frac,
0320     PyTime_t numer,
0321     PyTime_t denom);
0322 
0323 // Compute ticks * frac.numer / frac.denom.
0324 // Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
0325 extern PyTime_t _PyTimeFraction_Mul(
0326     PyTime_t ticks,
0327     const _PyTimeFraction *frac);
0328 
0329 // Compute a clock resolution: frac.numer / frac.denom / 1e9.
0330 extern double _PyTimeFraction_Resolution(
0331     const _PyTimeFraction *frac);
0332 
0333 
0334 #ifdef __cplusplus
0335 }
0336 #endif
0337 #endif   // !Py_INTERNAL_TIME_H