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
16 changes: 14 additions & 2 deletions app/Enums/EventTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Dto\ReaderClock;
use Carbon\CarbonInterface;
use Illuminate\Http\Request;
use IntlCalendar;
use IntlGregorianCalendar;

enum EventTheme: string
{
Expand Down Expand Up @@ -64,7 +66,7 @@ public static function current(?string $choice, CarbonInterface $date): ?self
*/
public static function choiceFrom(?string $cookie): string
{
if ($cookie === self::DEFAULT || self::tryFrom((string) $cookie) instanceof self) {
if ($cookie !== null && ($cookie === self::DEFAULT || self::tryFrom($cookie) instanceof self)) {
return $cookie;
}

Expand All @@ -88,6 +90,16 @@ public static function on(CarbonInterface $date): ?self

private static function readerNow(Request $request): CarbonInterface
{
return now(ReaderClock::of($request)->timeZone);
$now = now();
$timeZone = ReaderClock::of($request)->timeZone;

if ($timeZone === null) {
return $now;
}

$calendar = new IntlGregorianCalendar($timeZone);
$calendar->setTime($now->getTimestampMs());

return $now->utcOffset(($calendar->get(IntlCalendar::FIELD_ZONE_OFFSET) + $calendar->get(IntlCalendar::FIELD_DST_OFFSET)) / 60000);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"license": "MIT",
"require": {
"php": "^8.5.0",
"ext-intl": "*",
"lib-icu": ">=57.1",
"firebase/php-jwt": "^7.2.1",
"inertiajs/inertia-laravel": "^3.6.0",
"intervention/image": "^4.4.1",
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions tests/Feature/EventThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
->assertSee('data-event-theme="halloween"', false);
});

it('uses browser timezone aliases for the theme and greeting year', function (string $date, string $timeZone, string $theme, int $year): void {
Date::setTestNow($date);

$this->withUnencryptedCookie('time_zone', $timeZone)
->get(route('home'))
->assertOk()
->assertInertia(fn ($page) => $page
->where('eventTheme', $theme)
->where('eventYear', $year));
})->with([
'an eastern alias ahead of UTC' => ['2026-10-28 20:00:00 UTC', 'Asia/Calcutta', 'halloween', 2026],
'a western alias during daylight saving time' => ['2026-10-29 04:30:00 UTC', 'US/Eastern', 'halloween', 2026],
'a western alias after daylight saving time' => ['2026-11-02 00:30:00 UTC', 'US/Eastern', 'halloween', 2026],
'a western alias still in the previous year' => ['2027-01-01 01:00:00 UTC', 'US/Eastern', 'newyear', 2027],
]);

it('shows the theme the reader picked, whatever the day', function (string $choice): void {
Date::setTestNow('2026-06-15 12:00:00');

Expand Down Expand Up @@ -98,8 +114,8 @@
->assertOk()
->assertInertia(fn ($page) => $page->where('eventYear', $year));
})->with([
'New Year\'s Eve' => ['2026-12-31 23:59:59', 2027],
'New Year\'s Day' => ['2027-01-01 00:00:00', 2027],
"New Year's Eve" => ['2026-12-31 23:59:59', 2027],
"New Year's Day" => ['2027-01-01 00:00:00', 2027],
'the day after' => ['2027-01-02 12:00:00', 2027],
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Enums/EventThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
'the day after Christmas' => ['2026-12-26 23:59:59', EventTheme::Christmas],
'between Christmas and New Year' => ['2026-12-29 23:59:59', null],
'two days before New Year' => ['2026-12-30 00:00:00', EventTheme::NewYear],
'New Year\'s Eve' => ['2026-12-31 12:00:00', EventTheme::NewYear],
'New Year\'s Day' => ['2027-01-01 12:00:00', EventTheme::NewYear],
"New Year's Eve" => ['2026-12-31 12:00:00', EventTheme::NewYear],
"New Year's Day" => ['2027-01-01 12:00:00', EventTheme::NewYear],
'the day after New Year' => ['2027-01-02 23:59:59', EventTheme::NewYear],
'after the New Year window' => ['2027-01-03 00:00:00', null],
'an ordinary day' => ['2026-06-15 12:00:00', null],
Expand Down
Loading