Add remaining time to ban information strings

This commit is contained in:
Alexander van Gessel 2013-03-17 03:52:59 +01:00
parent 220c1fe869
commit 05e6afc22b
4 changed files with 28 additions and 1 deletions

View File

@ -124,6 +124,22 @@ std::string get_timestamp(const time_t& t, const std::string& format) {
}
return buf;
}
std::string get_timespan(const time_t& t) {
char buf[100];
// There doesn't seem to be any library function for this
const time_t minutes = t / 60;
const time_t days = minutes / 60 / 24;
if(t <= 0) {
strncpy(buf, "expired", 100);
} else if(minutes == 0) {
snprintf(buf, 100, "00:00:%02ld", t);
} else if(days == 0) {
snprintf(buf, 100, "%02ld:%02ld", minutes / 60, minutes % 60);
} else {
snprintf(buf, 100, "%ld %02ld:%02ld", days, (minutes / 60) % 24, minutes % 60);
}
return buf;
}
std::ostream &logger::operator()(log_domain const &domain, bool show_names, bool do_indent) const
{

View File

@ -94,6 +94,7 @@ public:
void timestamps(bool);
std::string get_timestamp(const time_t& t, const std::string& format="%Y%m%d %H:%M:%S ");
std::string get_timespan(const time_t& t);
extern logger err, warn, info, debug;
extern log_domain general;

View File

@ -231,6 +231,15 @@ static lg::log_domain log_server("server");
return lg::get_timestamp(end_time_);
}
std::string banned::get_human_time_span() const
{
if (end_time_ == 0)
{
return "permanent";
}
return lg::get_timespan(end_time_ - time(NULL));
}
bool banned::operator>(const banned& b) const
{
return end_time_ > b.get_end_time();
@ -646,7 +655,7 @@ static lg::log_domain log_server("server");
ban_set::const_iterator ban = std::find_if(bans_.begin(), bans_.end(), boost::bind(&banned::match_ip, boost::bind(&banned_ptr::get, _1), pair));
if (ban == bans_.end()) return "";
const std::string& nick = (*ban)->get_nick();
return (*ban)->get_reason() + (nick.empty() ? "" : " (" + nick + ")");
return (*ban)->get_reason() + (nick.empty() ? "" : " (" + nick + ")") + " (" + (*ban)->get_human_time_span() + ")";
}
void ban_manager::init_ban_help()

View File

@ -82,6 +82,7 @@ namespace wesnothd {
std::string get_human_end_time() const;
std::string get_human_start_time() const;
std::string get_human_time_span() const;
static std::string get_human_time(const time_t&);
std::string get_reason() const