Issue #25155: Add _PyTime_AsTimevalTime_t() function

On Windows, the tv_sec field of the timeval structure has the type C long,
whereas it has the type C time_t on all other platforms. A C long has a size of
32 bits (signed inter, 1 bit for the sign, 31 bits for the value) which is not
enough to store an Epoch timestamp after the year 2038.

Add the _PyTime_AsTimevalTime_t() function written for datetime.datetime.now():
convert a _PyTime_t timestamp to a (secs, us) tuple where secs type is time_t.
It allows to support dates after the year 2038 on Windows.

Enhance also _PyTime_AsTimeval_impl() to detect overflow on the number of
seconds when rounding the number of microseconds.
This commit is contained in:
Victor Stinner 2015-09-18 13:23:02 +02:00
parent 1bd0b54c74
commit 1e2b6882fc
3 changed files with 80 additions and 26 deletions

View file

@ -120,6 +120,18 @@ PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,
struct timeval *tv,
_PyTime_round_t round);
/* Convert a timestamp to a number of seconds (secs) and microseconds (us).
us is always positive. This function is similar to _PyTime_AsTimeval()
except that secs is always a time_t type, whereas the timeval structure
uses a C long for tv_sec on Windows.
Raise an exception and return -1 if the conversion overflowed,
return 0 on success. */
PyAPI_FUNC(int) _PyTime_AsTimevalTime_t(
_PyTime_t t,
time_t *secs,
int *us,
_PyTime_round_t round);
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
/* Convert a timestamp to a timespec structure (nanosecond resolution).
tv_nsec is always positive.