From 9a8089b32adee874caefbe2a96096998625c5a78 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 20 Mar 2015 01:42:20 +0100 Subject: Issue #23646: Enhance precision of time.sleep() and socket timeout when interrupted by a signal Add a new _PyTime_AddDouble() function and remove _PyTime_ADD_SECONDS() macro. The _PyTime_ADD_SECONDS only supported an integer number of seconds, the _PyTime_AddDouble() has subsecond resolution. --- Python/pytime.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'Python/pytime.c') diff --git a/Python/pytime.c b/Python/pytime.c index cdaa22ec66..894008db8b 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -367,6 +367,23 @@ _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec, return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round); } +void +_PyTime_AddDouble(_PyTime_timeval *tv, double interval, _PyTime_round_t round) +{ + _PyTime_timeval tv2; + double frac; + + frac = fmod(interval, 1.0); + interval = floor(interval); + tv2.tv_sec = (long)interval; + tv2.tv_usec = (long)(frac*1e6); + + tv->tv_sec += tv2.tv_sec; + tv->tv_usec += tv2.tv_usec; + tv->tv_sec += (time_t)(tv->tv_usec / 1000000); + tv->tv_usec %= 1000000; +} + int _PyTime_Init(void) { -- cgit v1.2.1