From fe7849d645f8c2f05ea8a1264d79b4e1240f5be7 Mon Sep 17 00:00:00 2001 From: Graham Sanderson Date: Mon, 8 Aug 2022 07:42:52 -0500 Subject: [PATCH] fix delayed_by_us and delayed_by_ms to not return times > at_the_end_of_time (#936) --- src/common/pico_time/include/pico/time.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/common/pico_time/include/pico/time.h b/src/common/pico_time/include/pico/time.h index 0fde00f..d1cc02d 100644 --- a/src/common/pico_time/include/pico/time.h +++ b/src/common/pico_time/include/pico/time.h @@ -95,8 +95,9 @@ static inline absolute_time_t delayed_by_us(const absolute_time_t t, uint64_t us absolute_time_t t2; uint64_t base = to_us_since_boot(t); uint64_t delayed = base + us; - if (delayed < base) { - delayed = (uint64_t)-1; + if ((int64_t)delayed < 0) { + // absolute_time_t (to allow for signed time deltas) is never greater than INT64_MAX which == at_the_end_of_time + delayed = INT64_MAX; } update_us_since_boot(&t2, delayed); return t2; @@ -113,8 +114,9 @@ static inline absolute_time_t delayed_by_ms(const absolute_time_t t, uint32_t ms absolute_time_t t2; uint64_t base = to_us_since_boot(t); uint64_t delayed = base + ms * 1000ull; - if (delayed < base) { - delayed = (uint64_t)-1; + if ((int64_t)delayed < 0) { + // absolute_time_t (to allow for signed time deltas) is never greater than INT64_MAX which == at_the_end_of_time + delayed = INT64_MAX; } update_us_since_boot(&t2, delayed); return t2;