Add an flag to ToHuman to shorten the output.

This commit is contained in:
Sadie Powell 2025-03-03 18:17:21 +00:00
parent abba3ed1e6
commit 205603cadf
7 changed files with 35 additions and 12 deletions

View File

@ -42,13 +42,18 @@ namespace Duration
*
* e.g. 33,019,565 will result in 1 year, 2 weeks, 3 days, 4 hours, 6
* minutes, 5 seconds.
*
* @param duration The number of seconds to convert.
* @param brief Whether to round to the nearest time period.
*/
CoreExport std::string ToHuman(unsigned long duration);
CoreExport std::string ToHuman(unsigned long duration, bool brief = false);
/** Converts a number of seconds to a duration string.
*
* e.g. 33,019,565 will result in 1y2w3d4h6m5s which represents one year,
* two weeks, three days, four hours, six minutes, and five seconds.
*
* @param duration The number of seconds to convert.
*/
CoreExport std::string ToString(unsigned long duration);

View File

@ -217,7 +217,7 @@ public:
std::string extra;
if (oper->IsAway())
{
const std::string awayperiod = Duration::ToHuman(ServerInstance->Time() - oper->away->time);
const std::string awayperiod = Duration::ToHuman(ServerInstance->Time() - oper->away->time, true);
const std::string awaytime = Time::ToString(oper->away->time);
extra = INSP_FORMAT(": away for {} [since {}] ({})", awayperiod, awaytime, oper->away->message);
@ -226,7 +226,7 @@ public:
auto* loper = IS_LOCAL(oper);
if (loper)
{
const std::string idleperiod = Duration::ToHuman(ServerInstance->Time() - loper->idle_lastmsg);
const std::string idleperiod = Duration::ToHuman(ServerInstance->Time() - loper->idle_lastmsg, true);
const std::string idletime = Time::ToString(loper->idle_lastmsg);
extra += INSP_FORMAT("{} idle for {} [since {}]", extra.empty() ? ':' : ',', idleperiod, idletime);

View File

@ -504,11 +504,29 @@ std::string Duration::ToString(unsigned long duration)
return ret;
}
std::string Duration::ToHuman(unsigned long duration)
std::string Duration::ToHuman(unsigned long duration, bool brief)
{
if (duration == 0)
return "0 seconds";
if (brief)
{
// This will get inlined when compiled with optimisations.
auto nearest = [](unsigned long seconds, unsigned long roundto) {
if ((seconds % roundto) <= (roundto / 2))
return seconds - (seconds % roundto);
return seconds - (seconds % roundto) + roundto;
};
// In order to get a shorter result we round to the nearest period.
if (duration >= SECONDS_PER_YEAR)
duration = nearest(duration, SECONDS_PER_DAY); // Nearest day if its more than a year
else if (duration >= SECONDS_PER_DAY)
duration = nearest(duration, SECONDS_PER_HOUR); // Nearest hour if its more than a day
else if (duration >= SECONDS_PER_HOUR)
duration = nearest(duration, SECONDS_PER_MINUTE); // Nearest minute if its more than an hour
}
std::string ret;
const auto years = (duration / SECONDS_PER_YEAR);

View File

@ -792,7 +792,7 @@ private:
else if (certinfo->activation >= ServerInstance->Time())
{
certinfo->error = INSP_FORMAT("Certificate not active for {} (on {})",
Duration::ToHuman(certinfo->activation - ServerInstance->Time()),
Duration::ToHuman(certinfo->activation - ServerInstance->Time(), true),
Time::ToString(certinfo->activation));
}
@ -805,7 +805,7 @@ private:
else if (certinfo->expiration <= ServerInstance->Time())
{
certinfo->error = INSP_FORMAT("Certificate expired {} ago (on {})",
Duration::ToHuman(ServerInstance->Time() - certinfo->expiration),
Duration::ToHuman(ServerInstance->Time() - certinfo->expiration, true),
Time::ToString(certinfo->expiration));
}

View File

@ -635,7 +635,7 @@ private:
if (activated != -1 && activated != 0)
{
certinfo->error = INSP_FORMAT("Certificate not active for {} (on {})",
Duration::ToHuman(certinfo->activation - ServerInstance->Time()),
Duration::ToHuman(certinfo->activation - ServerInstance->Time(), true),
Time::ToString(certinfo->activation));
}
@ -643,7 +643,7 @@ private:
if (expired != 0 && expired != 1)
{
certinfo->error = INSP_FORMAT("Certificate expired {} ago (on {})",
Duration::ToHuman(ServerInstance->Time() - certinfo->expiration),
Duration::ToHuman(ServerInstance->Time() - certinfo->expiration, true),
Time::ToString(certinfo->expiration));
}

View File

@ -91,7 +91,7 @@ public:
std::string extra;
if (helper->IsAway())
{
const std::string awayperiod = Duration::ToHuman(ServerInstance->Time() - helper->away->time);
const std::string awayperiod = Duration::ToHuman(ServerInstance->Time() - helper->away->time, true);
const std::string awaytime = Time::ToString(helper->away->time);
extra = INSP_FORMAT(": away for {} [since {}] ({})", awayperiod, awaytime, helper->away->message);
@ -100,7 +100,7 @@ public:
auto* lhelper = IS_LOCAL(helper);
if (lhelper)
{
const std::string idleperiod = Duration::ToHuman(ServerInstance->Time() - lhelper->idle_lastmsg);
const std::string idleperiod = Duration::ToHuman(ServerInstance->Time() - lhelper->idle_lastmsg, true);
const std::string idletime = Time::ToString(lhelper->idle_lastmsg);
extra += INSP_FORMAT("{} idle for {} [since {}]", extra.empty() ? ':' : ',', idleperiod, idletime);

View File

@ -157,7 +157,7 @@ public:
std::string extra;
if (oper->IsAway())
{
const std::string awayperiod = Duration::ToHuman(ServerInstance->Time() - oper->away->time);
const std::string awayperiod = Duration::ToHuman(ServerInstance->Time() - oper->away->time, true);
const std::string awaytime = Time::ToString(oper->away->time);
extra = INSP_FORMAT(": away for {} [since {}] ({})", awayperiod, awaytime, oper->away->message);
@ -166,7 +166,7 @@ public:
auto* loper = IS_LOCAL(oper);
if (loper)
{
const std::string idleperiod = Duration::ToHuman(ServerInstance->Time() - loper->idle_lastmsg);
const std::string idleperiod = Duration::ToHuman(ServerInstance->Time() - loper->idle_lastmsg, true);
const std::string idletime = Time::ToString(loper->idle_lastmsg);
extra += INSP_FORMAT("{} idle for {} [since {}]", extra.empty() ? ':' : ',', idleperiod, idletime);