From b14d0ddcb9c399c57429c6b11e0aa803cf5e8335 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Thu, 21 May 2015 01:21:02 +1000 Subject: [PATCH] Add timer_cmp() utility function to compare two timevals. This is preferable to using time_diff() to compare two timevals, because time_diff() gives its result as a 'double' which only has 53 bits of precision. Also switch a couple of places from using time_diff() to using this function. --- include/ircaux.h | 1 + source/ircaux.c | 9 +++++++++ source/timer.c | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/ircaux.h b/include/ircaux.h index 4027504..76fa5e7 100644 --- a/include/ircaux.h +++ b/include/ircaux.h @@ -82,6 +82,7 @@ struct timeval BX_get_time (struct timeval *); double BX_time_diff (struct timeval, struct timeval); double time_since(const struct timeval *tv_from); double time_until(const struct timeval *tv_to); +int time_cmp(const struct timeval *a, const struct timeval *b); char * BX_plural (int); int BX_time_to_next_minute (void); char * BX_remove_trailing_spaces (char *); diff --git a/source/ircaux.c b/source/ircaux.c index cab9395..9913bb3 100644 --- a/source/ircaux.c +++ b/source/ircaux.c @@ -1764,6 +1764,15 @@ double time_until(const struct timeval *tv_to) return BX_time_diff(tv_now, *tv_to); } +/* Compare two timevals - returns 1, 0 or -1 if a is >, = or < b */ +int time_cmp(const struct timeval *a, const struct timeval *b) +{ + if (a->tv_sec == b->tv_sec) + return (a->tv_usec > b->tv_usec) - (a->tv_usec < b->tv_usec); + else + return (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec); +} + int BX_time_to_next_minute (void) { time_t now = time(NULL); diff --git a/source/timer.c b/source/timer.c index 67709ce..419f9ca 100644 --- a/source/timer.c +++ b/source/timer.c @@ -176,7 +176,7 @@ extern void ExecuteTimers (void) get_time(&now1); parsingtimer = 1; - while (PendingTimers && BX_time_diff(now1, PendingTimers->time) < 0) + while (PendingTimers && time_cmp(&now1, &PendingTimers->time) >= 0) { int old_refnum = current_window->refnum; @@ -587,7 +587,7 @@ static char *schedule_timer (TimerList *ntimer) /* we've created it, now put it in order */ for (slot = &PendingTimers; *slot; slot = &(*slot)->next) { - if (BX_time_diff((*slot)->time, ntimer->time) < 0) + if (time_cmp(&(*slot)->time, &ntimer->time) > 0) break; } ntimer->next = *slot;