diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-09-18 11:23:02 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-09-18 11:23:02 (GMT) |
commit | 1e2b6882fc28a3cded227f55e4dc3f937afd678e (patch) | |
tree | 761eed5c9da66d8748b6382cd49589850cdf1e49 /Modules/_datetimemodule.c | |
parent | 1bd0b54c74ea7a108e4a7d921bf9ff904c6fa4d7 (diff) | |
download | cpython-1e2b6882fc28a3cded227f55e4dc3f937afd678e.zip cpython-1e2b6882fc28a3cded227f55e4dc3f937afd678e.tar.gz cpython-1e2b6882fc28a3cded227f55e4dc3f937afd678e.tar.bz2 |
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.
Diffstat (limited to 'Modules/_datetimemodule.c')
-rw-r--r-- | Modules/_datetimemodule.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index bbc51c6..5289222 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -4117,13 +4117,14 @@ static PyObject * datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) { _PyTime_t ts = _PyTime_GetSystemClock(); - struct timeval tv; + time_t secs; + int us; - if (_PyTime_AsTimeval(ts, &tv, _PyTime_ROUND_FLOOR) < 0) + if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) return NULL; - assert(0 <= tv.tv_usec && tv.tv_usec <= 999999); + assert(0 <= us && us <= 999999); - return datetime_from_timet_and_us(cls, f, tv.tv_sec, tv.tv_usec, tzinfo); + return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); } /*[clinic input] |