Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use DateTimeZone;
use Exception;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -43,7 +44,7 @@ class Events

public static function defaultTimezone(): string
{
return static::setting('timezone');
return static::resolveTimezone();
}

public static function fromCollection(string $handle): self
Expand All @@ -56,6 +57,17 @@ public static function fromEntry(string $id): self
return new static(new Parameters(['event' => $id]));
}

public static function resolveTimezone(mixed $timezone = null): string
{
return collect([
$timezone,
static::setting('timezone'),
config('statamic.system.display_timezone'),
config('app.timezone'),
'UTC',
])->first(fn (mixed $candidate) => static::isValidTimezone($candidate));
}

public function __construct(Parameters $params)
{
throw_if(
Expand All @@ -71,7 +83,7 @@ public function __construct(Parameters $params)
->offset(offset: $params->int('offset'))
->pagination(page: Paginator::resolveCurrentPage(), perPage: $params->int('paginate'))
->sort($params->get('sort', 'asc'))
->timezone(timezone: $params->get('timezone', static::defaultTimezone()));
->timezone(timezone: $params->get('timezone'));
}

public static function setting(string $key, $default = null): mixed
Expand Down Expand Up @@ -172,9 +184,9 @@ public function terms(string|array $terms): self
return $this;
}

public function timezone(string $timezone): self
public function timezone(?string $timezone = null): self
{
$this->timezone = $timezone;
$this->timezone = static::resolveTimezone($timezone);

return $this;
}
Expand All @@ -195,6 +207,21 @@ public function upcoming(int $limit = 1): EntryCollection|LengthAwarePaginator
);
}

private static function isValidTimezone(mixed $timezone): bool
{
if (! is_string($timezone) || ! filled($timezone)) {
return false;
}

try {
new DateTimeZone($timezone);
} catch (Exception) {
return false;
}

return true;
}

private function output(callable $type, string|CarbonInterface $from): EntryCollection|LengthAwarePaginator
{
$occurrences = $this->entries()->occurrences(generator: $type, from: $from);
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function calendar(): Collection

$month = $this->params->get('month', now()->englishMonth);
$year = $this->params->get('year', now()->year);
$timezone = $this->params->get('timezone', Generator::defaultTimezone());
$timezone = Generator::resolveTimezone($this->params->get('timezone'));

$from = parse_date($month.' '.$year)->shiftTimezone($timezone)->startOfMonth()->startOfWeek();
$to = parse_date($month.' '.$year)->shiftTimezone($timezone)->endOfMonth()->endOfWeek();
Expand Down
37 changes: 37 additions & 0 deletions tests/Tags/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Statamic\Facades\Site as SiteFacade;
use Statamic\Sites\Site;
use Statamic\Support\Arr;
use TransformStudios\Events\Events;
use TransformStudios\Events\Tags\Events as EventsTag;

beforeEach(function () {
Expand Down Expand Up @@ -663,6 +664,42 @@
->first()->start->timezone->getName()->toBe('America/Vancouver');
});

it('falls back to the default timezone when the timezone param is empty', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

$this->tag
->setContext([])
->setParameters([
'collection' => 'events',
'from' => Carbon::now()->subDay(),
'timezone' => '',
'to' => Carbon::now()->addDays(2),
]);

$occurrences = $this->tag->between();

expect($occurrences)->toHaveCount(1)
->first()->start->timezone->getName()->toBe(Events::defaultTimezone());
});

it('falls back to the default timezone when the timezone param is invalid', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

$this->tag
->setContext([])
->setParameters([
'collection' => 'events',
'from' => Carbon::now()->subDay(),
'timezone' => 'not-a-real-timezone',
'to' => Carbon::now()->addDays(2),
]);

$occurrences = $this->tag->between();

expect($occurrences)->toHaveCount(1)
->first()->start->timezone->getName()->toBe(Events::defaultTimezone());
});

it('sets "spanning"', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

Expand Down