diff options
author | Wuzzy <Wuzzy2@mail.ru> | 2021-03-17 19:03:00 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2021-10-15 18:13:57 +0200 |
commit | fe7195badb2801f4957d6dea2c961a3ffcf7debf (patch) | |
tree | 8d83ecd2d5e2c4d2efaf111d7862f98308dc91e0 /src/util | |
parent | 02292e03e42e5c3be0aa09a329dabd9c8dad5571 (diff) | |
download | minetest-fe7195badb2801f4957d6dea2c961a3ffcf7debf.tar.gz minetest-fe7195badb2801f4957d6dea2c961a3ffcf7debf.tar.bz2 minetest-fe7195badb2801f4957d6dea2c961a3ffcf7debf.zip |
Make /status message easier to read
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/string.h | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/util/string.h b/src/util/string.h index 21f1d6877..bca998f56 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -661,28 +661,49 @@ inline const char *bool_to_cstr(bool val) return val ? "true" : "false"; } +/** + * Converts a duration in seconds to a pretty-printed duration in + * days, hours, minutes and seconds. + * + * @param sec duration in seconds + * @return pretty-printed duration + */ inline const std::string duration_to_string(int sec) { + std::ostringstream ss; + const char *neg = ""; + if (sec < 0) { + sec = -sec; + neg = "-"; + } + int total_sec = sec; int min = sec / 60; sec %= 60; int hour = min / 60; min %= 60; + int day = hour / 24; + hour %= 24; + + if (day > 0) { + ss << neg << day << "d"; + if (hour > 0 || min > 0 || sec > 0) + ss << " "; + } - std::stringstream ss; if (hour > 0) { - ss << hour << "h"; + ss << neg << hour << "h"; if (min > 0 || sec > 0) ss << " "; } if (min > 0) { - ss << min << "min"; + ss << neg << min << "min"; if (sec > 0) ss << " "; } - if (sec > 0) { - ss << sec << "s"; + if (sec > 0 || total_sec == 0) { + ss << neg << sec << "s"; } return ss.str(); |