Skip to content

Commit b2343a2

Browse files
authored
Added possibility to format time, removed redundant default timezone (#25)
1 parent f10b825 commit b2343a2

13 files changed

+93
-39
lines changed

β€Žcomposer.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"minimum-stability": "dev",
88
"require": {
99
"php": ">=7.4.2",
10-
"aeon-php/calendar": ">=0.7.0",
10+
"aeon-php/calendar": ">=0.9.0",
1111
"twig/twig": "^1.42|^2.10|^3"
1212
},
1313
"require-dev": {

β€Žcomposer.lockβ€Ž

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žsrc/Aeon/Twig/CalendarExtension.phpβ€Ž

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Aeon\Calendar\Gregorian\Day;
1111
use Aeon\Calendar\Gregorian\Interval;
1212
use Aeon\Calendar\Gregorian\Month;
13+
use Aeon\Calendar\Gregorian\Time;
1314
use Aeon\Calendar\Gregorian\TimeZone;
1415
use Aeon\Calendar\Gregorian\Year;
1516
use Aeon\Calendar\TimeUnit;
@@ -25,27 +26,22 @@ final class CalendarExtension extends AbstractExtension
2526

2627
private string $defaultDayFormat;
2728

28-
private ?string $defaultTimeZone;
29+
private string $defaultTimeFormat;
2930

30-
public function __construct(Calendar $calendar, string $defaultDateTimeFormat = 'Y-m-d H:i:s', string $defaultDayFormat = 'Y-m-d', string $defaultTimeZone = null)
31+
public function __construct(Calendar $calendar, string $defaultDateTimeFormat = 'Y-m-d H:i:s', string $defaultDayFormat = 'Y-m-d', string $defaultTimeFormat = 'H:i:s')
3132
{
32-
if (\is_string($defaultTimeZone)) {
33-
if (!TimeZone::isValid($defaultTimeZone)) {
34-
throw new InvalidArgumentException($defaultTimeZone . ' is not valid timezone.');
35-
}
36-
}
37-
3833
$this->calendar = $calendar;
3934
$this->defaultDateTimeFormat = $defaultDateTimeFormat;
4035
$this->defaultDayFormat = $defaultDayFormat;
41-
$this->defaultTimeZone = $defaultTimeZone;
36+
$this->defaultTimeFormat = $defaultTimeFormat;
4237
}
4338

4439
public function getFilters() : array
4540
{
4641
return [
47-
new TwigFilter('aeon_date_format', [$this, 'aeon_date_format']),
42+
new TwigFilter('aeon_datetime_format', [$this, 'aeon_datetime_format']),
4843
new TwigFilter('aeon_day_format', [$this, 'aeon_day_format']),
44+
new TwigFilter('aeon_time_format', [$this, 'aeon_time_format']),
4945
new TwigFilter('aeon_in_seconds', [$this, 'aeon_in_seconds']),
5046
new TwigFilter('aeon_in_seconds_precise', [$this, 'aeon_in_seconds_precise']),
5147
new TwigFilter('aeon_interval', [$this, 'aeon_interval']),
@@ -64,6 +60,7 @@ public function getFunctions() : array
6460
{
6561
return [
6662
new TwigFunction('aeon_now', [$this, 'aeon_now']),
63+
new TwigFunction('aeon_current_time', [$this, 'aeon_current_time']),
6764
new TwigFunction('aeon_current_day', [$this, 'aeon_current_day']),
6865
new TwigFunction('aeon_current_month', [$this, 'aeon_current_month']),
6966
new TwigFunction('aeon_current_year', [$this, 'aeon_current_year']),
@@ -74,23 +71,32 @@ public function getFunctions() : array
7471
];
7572
}
7673

77-
public function aeon_date_format(DateTime $dateTime, string $format = null, string $timezone = null) : string
74+
public function aeon_datetime_format(DateTime $dateTime, string $format = null, string $timezone = null) : string
7875
{
79-
$tz = \is_string($timezone)
80-
? $timezone
81-
: (\is_string($this->defaultTimeZone) ? $this->defaultTimeZone : null);
76+
$tz = (\is_string($timezone) && TimeZone::isValid($timezone))
77+
? new TimeZone($timezone)
78+
: null;
8279

8380
$fmt = \is_string($format)
8481
? $format
8582
: $this->defaultDateTimeFormat;
8683

87-
if (\is_string($tz)) {
88-
return $dateTime->toTimeZone(new TimeZone($tz))->format($fmt);
84+
if ($tz instanceof TimeZone) {
85+
return $dateTime->toTimeZone($tz)->format($fmt);
8986
}
9087

9188
return $dateTime->format($fmt);
9289
}
9390

91+
public function aeon_time_format(Time $time, string $format = null) : string
92+
{
93+
$fmt = \is_string($format)
94+
? $format
95+
: $this->defaultTimeFormat;
96+
97+
return $time->format($fmt);
98+
}
99+
94100
public function aeon_day_format(Day $day, string $format = null) : string
95101
{
96102
$fmt = \is_string($format)
@@ -158,6 +164,11 @@ public function aeon_now(string $timezone = null) : DateTime
158164
return $this->calendar->now();
159165
}
160166

167+
public function aeon_current_time() : Time
168+
{
169+
return $this->calendar->now()->time();
170+
}
171+
161172
public function aeon_current_day() : Day
162173
{
163174
return $this->calendar->currentDay();

β€Žtests/Aeon/Twig/Tests/Integration/CalendarExtensionTest.phpβ€Ž

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function test_extension() : void
2525
$this->assertInstanceOf(ExtensionInterface::class, new CalendarExtension($this->calendarStub));
2626
}
2727

28-
public function test_filter_aeon_date() : void
28+
public function test_filter_aeon_datetime() : void
2929
{
3030
$twig = new Environment(
3131
new FilesystemLoader(
@@ -34,11 +34,45 @@ public function test_filter_aeon_date() : void
3434
]
3535
)
3636
);
37-
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'Europe/Warsaw'));
37+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
3838

3939
$this->assertStringEqualsFile(
40-
__DIR__ . '/Fixtures/filters/aeon_date_format.txt',
41-
$twig->render('aeon_date_format.twig.txt')
40+
__DIR__ . '/Fixtures/filters/aeon_datetime_format.txt',
41+
$twig->render('aeon_datetime_format.twig.txt')
42+
);
43+
}
44+
45+
public function test_filter_aeon_day() : void
46+
{
47+
$twig = new Environment(
48+
new FilesystemLoader(
49+
[
50+
__DIR__ . '/Fixtures/filters',
51+
]
52+
)
53+
);
54+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
55+
56+
$this->assertStringEqualsFile(
57+
__DIR__ . '/Fixtures/filters/aeon_day_format.txt',
58+
$twig->render('aeon_day_format.twig.txt')
59+
);
60+
}
61+
62+
public function test_filter_aeon_time() : void
63+
{
64+
$twig = new Environment(
65+
new FilesystemLoader(
66+
[
67+
__DIR__ . '/Fixtures/filters',
68+
]
69+
)
70+
);
71+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
72+
73+
$this->assertStringEqualsFile(
74+
__DIR__ . '/Fixtures/filters/aeon_time_format.txt',
75+
$twig->render('aeon_time_format.twig.txt')
4276
);
4377
}
4478

@@ -85,7 +119,7 @@ public function test_function_aeon_now() : void
85119
]
86120
)
87121
);
88-
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d', 'Europe/Warsaw'));
122+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'Y-m-d H:i:sO', 'Y-m-d'));
89123

