From 9b21b888e52d9a7268c231bc0d520ec0be9bfcb8 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 6 Jul 2024 23:35:59 -0500 Subject: [PATCH] Update: Rewrite timing logic in controller. Check if both numbers added together are greater than or equal to half a second. If they are, then an overflow will happen. Subtract the half seconds instead of handling an overflow. This does not handle the case where the milliseconds or nanoseconds are greater than one second. This only handles the cases where they are greater than half a second. --- level_3/controller/c/controller/private-controller.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/level_3/controller/c/controller/private-controller.c b/level_3/controller/c/controller/private-controller.c index d9d8857..9ce6e92 100644 --- a/level_3/controller/c/controller/private-controller.c +++ b/level_3/controller/c/controller/private-controller.c @@ -708,13 +708,13 @@ extern "C" { gettimeofday(&now, 0); time->tv_sec = now.tv_sec + seconds; - time->tv_nsec = (now.tv_usec * 1000) + nanoseconds; + time->tv_nsec = now.tv_usec * 1000; - // If tv_nsec is 1 second or greater, then increment seconds. - if (time->tv_nsec >= 1000000000) { + // If result would be greater than 1 second, then increment seconds without overflowing. + if (time->tv_nsec > 500000000 && nanoseconds > 500000000) { ++(time->tv_sec); - time->tv_nsec -= 1000000000; + time->tv_nsec = (time->tv_nsec - 500000000) + (nanoseconds - 500000000); } } #endif // _di_controller_time_ -- 1.8.3.1