bpo-41710: Add _PyTime_AsTimespec_clamp() (GH-28629) · python/cpython@09796f2

@@ -16,6 +16,7 @@ extern "C" {

1616

typedef int64_t _PyTime_t;

1717

#define _PyTime_MIN INT64_MIN

1818

#define _PyTime_MAX INT64_MAX

19+

#define _SIZEOF_PYTIME_T 8

19202021

typedef enum {

2122

/* Round towards minus infinity (-inf).

@@ -136,8 +137,9 @@ PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,

136137

struct timeval *tv,

137138

_PyTime_round_t round);

138139139-

/* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */

140-

PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,

140+

/* Similar to _PyTime_AsTimeval() but don't raise an exception on overflow.

141+

On overflow, clamp tv_sec to _PyTime_t min/max. */

142+

PyAPI_FUNC(void) _PyTime_AsTimeval_clamp(_PyTime_t t,

141143

struct timeval *tv,

142144

_PyTime_round_t round);

143145

@@ -162,6 +164,10 @@ PyAPI_FUNC(int) _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts);

162164

tv_nsec is always positive.

163165

Raise an exception and return -1 on error, return 0 on success. */

164166

PyAPI_FUNC(int) _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts);

167+168+

/* Similar to _PyTime_AsTimespec() but don't raise an exception on overflow.

169+

On overflow, clamp tv_sec to _PyTime_t min/max. */

170+

PyAPI_FUNC(void) _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts);

165171

#endif

166172167173

/* Compute ticks * mul / div.

@@ -181,7 +187,7 @@ typedef struct {

181187

/* Get the current time from the system clock.

182188183189

If the internal clock fails, silently ignore the error and return 0.

184-

On integer overflow, silently ignore the overflow and truncated the clock to

190+

On integer overflow, silently ignore the overflow and clamp the clock to

185191

_PyTime_MIN or _PyTime_MAX.

186192187193

Use _PyTime_GetSystemClockWithInfo() to check for failure. */

@@ -201,7 +207,7 @@ PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo(

201207

results of consecutive calls is valid.

202208203209

If the internal clock fails, silently ignore the error and return 0.

204-

On integer overflow, silently ignore the overflow and truncated the clock to

210+

On integer overflow, silently ignore the overflow and clamp the clock to

205211

_PyTime_MIN or _PyTime_MAX.

206212207213

Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */

@@ -232,7 +238,7 @@ PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm);

232238

measure a short duration.

233239234240

If the internal clock fails, silently ignore the error and return 0.

235-

On integer overflow, silently ignore the overflow and truncated the clock to

241+

On integer overflow, silently ignore the overflow and clamp the clock to

236242

_PyTime_MIN or _PyTime_MAX.

237243238244

Use _PyTime_GetPerfCounterWithInfo() to check for failure. */