From c2dfb9a4473cedc7a546ce81b54b2db1abb411d7 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 19 Sep 2025 20:38:33 +0100 Subject: [PATCH] Use rounded durations in more places. --- include/anope.h | 3 ++- src/misc.cpp | 42 ++++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/include/anope.h b/include/anope.h index 7e9808f55..3686e2c3f 100644 --- a/include/anope.h +++ b/include/anope.h @@ -494,9 +494,10 @@ namespace Anope /** Retrieves a human readable string representing the time in seconds * @param seconds The time on seconds, eg 60 * @param nc The account to use language settings for to translate this string, if applicable + * @param round Whether to round the duration to produce a shorter output. * @return A human readable string, eg "1 minute" */ - extern CoreExport Anope::string Duration(time_t seconds, const NickCore *nc = NULL); + extern CoreExport Anope::string Duration(time_t seconds, const NickCore *nc = nullptr, bool round = false); /** Generates a human readable string of type "expires in ..." * @param expires time in seconds diff --git a/src/misc.cpp b/src/misc.cpp index 58a55344f..374ad035b 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -554,8 +554,26 @@ time_t Anope::DoTime(const Anope::string &s) return amount; } -Anope::string Anope::Duration(time_t t, const NickCore *nc) +Anope::string Anope::Duration(time_t t, const NickCore *nc, bool round) { + if (round) + { + // This will get inlined when compiled with optimisations. + auto nearest = [](auto timeleft, auto roundto) { + if ((timeleft % roundto) <= (roundto / 2)) + return timeleft - (timeleft % roundto); + return timeleft - (timeleft % roundto) + roundto; + }; + + // In order to get a shorter result we round to the nearest period. + if (t >= 31536000) + t = nearest(t, 86400); // Nearest day if its more than a year + else if (t >= 86400) + t = nearest(t, 3600); // Nearest hour if its more than a day + else if (t >= 3600) + t = nearest(t, 60); // Nearest minute if its more than an hour + } + /* We first calculate everything */ time_t years = t / 31536000; time_t days = (t / 86400) % 365; @@ -599,9 +617,9 @@ Anope::string Anope::strftime(time_t t, const NickCore *nc, bool short_output) return buf; if (t < Anope::CurTime) - return Anope::Format(Language::Translate(nc, _("%s (%s ago)")), buf, Duration(Anope::CurTime - t, nc).c_str()); + return Anope::Format(Language::Translate(nc, _("%s (%s ago)")), buf, Duration(Anope::CurTime - t, nc, true).c_str()); else if (t > Anope::CurTime) - return Anope::Format(Language::Translate(nc, _("%s (%s from now)")), buf, Duration(t - Anope::CurTime, nc).c_str(), nc); + return Anope::Format(Language::Translate(nc, _("%s (%s from now)")), buf, Duration(t - Anope::CurTime, nc, true).c_str(), nc); else return Anope::Format(Language::Translate(nc, _("%s (now)")), buf); } @@ -614,23 +632,7 @@ Anope::string Anope::Expires(time_t expires, const NickCore *nc) if (expires <= Anope::CurTime) return Language::Translate(nc, _("expires momentarily")); - // This will get inlined when compiled with optimisations. - auto nearest = [](auto timeleft, auto roundto) { - if ((timeleft % roundto) <= (roundto / 2)) - return timeleft - (timeleft % roundto); - return timeleft - (timeleft % roundto) + roundto; - }; - - // In order to get a shorter result we round to the nearest period. - auto timeleft = expires - Anope::CurTime; - if (timeleft >= 31536000) - timeleft = nearest(timeleft, 86400); // Nearest day if its more than a year - else if (timeleft >= 86400) - timeleft = nearest(timeleft, 3600); // Nearest hour if its more than a day - else if (timeleft >= 3600) - timeleft = nearest(timeleft, 60); // Nearest minute if its more than an hour - - auto duration = Anope::Duration(timeleft, nc); + auto duration = Anope::Duration(expires - Anope::CurTime, nc, true); return Anope::Format(Language::Translate(nc, _("expires in %s")), duration.c_str()); }