From 205603cadf80ccd6cd9757455957b1f7c3a843be Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Mon, 3 Mar 2025 18:17:21 +0000 Subject: Add an flag to ToHuman to shorten the output. --- src/helperfuncs.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/helperfuncs.cpp') diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 3cfb65d9f..4f6ccb6f2 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -504,11 +504,29 @@ std::string Duration::ToString(unsigned long duration) return ret; } -std::string Duration::ToHuman(unsigned long duration) +std::string Duration::ToHuman(unsigned long duration, bool brief) { if (duration == 0) return "0 seconds"; + if (brief) + { + // This will get inlined when compiled with optimisations. + auto nearest = [](unsigned long seconds, unsigned long roundto) { + if ((seconds % roundto) <= (roundto / 2)) + return seconds - (seconds % roundto); + return seconds - (seconds % roundto) + roundto; + }; + + // In order to get a shorter result we round to the nearest period. + if (duration >= SECONDS_PER_YEAR) + duration = nearest(duration, SECONDS_PER_DAY); // Nearest day if its more than a year + else if (duration >= SECONDS_PER_DAY) + duration = nearest(duration, SECONDS_PER_HOUR); // Nearest hour if its more than a day + else if (duration >= SECONDS_PER_HOUR) + duration = nearest(duration, SECONDS_PER_MINUTE); // Nearest minute if its more than an hour + } + std::string ret; const auto years = (duration / SECONDS_PER_YEAR); -- cgit v1.3.1-10-gc9f91