From e459b750de4e81af8fe770aaef12c7a9860cbb1d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 20 Nov 2023 02:37:13 +0100 Subject: [PATCH] added filters |localDate & |localTime WIP --- src/Latte/Essential/CoreExtension.php | 2 ++ src/Latte/Essential/Filters.php | 35 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Latte/Essential/CoreExtension.php b/src/Latte/Essential/CoreExtension.php index 63346bbe2..58f0aa908 100644 --- a/src/Latte/Essential/CoreExtension.php +++ b/src/Latte/Essential/CoreExtension.php @@ -141,6 +141,8 @@ public function getFilters(): array 'join' => [Filters::class, 'implode'], 'last' => [Filters::class, 'last'], 'length' => [Filters::class, 'length'], + 'localDate' => fn($date, string $format = 'medium') => Filters::localDateTime($date, $format, date: true), + 'localTime' => fn($time, string $format = 'medium') => Filters::localDateTime($time, $format, date: false), 'lower' => extension_loaded('mbstring') ? [Filters::class, 'lower'] : function () { throw new RuntimeException('Filter |lower requires mbstring extension.'); }, diff --git a/src/Latte/Essential/Filters.php b/src/Latte/Essential/Filters.php index 9c07ecca0..17b4654a9 100644 --- a/src/Latte/Essential/Filters.php +++ b/src/Latte/Essential/Filters.php @@ -172,17 +172,12 @@ public static function date(string|int|\DateTimeInterface|\DateInterval|null $ti return null; } - if (!isset($format)) { - $format = Latte\Runtime\Filters::$dateFormat; - } - + $format ??= Latte\Runtime\Filters::$dateFormat; if ($time instanceof \DateInterval) { return $time->format($format); } elseif (is_numeric($time)) { - $time = new \DateTime('@' . $time); - $time->setTimeZone(new \DateTimeZone(date_default_timezone_get())); - + $time = (new \DateTime)->setTimestamp((int) $time); } elseif (!$time instanceof \DateTimeInterface) { $time = new \DateTime($time); } @@ -199,6 +194,32 @@ public static function date(string|int|\DateTimeInterface|\DateInterval|null $ti } + /** + * Local date/time formatting. + */ + public static function localDateTime(string|int|\DateTimeInterface|null $time, string $format, bool $date): ?string + { + if (!class_exists(\IntlDateFormatter::class)) { + throw new Latte\RuntimeException("Filters |localDate and |localTime requires 'intl' extension."); + } elseif ($time == null) { // intentionally == + return null; + } elseif (is_numeric($time)) { + $time = (new \DateTime)->setTimestamp((int) $time); + } elseif (is_string($time)) { + $time = new \DateTime($time); + } + + $format = match ($format) { + 'short' => \IntlDateFormatter::SHORT, + 'medium' => \IntlDateFormatter::MEDIUM, + 'long' => \IntlDateFormatter::LONG, + 'full' => \IntlDateFormatter::FULL, + default => throw new Latte\RuntimeException("Unknown format '$format' used in |localDate or |localTime."), + }; + return \IntlDateFormatter::formatObject($time, $date ? [$format, \IntlDateFormatter::NONE] : [\IntlDateFormatter::NONE, $format]); + } + + /** * Converts to human readable file size. */