diff options
author | Sadie Powell <sadie@witchery.services> | 2024-11-25 01:34:35 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2024-11-25 03:02:24 +0000 |
commit | 2464913200d801ae24932119146fea109ede3346 (patch) | |
tree | 85411e011a83f20c276d6266f61236e8856576cd | |
parent | ebea7289572dde1cb0b87118e4be4f5718256941 (diff) |
Simplify the duration string logic in Anope::Expires.
This is just duplicating Anope::Duration there's no need to reimplement
a worse version of that here.
-rw-r--r-- | language/anope.en_US.po | 30 | ||||
-rw-r--r-- | language/anope.fr_FR.po | 34 | ||||
-rw-r--r-- | src/misc.cpp | 46 |
3 files changed, 27 insertions, 83 deletions
diff --git a/language/anope.en_US.po b/language/anope.en_US.po index b188270d3..b3cbcca10 100644 --- a/language/anope.en_US.po +++ b/language/anope.en_US.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: Anope\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-11-25 01:18+0000\n" -"PO-Revision-Date: 2024-11-13 02:45+0000\n" +"POT-Creation-Date: 2024-11-25 01:30+0000\n" +"PO-Revision-Date: 2024-11-25 01:32+0000\n" "Last-Translator: Sadie Powell <sadie@witchery.services>\n" "Language-Team: English\n" "Language: en_US\n" @@ -7379,33 +7379,9 @@ msgid "does not expire" msgstr "" #, c-format -msgid "expires in %d day" -msgid_plural "expires in %d days" -msgstr[0] "" -msgstr[1] "" - -#, c-format -msgid "expires in %d hour, %d minute" +msgid "expires in %s" msgstr "" -#, c-format -msgid "expires in %d hour, %d minutes" -msgstr "" - -#, c-format -msgid "expires in %d hours, %d minute" -msgstr "" - -#, c-format -msgid "expires in %d hours, %d minutes" -msgstr "" - -#, c-format -msgid "expires in %d minute" -msgid_plural "expires in %d minutes" -msgstr[0] "" -msgstr[1] "" - msgid "expires momentarily" msgstr "" diff --git a/language/anope.fr_FR.po b/language/anope.fr_FR.po index 0e85e48c3..4e88a1968 100644 --- a/language/anope.fr_FR.po +++ b/language/anope.fr_FR.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: Anope\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-11-25 01:18+0000\n" -"PO-Revision-Date: 2024-11-22 21:32+0100\n" +"POT-Creation-Date: 2024-11-25 01:30+0000\n" +"PO-Revision-Date: 2024-11-25 01:32+0000\n" "Last-Translator: Val Lorentz <progval+git@progval.net>\n" "Language-Team: French\n" "Language: fr_FR\n" @@ -16,7 +16,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -"X-Generator: Poedit 3.2.2\n" +"X-Generator: Poedit 3.4.2\n" #, c-format msgid "%d channel(s) cleared, and %d channel(s) dropped." @@ -8937,32 +8937,8 @@ msgid "does not expire" msgstr "n'expire pas" #, c-format -msgid "expires in %d day" -msgid_plural "expires in %d days" -msgstr[0] "expire dans %d jour" -msgstr[1] "expire dans %d jours" - -#, c-format -msgid "expires in %d hour, %d minute" -msgstr "expire dans %d heure, %d minute" - -#, c-format -msgid "expires in %d hour, %d minutes" -msgstr "expire dans %d heure, %d minutes" - -#, c-format -msgid "expires in %d hours, %d minute" -msgstr "expire dans %d heures, %d minute" - -#, c-format -msgid "expires in %d hours, %d minutes" -msgstr "expire dans %d heures, %d minutes" - -#, c-format -msgid "expires in %d minute" -msgid_plural "expires in %d minutes" -msgstr[0] "expire dans %d minute" -msgstr[1] "expire dans %d minutes" +msgid "expires in %s" +msgstr "expire dans %s" msgid "expires momentarily" msgstr "expire d'un instant à l'autre" diff --git a/src/misc.cpp b/src/misc.cpp index 6cf83c3f1..2a51c7872 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -355,36 +355,28 @@ Anope::string Anope::Expires(time_t expires, const NickCore *nc) { if (!expires) return Language::Translate(nc, NO_EXPIRE); - else if (expires <= Anope::CurTime) + + if (expires <= Anope::CurTime) return Language::Translate(nc, _("expires momentarily")); - else - { - char buf[256]; - time_t diff = expires - Anope::CurTime + 59; - if (diff >= 86400) - { - int days = diff / 86400; - snprintf(buf, sizeof(buf), Language::Translate(nc, days, N_("expires in %d day", "expires in %d days")), days); - } - else - { - if (diff <= 3600) - { - int minutes = diff / 60; - snprintf(buf, sizeof(buf), Language::Translate(nc, minutes, N_("expires in %d minute", "expires in %d minutes")), minutes); - } - else - { - int hours = diff / 3600, minutes; - diff -= hours * 3600; - minutes = diff / 60; - snprintf(buf, sizeof(buf), Language::Translate(nc, hours == 1 && minutes == 1 ? _("expires in %d hour, %d minute") : (hours == 1 && minutes != 1 ? _("expires in %d hour, %d minutes") : (hours != 1 && minutes == 1 ? _("expires in %d hours, %d minute") : _("expires in %d hours, %d minutes")))), hours, minutes); - } - } + // 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; + }; - return buf; - } + // 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); + return Anope::printf(Language::Translate(nc, _("expires in %s")), duration.c_str()); } bool Anope::Match(const Anope::string &str, const Anope::string &mask, bool case_sensitive, bool use_regex) |