Add column renderer for timestamps
authorMarcel Werk <burntime@woltlab.com>
Mon, 23 Sep 2024 15:28:37 +0000 (17:28 +0200)
committerMarcel Werk <burntime@woltlab.com>
Mon, 30 Sep 2024 11:56:32 +0000 (13:56 +0200)
wcfsetup/install/files/lib/system/view/grid/renderer/TimeColumnRenderer.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/view/grid/renderer/TimeColumnRenderer.class.php b/wcfsetup/install/files/lib/system/view/grid/renderer/TimeColumnRenderer.class.php
new file mode 100644 (file)
index 0000000..f8156e5
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace wcf\system\view\grid\renderer;
+
+use wcf\system\WCF;
+
+class TimeColumnRenderer extends AbstractColumnRenderer
+{
+    public function render(mixed $value, mixed $context = null): string
+    {
+        $timestamp = \intval($value);
+        if (!$timestamp) {
+            return '';
+        }
+
+        $dateTime = new \DateTimeImmutable('@' . $timestamp);
+        $dateTime = $dateTime->setTimezone(WCF::getUser()->getTimeZone());
+        $locale = WCF::getLanguage()->getLocale();
+
+        $isFutureDate = $dateTime->getTimestamp() > TIME_NOW;
+
+        $dateAndTime = \IntlDateFormatter::formatObject(
+            $dateTime,
+            [
+                \IntlDateFormatter::LONG,
+                \IntlDateFormatter::SHORT,
+            ],
+            $locale
+        );
+
+        return \sprintf(
+            '<woltlab-core-date-time date="%s"%s>%s</woltlab-core-date-time>',
+            $dateTime->format('c'),
+            $isFutureDate ? ' static' : '',
+            $dateAndTime
+        );
+    }
+
+    public function getClasses(): string
+    {
+        return 'columnDate';
+    }
+}