Skip to content

Commit ce54aca

Browse files
authored
Restore default timezone in Calendar Extension (#30)
1 parent 16c8853 commit ce54aca

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/Aeon/Twig/CalendarExtension.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ final class CalendarExtension extends AbstractExtension
2222
{
2323
private Calendar $calendar;
2424

25+
private TimeZone $defaultTimeZone;
26+
2527
private string $defaultDateTimeFormat;
2628

2729
private string $defaultDayFormat;
2830

2931
private string $defaultTimeFormat;
3032

31-
public function __construct(Calendar $calendar, string $defaultDateTimeFormat = 'Y-m-d H:i:s', string $defaultDayFormat = 'Y-m-d', string $defaultTimeFormat = 'H:i:s')
33+
public function __construct(Calendar $calendar, string $defaultTimeZone = 'UTC', string $defaultDateTimeFormat = 'Y-m-d H:i:s', string $defaultDayFormat = 'Y-m-d', string $defaultTimeFormat = 'H:i:s')
3234
{
35+
if (!TimeZone::isValid($defaultTimeZone)) {
36+
throw new InvalidArgumentException($defaultTimeZone . ' is not valid timezone name.');
37+
}
38+
3339
$this->calendar = $calendar;
40+
$this->defaultTimeZone = new TimeZone($defaultTimeZone);
3441
$this->defaultDateTimeFormat = $defaultDateTimeFormat;
3542
$this->defaultDayFormat = $defaultDayFormat;
3643
$this->defaultTimeFormat = $defaultTimeFormat;
@@ -85,7 +92,7 @@ public function aeon_datetime_format(DateTime $dateTime, string $format = null,
8592
return $dateTime->toTimeZone($tz)->format($fmt);
8693
}
8794

88-
return $dateTime->toTimeZone($this->calendar->timeZone())->format($fmt);
95+
return $dateTime->toTimeZone($this->defaultTimeZone)->format($fmt);
8996
}
9097

9198
public function aeon_time_format(Time $time, string $format = null) : string

tests/Aeon/Twig/Tests/Integration/CalendarExtensionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function test_filter_aeon_datetime() : void
3434
]
3535
)
3636
);
37-
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
37+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'UTC', 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
3838

3939
$this->assertStringEqualsFile(
4040
__DIR__ . '/Fixtures/filters/aeon_datetime_format.txt',
@@ -51,7 +51,7 @@ public function test_filter_aeon_day() : void
5151
]
5252
)
5353
);
54-
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
54+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'UTC', 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
5555

5656
$this->assertStringEqualsFile(
5757
__DIR__ . '/Fixtures/filters/aeon_day_format.txt',
@@ -68,7 +68,7 @@ public function test_filter_aeon_time() : void
6868
]
6969
)
7070
);
71-
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
71+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'UTC', 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
7272

7373
$this->assertStringEqualsFile(
7474
__DIR__ . '/Fixtures/filters/aeon_time_format.txt',
@@ -119,7 +119,7 @@ public function test_function_aeon_now() : void
119119
]
120120
)
121121
);
122-
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d'));
122+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'UTC', 'Y-m-d H:i:sO', 'Y-m-d'));
123123

124124
$this->assertStringEqualsFile(
125125
__DIR__ . '/Fixtures/functions/aeon_now.txt',

tests/Aeon/Twig/Tests/Unit/CalendarExtensionTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717
final class CalendarExtensionTest extends TestCase
1818
{
19+
public function test_create_with_invalid_timezone() : void
20+
{
21+
$this->expectException(InvalidArgumentException::class);
22+
$this->expectExceptionMessage('not_timezone is not valid timezone name.');
23+
24+
new CalendarExtension($calendar = new GregorianCalendarStub(), 'not_timezone');
25+
}
26+
1927
public function test_aeon_now() : void
2028
{
2129
$extension = new CalendarExtension($calendar = new GregorianCalendarStub());
@@ -29,7 +37,8 @@ public function test_aeon_now() : void
2937
public function test_aeon_datetime_format_takes_timezone_from_calendar_instance_when_not_provided() : void
3038
{
3139
$extension = new CalendarExtension(
32-
$calendar = new GregorianCalendarStub(new \DateTimeImmutable('2020-01-01 Europe/Warsaw'))
40+
$calendar = new GregorianCalendarStub(new \DateTimeImmutable('2020-01-01 UTC')),
41+
'Europe/Warsaw'
3342
);
3443

3544
$this->assertEquals(

0 commit comments

Comments
 (0)