From adef394ac5390e80227c949cbea4a7c22a114677 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 7 Jun 2017 13:16:56 +0200 Subject: timers: store internal time stamps as time_t instead of doubles This gives us accurate precision and it allows us to avoid storing "no time" for systems with too low timer resolution as we then bump the time up to 1 microsecond. Should fix test 573 on windows. Remove the now unused curlx_tvdiff_secs() function. Maintains the external getinfo() API with using doubles. Fixes #1531 --- lib/timeval.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'lib/timeval.c') diff --git a/lib/timeval.c b/lib/timeval.c index bed44c573..1012b4e39 100644 --- a/lib/timeval.c +++ b/lib/timeval.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2016, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2017, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -130,14 +130,24 @@ time_t curlx_tvdiff(struct timeval newer, struct timeval older) } /* - * Same as curlx_tvdiff but with full usec resolution. + * Make sure that the first argument is the more recent time, as otherwise + * we'll get a weird negative time-diff back... * - * Returns: the time difference in seconds with subsecond resolution. + * Returns: the time difference in number of microseconds. For too large diffs + * it returns max value. */ -double curlx_tvdiff_secs(struct timeval newer, struct timeval older) +time_t Curl_tvdiff_us(struct timeval newer, struct timeval older) { - if(newer.tv_sec != older.tv_sec) - return (double)(newer.tv_sec-older.tv_sec)+ - (double)(newer.tv_usec-older.tv_usec)/1000000.0; - return (double)(newer.tv_usec-older.tv_usec)/1000000.0; + time_t diff = newer.tv_sec-older.tv_sec; +#if SIZEOF_TIME_T < 8 + /* for 32bit time_t systems */ + if(diff >= (0x7fffffff/1000000)) + return 0x7fffffff; +#else + /* for 64bit time_t systems */ + if(diff >= (0x7fffffffffffffff/1000000)) + return 0x7fffffffffffffff; +#endif + return (newer.tv_sec-older.tv_sec)*1000000+ + (time_t)(newer.tv_usec-older.tv_usec); } -- cgit v1.2.3