diff options
Diffstat (limited to 'Modules/_datetimemodule.c')
-rw-r--r-- | Modules/_datetimemodule.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 09285d9..c3e54f7 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -7,6 +7,10 @@ #include <time.h> +#ifdef MS_WINDOWS +# include <winsock2.h> /* struct timeval */ +#endif + /* Differentiate between building the core module and building extension * modules. */ @@ -4093,6 +4097,8 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, if (_PyTime_ObjectToTimeval(timestamp, &timet, &us, _PyTime_ROUND_DOWN) == -1) return NULL; + assert(0 <= us && us <= 999999); + return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); } @@ -4103,10 +4109,14 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, static PyObject * datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) { - _PyTime_timeval t; - _PyTime_gettimeofday(&t); - return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, - tzinfo); + _PyTime_t ts = _PyTime_GetSystemClock(); + struct timeval tv; + + if (_PyTime_AsTimeval(ts, &tv, _PyTime_ROUND_FLOOR) < 0) + return NULL; + assert(0 <= tv.tv_usec && tv.tv_usec <= 999999); + + return datetime_from_timet_and_us(cls, f, tv.tv_sec, tv.tv_usec, tzinfo); } /*[clinic input] |