From 47f90356013f2170e2fb991ac370766f71965271 Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Sun, 1 Feb 2026 15:18:06 +0100 Subject: [PATCH] time: ensure type correctness and calculate nsec correctly (#13167) use auto for nsecdur, assigning system_tp into steady_tp compiles but is not correct. just change it to auto. use {} initialization for timespec structs and returning std::pair. in timediff, fromTimespec and toTimespec the else case was calculating wrong. we need to correctly handle the borrow when the nanoseconds of the first time are smaller than the second, by adding TIMESPEC_NSEC_PER_SEC and decrementing the seconds. --- src/helpers/time/Time.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/helpers/time/Time.cpp b/src/helpers/time/Time.cpp index 791f5ea1..7ef24e22 100644 --- a/src/helpers/time/Time.cpp +++ b/src/helpers/time/Time.cpp @@ -5,7 +5,6 @@ using s_ns = std::pair; -// HAS to be a > b static s_ns timediff(const s_ns& a, const s_ns& b) { s_ns d; @@ -13,7 +12,7 @@ static s_ns timediff(const s_ns& a, const s_ns& b) { if (a.second >= b.second) d.second = a.second - b.second; else { - d.second = b.second - a.second; + d.second = (TIMESPEC_NSEC_PER_SEC + a.second) - b.second; d.first -= 1; } @@ -46,9 +45,9 @@ uint64_t Time::millis(const steady_tp& tp) { } s_ns Time::secNsec(const steady_tp& tp) { - const uint64_t sec = chr::duration_cast(tp.time_since_epoch()).count(); - const chr::steady_clock::duration nsecdur = tp - chr::steady_clock::time_point(chr::seconds(sec)); - return std::make_pair<>(sec, chr::duration_cast(nsecdur).count()); + const uint64_t sec = chr::duration_cast(tp.time_since_epoch()).count(); + const auto nsecdur = tp - chr::steady_clock::time_point(chr::seconds(sec)); + return {sec, chr::duration_cast(nsecdur).count()}; } uint64_t Time::millis(const system_tp& tp) { @@ -56,9 +55,9 @@ uint64_t Time::millis(const system_tp& tp) { } s_ns Time::secNsec(const system_tp& tp) { - const uint64_t sec = chr::duration_cast(tp.time_since_epoch()).count(); - const chr::steady_clock::duration nsecdur = tp - chr::system_clock::time_point(chr::seconds(sec)); - return std::make_pair<>(sec, chr::duration_cast(nsecdur).count()); + const uint64_t sec = chr::duration_cast(tp.time_since_epoch()).count(); + const auto nsecdur = tp - chr::system_clock::time_point(chr::seconds(sec)); + return {sec, chr::duration_cast(nsecdur).count()}; } // TODO: this is a mess, but C++ doesn't define what steady_clock is. @@ -69,12 +68,12 @@ s_ns Time::secNsec(const system_tp& tp) { // In general, this may shift the time around by a couple hundred ns. Doesn't matter, realistically. Time::steady_tp Time::fromTimespec(const timespec* ts) { - struct timespec mono, real; + timespec mono{}, real{}; clock_gettime(CLOCK_MONOTONIC, &mono); clock_gettime(CLOCK_REALTIME, &real); - Time::steady_tp now = Time::steadyNow(); - Time::system_tp nowSys = Time::systemNow(); - s_ns stdSteady, stdReal; + auto now = Time::steadyNow(); + auto nowSys = Time::systemNow(); + s_ns stdSteady, stdReal; stdSteady = Time::secNsec(now); stdReal = Time::secNsec(nowSys); @@ -84,7 +83,7 @@ Time::steady_tp Time::fromTimespec(const timespec* ts) { if (real.tv_nsec >= mono.tv_nsec) diff.second = real.tv_nsec - mono.tv_nsec; else { - diff.second = mono.tv_nsec - real.tv_nsec; + diff.second = TIMESPEC_NSEC_PER_SEC + real.tv_nsec - mono.tv_nsec; diff.first -= 1; } @@ -119,7 +118,7 @@ struct timespec Time::toTimespec(const steady_tp& tp) { if (real.tv_nsec >= mono.tv_nsec) diff.second = real.tv_nsec - mono.tv_nsec; else { - diff.second = mono.tv_nsec - real.tv_nsec; + diff.second = TIMESPEC_NSEC_PER_SEC + real.tv_nsec - mono.tv_nsec; diff.first -= 1; }