Improve the runtime performance of `|date`
authorAlexander Ebert <ebert@woltlab.com>
Wed, 17 Apr 2024 17:01:10 +0000 (19:01 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:31:07 +0000 (12:31 +0200)
See 75f848c4f07ef00dfa0b191665f40d8a81fa2d0c

wcfsetup/install/files/lib/system/template/plugin/DateModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php

index 2747716f4a09299e2d802392e01518d221d16eb2..5210ab12c01dcafe0c3dfcfaf613d030aad1c2e0 100644 (file)
@@ -21,6 +21,9 @@ use wcf\util\DateUtil;
  */
 class DateModifierTemplatePlugin implements IModifierTemplatePlugin
 {
+    /** @var array<string, \IntlDateFormatter> */
+    private array $dateFormatter = [];
+
     /**
      * @inheritDoc
      */
@@ -39,12 +42,23 @@ class DateModifierTemplatePlugin implements IModifierTemplatePlugin
                 $tagArgs[1]
             );
         } else {
-            return \IntlDateFormatter::create(
-                WCF::getLanguage()->getLocale(),
-                \IntlDateFormatter::LONG,
-                \IntlDateFormatter::NONE,
-                WCF::getUser()->getTimeZone()
-            )->format($dateTime);
+            $locale = WCF::getLanguage()->getLocale();
+            $timeZone = WCF::getUser()->getTimeZone();
+
+            $key = $locale . '::' . $timeZone->getName();
+            $dateFormatter = $this->dateFormatter[$key] ?? null;
+            if ($dateFormatter === null) {
+                $dateFormatter = \IntlDateFormatter::create(
+                    $locale,
+                    \IntlDateFormatter::LONG,
+                    \IntlDateFormatter::NONE,
+                    $timeZone
+                );
+
+                $this->dateFormatter[$key] = $dateFormatter;
+            }
+
+            return $dateFormatter->format($dateTime);
         }
     }
 }
index cb59f8d35fef104fde5109006f6fead3b50c4045..9f68000d263becf930dad3a1ebcb109c5823445b 100644 (file)
@@ -44,10 +44,10 @@ class TimeModifierTemplatePlugin implements IModifierTemplatePlugin
         $dateFormatter = $this->dateFormatter[$key] ?? null;
         if ($dateFormatter === null) {
             $dateFormatter = \IntlDateFormatter::create(
-                WCF::getLanguage()->getLocale(),
+                $locale,
                 \IntlDateFormatter::LONG,
                 \IntlDateFormatter::SHORT,
-                WCF::getUser()->getTimeZone()
+                $timeZone
             );
 
             $this->dateFormatter[$key] = $dateFormatter;