wesnothd: format logged-in time directly

This was the last use of lg::format_timespan, which was only ever used in server code anyway. The server probably shouldn't be formatting this anyway, but as long as it is, we don't need a dedicated function or fancy formatting.
This commit is contained in:
Charles Dang 2024-12-01 21:42:04 -05:00
parent 70c89f819d
commit 0ed106c4e2
3 changed files with 7 additions and 27 deletions

View File

@ -401,30 +401,6 @@ bool broke_strict() {
return strict_threw_;
}
std::string format_timespan(const std::chrono::seconds& span)
{
if(span <= std::chrono::seconds{0}) {
return "expired";
}
auto [days, hours, minutes, seconds] = chrono::deconstruct_duration(chrono::format::days_hours_mins_secs, span);
std::vector<std::string> formatted_values;
// TODO C++20: see if we can use the duration stream operators
const auto format_time = [&formatted_values](const auto& val, const std::string& suffix) {
if(val > std::decay_t<decltype(val)>{0}) {
formatted_values.push_back(formatter{} << val.count() << " " << suffix);
}
};
format_time(days, "days");
format_time(hours, "hours");
format_time(minutes, "minutes");
format_time(seconds, "seconds");
return utils::join(formatted_values, ", ");
}
void set_log_sanitize(bool sanitize) {
log_sanitization = sanitize;
}

View File

@ -230,8 +230,6 @@ public:
void timestamps(bool);
void precise_timestamps(bool);
/** TODO: we also have utils::format_timespan, which does something very similar... */
std::string format_timespan(const std::chrono::seconds& span);
std::string sanitize_log(const std::string& logstr);
std::string get_log_file_path();

View File

@ -188,9 +188,15 @@ static bool make_change_diff(const simple_wml::node& src,
static std::string player_status(const wesnothd::player_record& player)
{
auto logged_on_time = std::chrono::steady_clock::now() - player.login_time;
auto [d, h, m, s] = chrono::deconstruct_duration(chrono::format::days_hours_mins_secs, logged_on_time);
std::ostringstream out;
out << "'" << player.name() << "' @ " << player.client_ip()
<< " logged on for " << lg::format_timespan(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - player.login_time));
<< " logged on for "
<< d.count() << " days, "
<< h.count() << " hours, "
<< m.count() << " minutes, "
<< s.count() << " seconds";
return out.str();
}