From f8df3aa4cfeb0c29bd8fd112c4275e7c53b6bc8e Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Tue, 10 Dec 2024 08:46:48 +0100 Subject: [PATCH 1/2] fix: time diff calculation I used `ceil` instead of `floor` for weeks to help with underestimation of the time till an activity (or how long an activity would still run for). However, this resulted in a lot of confusion as it was possible for an activity to "happen" in two weeks while it would have been 1 week and 1 day. As such, the formatter now displays with multiple units if possible. But only for years, months, weeks, and days OR hours and minutes OR minutes and seconds. -- For Dutch, this also changes `jaren` to `jaar` and `uren` to `uur`. --- .../view/activity/activity/list.phtml | 2 +- module/Application/language/en.po | 14 ++-- module/Application/language/gewisweb.pot | 12 +++- module/Application/language/nl.po | 21 +++--- .../Application/src/View/Helper/TimeDiff.php | 72 +++++++++++++++---- 5 files changed, 90 insertions(+), 31 deletions(-) diff --git a/module/Activity/view/activity/activity/list.phtml b/module/Activity/view/activity/activity/list.phtml index 179221008d..a5826b4415 100644 --- a/module/Activity/view/activity/activity/list.phtml +++ b/module/Activity/view/activity/activity/list.phtml @@ -56,7 +56,7 @@ use Laminas\View\Renderer\PhpRenderer;

