From 8074d501d95bd622bdafc4e3c45e0b920ec17550 Mon Sep 17 00:00:00 2001 From: William Rose Date: Sun, 29 Jun 2025 22:32:52 -0700 Subject: [PATCH] Try to use browser timezone --- app/Http/Controllers/Posts/PostController.php | 3 ++ app/Livewire/Comments/CommentComponent.php | 2 + .../Localization/SwitchTimezoneComponent.php | 19 +++++++++ app/Livewire/Posts/PostIndexComponent.php | 5 ++- app/Traits/SessionTimezoneTrait.php | 39 +++++++++++++++++++ .../Dates/FormattedDateTimeComponent.php | 6 ++- .../partials/comment-timestamp.blade.php | 4 +- resources/views/layouts/app.blade.php | 2 + .../switch-timezone-component.blade.php | 10 +++++ resources/views/posts/show.blade.php | 4 +- 10 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 app/Livewire/Localization/SwitchTimezoneComponent.php create mode 100644 app/Traits/SessionTimezoneTrait.php create mode 100644 resources/views/livewire/localization/switch-timezone-component.blade.php diff --git a/app/Http/Controllers/Posts/PostController.php b/app/Http/Controllers/Posts/PostController.php index 9d72798d..6ec3c7d0 100644 --- a/app/Http/Controllers/Posts/PostController.php +++ b/app/Http/Controllers/Posts/PostController.php @@ -14,12 +14,14 @@ use App\Services\LdJsonService; use App\Services\PostService; use App\Traits\PostTrait; +use App\Traits\SessionTimezoneTrait; use App\Traits\SubsiteTrait; use Illuminate\Contracts\View\View; final class PostController extends BaseController { use PostTrait; + use SessionTimezoneTrait; use SubsiteTrait; protected int $subsiteId; @@ -55,6 +57,7 @@ public function show(Post $post): View 'next' => $post->next(), 'previous' => $post->previous(), 'canonicalUrl' => $this->getCanonicalUrl($post), + 'displayTimezone' => $this->getDisplayTimezone(), 'relatedPosts' => $relatedPosts, 'subdomain' => $subdomain, 'useLivewire' => true, diff --git a/app/Livewire/Comments/CommentComponent.php b/app/Livewire/Comments/CommentComponent.php index cf65ed09..0022fef1 100644 --- a/app/Livewire/Comments/CommentComponent.php +++ b/app/Livewire/Comments/CommentComponent.php @@ -10,6 +10,7 @@ use App\Models\Post; use App\Models\User; use App\Traits\CommentComponentTrait; +use App\Traits\SessionTimezoneTrait; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\DB; use Livewire\Attributes\On; @@ -18,6 +19,7 @@ final class CommentComponent extends Component { use CommentComponentTrait; + use SessionTimezoneTrait; // Data public ?int $authorizedUserId; diff --git a/app/Livewire/Localization/SwitchTimezoneComponent.php b/app/Livewire/Localization/SwitchTimezoneComponent.php new file mode 100644 index 00000000..8cb529b4 --- /dev/null +++ b/app/Livewire/Localization/SwitchTimezoneComponent.php @@ -0,0 +1,19 @@ + $posts, + 'displayTimezone' => $this->getDisplayTimezone(), ]); } @@ -72,7 +75,7 @@ public function query(): Builder private function getPosts(): CursorPaginator { $dateQueries = [ - DB::raw('DATE_FORMAT(posts.created_at, "%m-%d") as month_day'), + DB::raw("DATE_FORMAT(CONVERT_TZ(posts.created_at, 'UTC', " . DB::connection()->getPdo()->quote($this->getDisplayTimezone()) . "), '%m-%d') as month_day"), DB::raw('COUNT(*) as total_posts'), ]; diff --git a/app/Traits/SessionTimezoneTrait.php b/app/Traits/SessionTimezoneTrait.php new file mode 100644 index 00000000..c4e53843 --- /dev/null +++ b/app/Traits/SessionTimezoneTrait.php @@ -0,0 +1,39 @@ +displayTimezone is null. + return $this->displayTimezone ?? session('displayTimezone') ?? config('app.timezone'); + } + + public function setDisplayTimezone(string $timezone): void + { + try { + // Try to create a CarbonTimezone object to validate the timezone string. + CarbonTimezone::create($timezone); + + // If we get here, the timezone is valid, so we can set it. + $this->displayTimezone = $timezone; + } catch (InvalidTimeZoneException $e) { + $this->logError('Invalid timezone: ' . $timezone); + } + } +} diff --git a/app/View/Components/Dates/FormattedDateTimeComponent.php b/app/View/Components/Dates/FormattedDateTimeComponent.php index e165f673..d9d27788 100644 --- a/app/View/Components/Dates/FormattedDateTimeComponent.php +++ b/app/View/Components/Dates/FormattedDateTimeComponent.php @@ -4,12 +4,15 @@ namespace App\View\Components\Dates; +use App\Traits\SessionTimezoneTrait; use Carbon\Carbon; use Illuminate\Contracts\View\View; use Illuminate\View\Component; final class FormattedDateTimeComponent extends Component { + use SessionTimezoneTrait; + public Carbon $date; public string $format = 'Y-m-d H:i:s'; @@ -36,7 +39,8 @@ public function render(): View private function getFormattedDate(): string { - $formattedDate = $this->date->format($this->format()); + $date = $this->date->tz($this->getDisplayTimezone()); + $formattedDate = $date->format($this->format()); return $this->addPeriods($formattedDate); } diff --git a/resources/views/comments/partials/comment-timestamp.blade.php b/resources/views/comments/partials/comment-timestamp.blade.php index a6f24c2c..7883a5c9 100644 --- a/resources/views/comments/partials/comment-timestamp.blade.php +++ b/resources/views/comments/partials/comment-timestamp.blade.php @@ -1,11 +1,11 @@ diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 40cc2ef0..4d7404b4 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -21,6 +21,8 @@ @endif + + diff --git a/resources/views/livewire/localization/switch-timezone-component.blade.php b/resources/views/livewire/localization/switch-timezone-component.blade.php new file mode 100644 index 00000000..0e3e7620 --- /dev/null +++ b/resources/views/livewire/localization/switch-timezone-component.blade.php @@ -0,0 +1,10 @@ +
+ +@script + +@endscript diff --git a/resources/views/posts/show.blade.php b/resources/views/posts/show.blade.php index 9ba8b775..7a767e42 100644 --- a/resources/views/posts/show.blade.php +++ b/resources/views/posts/show.blade.php @@ -18,10 +18,10 @@