diff options
| author | 2025-03-10 19:41:57 +0000 | |
|---|---|---|
| committer | 2025-03-10 19:41:57 +0000 | |
| commit | b3f973085679009d43e1c06b9d9de0ca031474a9 (patch) | |
| tree | 057b76a3ef62a649137cf80794379fd4e34e4423 /src/helperfuncs.cpp | |
| parent | Merge branch 'insp4' into master. (diff) | |
| parent | Add <helpchan> for granting privs to helpers on join. (diff) | |
Merge branch 'insp4' into master.
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 a909686c2..99bf8f510 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -498,11 +498,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); |