- translate('for another') ?> timeDiff($currentTime, $endTime) ?> + translate('for') ?> timeDiff($currentTime, $endTime) ?> translate('in') ?> timeDiff($currentTime, $beginTime) ?> diff --git a/module/Application/language/en.po b/module/Application/language/en.po index 43273b1252..2635c88d41 100644 --- a/module/Application/language/en.po +++ b/module/Application/language/en.po @@ -8,8 +8,8 @@ msgid "" msgstr "" "Project-Id-Version: GEWISweb 0.1.0-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-12-08 16:21+0100\n" -"PO-Revision-Date: 2024-12-08 16:21+0100\n" +"POT-Creation-Date: 2024-12-10 09:16+0100\n" +"PO-Revision-Date: 2024-12-10 09:18+0100\n" "Last-Translator: Tom Udding \n" "Language-Team: English \n" "Language: en\n" @@ -4259,8 +4259,8 @@ msgstr "email to the board of GEWIS" msgid "enabled" msgstr "enabled" -msgid "for another" -msgstr "for another" +msgid "for" +msgstr "for" msgid "hour" msgid_plural "hours" @@ -4273,6 +4273,9 @@ msgstr "in" msgid "in the spotlight" msgstr "in the spotlight" +msgid "less than a minute" +msgstr "less than a minute" + msgid "minute" msgid_plural "minutes" msgstr[0] "minute" @@ -4310,6 +4313,9 @@ msgstr "" "exams on this website for commercial goals without the permission of the " "author is illegal." +msgid "unable to estimate time" +msgstr "unable to estimate time" + msgid "unavailable" msgstr "unavailable" diff --git a/module/Application/language/gewisweb.pot b/module/Application/language/gewisweb.pot index 9fca3a1efc..a1d2f87f8e 100644 --- a/module/Application/language/gewisweb.pot +++ b/module/Application/language/gewisweb.pot @@ -6,9 +6,9 @@ #, fuzzy msgid "" msgstr "" -"Project-Id-Version: GEWISweb v2.8.6-822-g4410d2a12-dirty\n" +"Project-Id-Version: GEWISweb v2.8.6-825-gf1cb50e12-dirty\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-12-08 17:17+0100\n" +"POT-Creation-Date: 2024-12-10 09:16+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -3995,7 +3995,7 @@ msgstr "" msgid "enabled" msgstr "" -msgid "for another" +msgid "for" msgstr "" msgid "hour" @@ -4009,6 +4009,9 @@ msgstr "" msgid "in the spotlight" msgstr "" +msgid "less than a minute" +msgstr "" + msgid "minute" msgid_plural "minutes" msgstr[0] "" @@ -4043,6 +4046,9 @@ msgid "" "author is illegal." msgstr "" +msgid "unable to estimate time" +msgstr "" + msgid "unavailable" msgstr "" diff --git a/module/Application/language/nl.po b/module/Application/language/nl.po index f2f2a536ca..01c5203f4a 100644 --- a/module/Application/language/nl.po +++ b/module/Application/language/nl.po @@ -8,8 +8,8 @@ msgid "" msgstr "" "Project-Id-Version: GEWISweb 0.1.0-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-12-08 16:21+0100\n" -"PO-Revision-Date: 2024-12-08 16:20+0100\n" +"POT-Creation-Date: 2024-12-10 09:16+0100\n" +"PO-Revision-Date: 2024-12-10 09:25+0100\n" "Last-Translator: Tom Udding \n" "Language-Team: English \n" "Language: nl\n" @@ -4319,13 +4319,13 @@ msgstr "een mail te sturen naar het bestuur van GEWIS" msgid "enabled" msgstr "ingeschakeld" -msgid "for another" -msgstr "voor nog" +msgid "for" +msgstr "voor" msgid "hour" msgid_plural "hours" msgstr[0] "uur" -msgstr[1] "uren" +msgstr[1] "uur" msgid "in" msgstr "over" @@ -4333,6 +4333,9 @@ msgstr "over" msgid "in the spotlight" msgstr "uitgelicht" +msgid "less than a minute" +msgstr "minder dan een minuut" + msgid "minute" msgid_plural "minutes" msgstr[0] "minuut" @@ -4350,11 +4353,10 @@ msgstr "van" msgid "resolves to %s" msgstr "verwijst naar %s" -#, fuzzy msgid "second" msgid_plural "seconds" msgstr[0] "seconde" -msgstr[1] "secondes" +msgstr[1] "seconden" msgid "the organizing party" msgstr "de organiserende partij" @@ -4372,6 +4374,9 @@ msgstr "" "toestemming van de auteur te gebruiken voor commerciƫle doeleinden, dit is " "strafbaar." +msgid "unable to estimate time" +msgstr "kan de tijd niet bepalen" + msgid "unavailable" msgstr "niet beschikbaar" @@ -4389,7 +4394,7 @@ msgstr[1] "weken" msgid "year" msgid_plural "years" msgstr[0] "jaar" -msgstr[1] "jaren" +msgstr[1] "jaar" msgid "years" msgstr "jaren" diff --git a/module/Application/src/View/Helper/TimeDiff.php b/module/Application/src/View/Helper/TimeDiff.php index ec43d5e205..7e598bc4e0 100644 --- a/module/Application/src/View/Helper/TimeDiff.php +++ b/module/Application/src/View/Helper/TimeDiff.php @@ -8,7 +8,12 @@ use Laminas\Mvc\I18n\Translator; use Laminas\View\Helper\AbstractHelper; -use function ceil; +use function array_pop; +use function array_slice; +use function count; +use function floor; +use function implode; +use function sprintf; class TimeDiff extends AbstractHelper { @@ -20,26 +25,62 @@ public function __invoke( DateTime $start, DateTime $end, ): string { - $units = [ - 'year' => 31536000, - 'month' => 2592000, - 'week' => 604800, - 'day' => 86400, - 'hour' => 3600, - 'minute' => 60, - ]; + $diffInterval = $start->diff($end); + $days = false === $diffInterval->days ? 0 : $diffInterval->days; - $diff = $end->getTimestamp() - $start->getTimestamp(); + $diff = [ + 'year' => $diffInterval->y, + 'month' => $diffInterval->m, + 'week' => (int) floor($days / 7) % 4, + 'day' => $days % 7, + 'hour' => $diffInterval->h, + 'minute' => $diffInterval->i, + 'second' => $diffInterval->s, + ]; - foreach ($units as $unit => $value) { - if ($diff >= $value) { - $number = ceil($diff / $value); + $units = []; + $result = []; + if (0 === $days) { + if ( + 0 === $diff['hour'] + && 0 === $diff['minute'] + ) { + $result[] = $this->translator->translate('less than a minute'); + } elseif (0 === $diff['hour']) { + $units = ['minute', 'second']; + } else { + $units = ['hour', 'minute']; + } + } else { + $units = ['year', 'month', 'week', 'day']; + } - return $number . ' ' . $this->translator->translatePlural($unit, $unit . 's', (int) $number); + foreach ($units as $unit) { + if (0 === $diff[$unit]) { + continue; } + + $result[] = sprintf( + '%d %s', + $diff[$unit], + $this->translator->translatePlural($unit, $unit . 's', $diff[$unit]), + ); + } + + if (1 < count($result)) { + return sprintf( + '%s %s %s', + implode(', ', array_slice($result, 0, -1)), + $this->translator->translate('and'), + array_pop($result), + ); + } + + if (1 === count($result)) { + return $result[0]; } - return $diff . '' . $this->translator->translatePlural('second', 'seconds', $diff); + return $this->translator->translate('unable to estimate time'); } /** @@ -54,5 +95,6 @@ private function registerTranslations(): void $this->translator->translatePlural('day', 'days', 1); $this->translator->translatePlural('hour', 'hours', 1); $this->translator->translatePlural('minute', 'minutes', 1); + $this->translator->translatePlural('second', 'seconds', 1); } } From 0c479e8236b9fd24fbf54b3e1fa28b64ec39a187 Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Tue, 10 Dec 2024 09:20:46 +0100 Subject: [PATCH 2/2] fix: always capitalise first word of date string This is necessary to make it look good when using Dutch. This cannot be done through the ICU date/time patterns. --- module/Activity/view/activity/activity/list.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/Activity/view/activity/activity/list.phtml b/module/Activity/view/activity/activity/list.phtml index a5826b4415..93e9aecb5b 100644 --- a/module/Activity/view/activity/activity/list.phtml +++ b/module/Activity/view/activity/activity/list.phtml @@ -54,7 +54,7 @@ use Laminas\View\Renderer\PhpRenderer; $isOngoing = $currentTime >= $beginTime && $currentTime <= $endTime; ?>

- + translate('for') ?> timeDiff($currentTime, $endTime) ?>