Skip to content

Commit

Permalink
added filters |localDate & |localTime [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 6, 2023
1 parent cc8bbcd commit 4ff8cbd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ext-iconv": "to use filters |reverse, |substring",
"ext-mbstring": "to use filters like lower, upper, capitalize, ...",
"ext-fileinfo": "to use filter |datastream",
"ext-intl": "to use filters |localDate, |localTime",
"nette/utils": "to use filter |webalize",
"nette/php-generator": "to use tag {templatePrint}"
},
Expand Down
1 change: 1 addition & 0 deletions src/Latte/Essential/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function getFilters(): array
'join' => [Filters::class, 'implode'],
'last' => [Filters::class, 'last'],
'length' => [Filters::class, 'length'],
'localDate' => [Filters::class, 'localDate'],
'lower' => extension_loaded('mbstring')
? [Filters::class, 'lower']
: function () { throw new RuntimeException('Filter |lower requires mbstring extension.'); },
Expand Down
31 changes: 31 additions & 0 deletions src/Latte/Essential/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,37 @@ public static function date(string|int|\DateTimeInterface|\DateInterval|null $ti
}


/**
* Local date/time formatting.
* https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
*/
public static function localDate(string|int|\DateTimeInterface|null $time, string $format = 'medium'): ?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) {
'time' => [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT],
'time+sec' => [\IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM],
'short' => [\IntlDateFormatter::SHORT, \IntlDateFormatter::NONE],
'medium' => [\IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE],
'long' => [\IntlDateFormatter::LONG, \IntlDateFormatter::NONE],
'full' => [\IntlDateFormatter::FULL, \IntlDateFormatter::NONE],
default => $format,
};
$res = \IntlDateFormatter::formatObject($time, $format);
$res = preg_replace('~(\d\.) ~', "\$1\u{a0}", $res);
return $res;
}


/**
* Converts to human-readable file size.
*/
Expand Down
41 changes: 41 additions & 0 deletions tests/filters/localDate.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Test: Latte\Essential\Filters::localDate()
*/

declare(strict_types=1);

use Latte\Essential\Filters;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


ini_set('intl.default_locale', 'cs_CZ');

// types of value
Assert::null(Filters::localDate(null));
Assert::same("23.\u{a0}1.\u{a0}1978", Filters::localDate(254_400_000));
Assert::same("5.\u{a0}5.\u{a0}1978", Filters::localDate('1978-05-05'));
Assert::same("5.\u{a0}5.\u{a0}1978", Filters::localDate(new DateTime('1978-05-05')));

// predefined date format
Assert::same('05.05.78', Filters::localDate(new DateTime('1978-05-05'), 'short'));
Assert::same("5.\u{a0}5.\u{a0}1978", Filters::localDate(new DateTime('1978-05-05'), 'medium'));
Assert::same("5.\u{a0}května 1978", Filters::localDate(new DateTime('1978-05-05'), 'long'));
Assert::same("pátek 5.\u{a0}května 1978", Filters::localDate(new DateTime('1978-05-05'), 'full'));

// predefined time format
Assert::same('12:13', Filters::localDate(new DateTime('12:13:14'), 'time'));
Assert::same('12:13:14', Filters::localDate(new DateTime('12:13:14'), 'time+sec'));

// combined
Assert::same('05.05.78 12:13', Filters::localDate(new DateTime('1978-05-05'), 'short+time'));

// custom format
Assert::same("po, led 23, '78", Filters::localDate(254_400_000, "EEE, MMM d, ''yy"));

// timestamp & timezone
date_default_timezone_set('America/Los_Angeles');
Assert::same('7:09:31', Filters::localDate(1_408_284_571, 'time+sec'));

0 comments on commit 4ff8cbd

Please sign in to comment.