diff options
| author | 2025-03-02 14:15:02 +0000 | |
|---|---|---|
| committer | 2025-03-03 14:00:16 +0000 | |
| commit | 2102b065d4ed91c5199c1dececdec4e385657cfd (patch) | |
| tree | 344337bb5099dcbf3c0aba113d3ec07955ef00f9 /src/helperfuncs.cpp | |
| parent | Add an 005 token to let clients know that fakelag is enabled. (diff) | |
Add Duration::ToHuman and update messages to use it.
Diffstat (limited to 'src/helperfuncs.cpp')
| -rw-r--r-- | src/helperfuncs.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index a43c4d9dd..3cfb65d9f 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -504,6 +504,77 @@ std::string Duration::ToString(unsigned long duration) return ret; } +std::string Duration::ToHuman(unsigned long duration) +{ + if (duration == 0) + return "0 seconds"; + + std::string ret; + + const auto years = (duration / SECONDS_PER_YEAR); + if (years) + { + ret = INSP_FORMAT("{} {}", years, years == 1 ? "year" : "years"); + duration -= (years * SECONDS_PER_YEAR); + } + + const auto weeks = (duration / SECONDS_PER_WEEK); + if (weeks) + { + ret += ret.empty() ? "" : ", "; + ret += INSP_FORMAT("{} {}", weeks, weeks == 1 ? "week" : "weeks"); + duration -= (weeks * SECONDS_PER_WEEK); + } + + const auto days = (duration / SECONDS_PER_DAY); + if (days) + { + ret += ret.empty() ? "" : ", "; + ret += INSP_FORMAT("{} {}", days, days == 1 ? "day" : "days"); + duration -= (days * SECONDS_PER_DAY); + } + + const auto hours = (duration / SECONDS_PER_HOUR); + if (hours) + { + ret += ret.empty() ? "" : ", "; + ret += INSP_FORMAT("{} {}", hours, hours == 1 ? "hour" : "hours"); + duration -= (hours * SECONDS_PER_HOUR); + } + + const auto minutes = (duration / SECONDS_PER_MINUTE); + if (minutes) + { + ret += ret.empty() ? "" : ", "; + ret += INSP_FORMAT("{} {}", minutes, minutes == 1 ? "minute" : "minutes"); + duration -= (minutes * SECONDS_PER_MINUTE); + } + + if (duration) + { + ret += ret.empty() ? "" : ", "; + ret += INSP_FORMAT("{} {}", duration, duration == 1 ? "second" : "seconds"); + } + + const auto first_comma = ret.find(','); + const auto last_comma = ret.rfind(','); + if (first_comma != std::string::npos) + { + if (first_comma == last_comma) + { + // BEFORE: 1 minute, 2 seconds + // AFTER: 1 minute and 2 seconds + ret.replace(last_comma, 1, " and"); + } + else + { + // BEFORE: 1 hour, 2 minutes, 3 seconds + // AFTER: 1 hour, 2 minutes, and 3 seconds + ret.insert(last_comma + 1, " and"); + } + } + return ret; +} std::string Time::ToString(time_t curtime, const char* format, bool utc) { |
