diff options
| author | 2025-03-03 18:17:21 +0000 | |
|---|---|---|
| committer | 2025-03-03 18:19:49 +0000 | |
| commit | 205603cadf80ccd6cd9757455957b1f7c3a843be (patch) | |
| tree | 3bbd021c7d40b862d3c339141dfbf17c0f5e6dd5 /src/helperfuncs.cpp | |
| parent | Use Duration::ToHuman instead of seconds in various messages. (diff) | |
Add an flag to ToHuman to shorten the output.
Diffstat (limited to 'src/helperfuncs.cpp')
| -rw-r--r-- | src/helperfuncs.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
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); |
