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 Nov 22, 2023
1 parent f84500e commit f422e45
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 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
40 changes: 33 additions & 7 deletions src/Latte/Essential/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -199,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],
'timeSec' => [\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
38 changes: 38 additions & 0 deletions tests/filters/localDate.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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'), 'timeSec'));

// 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, 'timeSec'));

0 comments on commit f422e45

Please sign in to comment.