1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Make /status message easier to read

This commit is contained in:
Wuzzy 2021-03-17 19:03:00 +01:00 committed by sfan5
parent 02292e03e4
commit fe7195badb
2 changed files with 31 additions and 10 deletions

View file

@ -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();