90124
$this->assertStringEqualsFile(
91125
__DIR__ . '/Fixtures/functions/aeon_now.txt',

β€Žtests/Aeon/Twig/Tests/Integration/Fixtures/filters/aeon_date_format.twig.txtβ€Ž

Lines changed: 0 additions & 1 deletion
This file was deleted.

β€Žtests/Aeon/Twig/Tests/Integration/Fixtures/filters/aeon_date_format.txtβ€Ž

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{ aeon_now() | aeon_datetime_format('Y-m-d') }}
2+
{{ aeon_now() | aeon_datetime_format }}
3+
{{ aeon_now() | aeon_datetime_format('Y-m-d H:i:s') }}
4+
{{ aeon_now() | aeon_datetime_format('Y-m-d H:i:s', 'UTC') }}
5+
{{ aeon_now() | aeon_datetime_format('Y-m-d H:i:s', 'Europe/Warsaw') }}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2020-01-01
2+
2020-01-01 00:00:00+0000
3+
2020-01-01 00:00:00
4+
2020-01-01 00:00:00
5+
2020-01-01 01:00:00
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{ aeon_current_day() | aeon_day_format('Y m d') }}
2+
{{ aeon_current_day() | aeon_day_format }}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2020 01 01
2+
2020-01-01

0 commit comments

Comments
Β (0)