Skip to content

Commit e72836f

Browse files
committed
use tag param to set timezone
1 parent 8aec565 commit e72836f

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/Events.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Events
4444

4545
private array $terms = [];
4646

47+
private ?string $timezone = null;
48+
4749
public static function defaultTimezone(): string
4850
{
4951
return static::setting('timezone', config('statamic.system.display_timezone') ?? config('app.timezone', 'UTC'));
@@ -139,6 +141,13 @@ public function terms(string|array $terms): self
139141
return $this;
140142
}
141143

144+
public function timezone(string $timezone): self
145+
{
146+
$this->timezone = $timezone;
147+
148+
return $this;
149+
}
150+
142151
public function between(string|CarbonInterface $from, string|CarbonInterface $to): EntryCollection|LengthAwarePaginator
143152
{
144153
return $this->output(
@@ -157,6 +166,13 @@ private function output(callable $type): EntryCollection|LengthAwarePaginator
157166
{
158167
$occurrences = $this->entries()->occurrences(generator: $type);
159168

169+
if (! is_null($this->timezone)) {
170+
$occurrences->transform(fn (Entry $occurrence) => $occurrence
171+
->setSupplement('start', $occurrence->start->setTimezone($this->timezone))
172+
->setSupplement('end', $occurrence->end->setTimezone($this->timezone))
173+
);
174+
}
175+
160176
if ($this->offset) {
161177
$occurrences = $occurrences->slice(offset: $this->offset);
162178
}

src/Tags/Events.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ private function generator(): Generator
193193
)->when(
194194
value: $this->params->bool('collapse_multi_days'),
195195
callback: fn (Generator $generator) => $generator->collapseMultiDays()
196+
)->when(
197+
value: $this->params->get('timezone'),
198+
callback: fn (Generator $generator, string $tz) => $generator->timezone(timezone: $tz)
196199
);
197200
}
198201

tests/Tags/EventsTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Statamic\Facades\Site as SiteFacade;
99
use Statamic\Sites\Site;
1010
use Statamic\Support\Arr;
11-
use TransformStudios\Events\Tags\Events;
11+
use TransformStudios\Events\Tags\Events as EventsTag;
1212

1313
beforeEach(function () {
1414
Entry::make()
@@ -24,7 +24,7 @@
2424
'categories' => ['one'],
2525
])->save();
2626

27-
$this->tag = app(Events::class);
27+
$this->tag = app(EventsTag::class);
2828
});
2929

3030
test('can generate between occurrences', function () {
@@ -409,3 +409,21 @@
409409
'long' => 'Sunday',
410410
]);
411411
});
412+
413+
test('uses the timezone param when generating occurrences', function () {
414+
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));
415+
416+
$this->tag
417+
->setContext([])
418+
->setParameters([
419+
'collection' => 'events',
420+
'from' => Carbon::now()->subDay(),
421+
'timezone' => 'America/Vancouver',
422+
'to' => Carbon::now()->addDays(2),
423+
]);
424+
425+
$occurrences = $this->tag->between();
426+
427+
expect($occurrences)->toHaveCount(1)
428+
->first()->start->timezone->getName()->toBe('America/Vancouver');
429+
});

0 commit comments

Comments
 (0)