Skip to content

Commit c89b60d

Browse files
committed
issue-46
Signed-off-by: Jurj-Bogdan <[email protected]>
1 parent 88f8b24 commit c89b60d

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"mezzio/mezzio-twigrenderer": "^2.15",
2222
"laminas/laminas-view": "^2.33",
2323
"laminas/laminas-authentication": "^2.16",
24-
"dotkernel/dot-navigation": "^3.4.2",
24+
"dotkernel/dot-navigation": "^3.5.1",
2525
"dotkernel/dot-flashmessenger": "^3.4.2",
2626
"laminas/laminas-form": "^3.19.1",
2727
"dotkernel/dot-authorization": "^3.4.1"

psalm.xml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88
xmlns="https://getpsalm.org/schema/config"
99
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
10-
errorBaseline="psalm-baseline.xml"
1110
>
1211
<projectFiles>
1312
<directory name="src" />

src/Extension/DateExtension.php

+54-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
namespace Dot\Twig\Extension;
66

7+
use DateTime;
8+
use DateTimeImmutable;
79
use DateTimeInterface;
10+
use DateTimeZone;
11+
use Exception;
812
use Twig\Environment;
913
use Twig\Extension\AbstractExtension;
1014
use Twig\Extension\CoreExtension;
1115
use Twig\TwigFilter;
1216

17+
use function ctype_digit;
18+
use function strtolower;
19+
use function substr;
20+
1321
class DateExtension extends AbstractExtension
1422
{
1523
public static array $units = [
@@ -31,15 +39,24 @@ public function getFilters(): array
3139
/**
3240
* Filters for converting dates to a time ago string like Facebook and Twitter has.
3341
* If none given, the current time will be used.
42+
*
43+
* @throws Exception
3444
*/
3545
public function diff(
3646
Environment $env,
3747
string|DateTimeInterface|null $date,
38-
string|DateTimeInterface|null $now = null
48+
string|DateTimeInterface|null $now = null,
49+
string|DateTimeZone|null $timezone = null,
3950
): string {
51+
if (null === $timezone) {
52+
$timezone = $env->getExtension(CoreExtension::class)->getTimezone();
53+
} elseif (! $timezone instanceof DateTimeZone) {
54+
$timezone = new DateTimeZone($timezone);
55+
}
56+
4057
// Convert both dates to DateTime instances.
41-
$date = $env->getExtension(CoreExtension::class)->convertDate($date);
42-
$now = $env->getExtension(CoreExtension::class)->convertDate($now);
58+
$date = $this->convertDate($date, $timezone);
59+
$now = $this->convertDate($now, $timezone);
4360

4461
// Get the difference between the two DateTime objects.
4562
$diff = $date->diff($now);
@@ -56,6 +73,40 @@ public function diff(
5673
return '';
5774
}
5875

76+
/**
77+
* @throws Exception
78+
*/
79+
protected function convertDate(string|DateTimeInterface|null $date, DateTimeZone $timezone): DateTimeInterface
80+
{
81+
if ($date instanceof DateTimeImmutable) {
82+
return $date->setTimezone($timezone);
83+
}
84+
85+
if ($date instanceof DateTimeInterface) {
86+
$date = clone $date;
87+
$date->setTimezone($timezone);
88+
89+
return $date;
90+
}
91+
92+
if (null === $date || 'now' === strtolower($date)) {
93+
if (null === $date) {
94+
$date = 'now';
95+
}
96+
97+
return new DateTime($date, $timezone);
98+
}
99+
100+
if (
101+
ctype_digit($date)
102+
|| (! empty($date) && '-' === $date[0] && ctype_digit(substr($date, 1)))
103+
) {
104+
return new DateTime('@' . $date, $timezone);
105+
} else {
106+
return new DateTime($date, $timezone);
107+
}
108+
}
109+
59110
public function getPluralizedInterval(mixed $count, int $invert, string $unit): string
60111
{
61112
if (1 !== $count) {

test/Extension/DateExtensionTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DotTest\Twig\Extension;
66

7+
use DateTimeImmutable;
78
use Dot\Twig\Extension\DateExtension;
89
use Exception;
910
use PHPUnit\Framework\MockObject\MockObject;
@@ -38,7 +39,16 @@ public function testFilters(): void
3839

3940
public function testDiffWillReturnString(): void
4041
{
41-
$this->assertIsString($this->extension->diff($this->env, "2023-07-31"));
42+
$this->assertIsString($this->extension->diff($this->env, '2023-07-31'));
43+
$this->assertIsString(
44+
$this->extension->diff(
45+
$this->env,
46+
new DateTimeImmutable('yesterday'),
47+
new DateTimeImmutable(),
48+
'GMT+2'
49+
)
50+
);
51+
$this->assertIsString($this->extension->diff($this->env, '802587600'));
4252
}
4353

4454
public function testDiffWillReturnExceptionUnexpectedCharacters(): void

0 commit comments

Comments
 (0)