Skip to content

Commit 8fc5e2a

Browse files
authored
[Feature] Database has time zone setting (alexjustesen#984)
1 parent b71beb1 commit 8fc5e2a

File tree

11 files changed

+68
-21
lines changed

11 files changed

+68
-21
lines changed

app/Filament/Pages/Settings/GeneralPage.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,21 @@ public function form(Form $form): Form
5050
Forms\Components\TextInput::make('site_name')
5151
->maxLength(50)
5252
->required()
53-
->columnSpan(['md' => 2]),
53+
->columnSpanFull(),
54+
Forms\Components\Toggle::make('public_dashboard_enabled')
55+
->label('Public dashboard'),
56+
])
57+
->compact()
58+
->columns([
59+
'default' => 1,
60+
'md' => 2,
61+
]),
62+
63+
Forms\Components\Section::make('Time Zone Settings')
64+
->schema([
5465
Forms\Components\Select::make('timezone')
5566
->label('Time zone')
67+
->hint(new HtmlString('&#x1f517;<a href="https://docs.speedtest-tracker.dev/" target="_blank" rel="nofollow">Docs</a>'))
5668
->options(TimeZoneHelper::list())
5769
->searchable()
5870
->required(),
@@ -61,6 +73,9 @@ public function form(Form $form): Form
6173
->placeholder('M j, Y G:i:s')
6274
->maxLength(25)
6375
->required(),
76+
Forms\Components\Toggle::make('db_has_timezone')
77+
->label('Database has time zone')
78+
->helperText(new HtmlString('Enable if your database <strong>has</strong> a time zone already set.')),
6479
])
6580
->compact()
6681
->columns([
@@ -113,18 +128,6 @@ public function form(Form $form): Form
113128
'default' => 1,
114129
'md' => 2,
115130
]),
116-
117-
Forms\Components\Section::make('Public Dashboard Settings')
118-
->schema([
119-
Forms\Components\Toggle::make('public_dashboard_enabled')
120-
->label('Enable')
121-
->columnSpan(2),
122-
])
123-
->compact()
124-
->columns([
125-
'default' => 1,
126-
'md' => 2,
127-
]),
128131
])
129132
->columnSpan('full'),
130133
]);

app/Filament/Resources/ResultResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Exports\ResultsSelectedBulkExport;
66
use App\Filament\Resources\ResultResource\Pages;
7+
use App\Helpers\TimeZoneHelper;
78
use App\Models\Result;
89
use App\Settings\GeneralSettings;
910
use Carbon\Carbon;
@@ -142,7 +143,7 @@ public static function table(Table $table): Table
142143
TextColumn::make('created_at')
143144
->label('Created')
144145
->dateTime($settings->time_format ?? 'M j, Y G:i:s')
145-
->timezone($settings->timezone ?? 'UTC')
146+
->timezone(TimeZoneHelper::displayTimeZone($settings))
146147
->sortable(),
147148
])
148149
->filters([

app/Filament/Widgets/RecentJitterChartWidget.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Widgets;
44

5+
use App\Helpers\TimeZoneHelper;
56
use App\Models\Result;
67
use App\Settings\GeneralSettings;
78
use Filament\Widgets\ChartWidget;
@@ -77,7 +78,7 @@ protected function getData(): array
7778
'tension' => 0.4,
7879
],
7980
],
80-
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
81+
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
8182
];
8283
}
8384

app/Filament/Widgets/RecentPingChartWidget.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Widgets;
44

5+
use App\Helpers\TimeZoneHelper;
56
use App\Models\Result;
67
use App\Settings\GeneralSettings;
78
use Filament\Widgets\ChartWidget;
@@ -59,7 +60,7 @@ protected function getData(): array
5960
'tension' => 0.4,
6061
],
6162
],
62-
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
63+
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
6364
];
6465
}
6566

app/Filament/Widgets/RecentSpeedChartWidget.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Widgets;
44

5+
use App\Helpers\TimeZoneHelper;
56
use App\Models\Result;
67
use App\Settings\GeneralSettings;
78
use Filament\Widgets\ChartWidget;
@@ -68,7 +69,7 @@ protected function getData(): array
6869
'tension' => 0.4,
6970
],
7071
],
71-
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
72+
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
7273
];
7374
}
7475

app/Helpers/TimeZoneHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22

33
namespace App\Helpers;
44

5+
use App\Settings\GeneralSettings;
56
use DateTimeZone;
67
use Illuminate\Support\Facades\Cache;
78
use Illuminate\Support\Str;
89

910
class TimeZoneHelper
1011
{
12+
/**
13+
* Returns the display timezone
14+
*/
15+
public static function displayTimeZone(GeneralSettings $settings): string
16+
{
17+
// Don't translate to local time if the database already returns it.
18+
if ($settings->db_has_timezone) {
19+
return 'UTC';
20+
}
21+
22+
return $settings->timezone
23+
?? 'UTC';
24+
}
25+
1126
/**
1227
* Returns a collection of time zones with their offset from UTC.
1328
*/

app/Http/Controllers/HomeController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ public function __invoke(Request $request)
2828
return view('get-started');
2929
}
3030

31+
/**
32+
* This jank needs to happen because some people like
33+
* to watch the world burn by setting a time zone
34+
* in their database instances.
35+
*/
36+
if ($settings->db_has_timezone) {
37+
date_default_timezone_set($settings->timezone ?? 'UTC');
38+
}
39+
40+
$diff = $latestResult->created_at->diffForHumans();
41+
3142
return view('dashboard', [
43+
'diff' => $diff,
3244
'latestResult' => $latestResult,
3345
]);
3446
}

app/Settings/GeneralSettings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class GeneralSettings extends Settings
1919

2020
public string $timezone;
2121

22+
public bool $db_has_timezone;
23+
2224
public bool $public_dashboard_enabled;
2325

2426
public static function group(): string
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Spatie\LaravelSettings\Migrations\SettingsMigration;
4+
5+
return new class extends SettingsMigration
6+
{
7+
public function up(): void
8+
{
9+
$this->migrator->add('general.db_has_timezone', false);
10+
}
11+
};

resources/views/dashboard.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
@livewire(\App\Filament\Widgets\StatsOverviewWidget::class)
55
</div>
66

7-
@if ($latestResult)
7+
@isset($latestResult)
88
<div class="text-sm font-semibold leading-6 text-center col-span-full sm:text-base">
9-
Latest result: {{ $latestResult?->created_at->diffForHumans() }}
9+
Latest result: <time datetime="{{ $latestResult->created_at }}">{{ $diff }}</time>
1010
</div>
11-
@endif
11+
@endisset
1212

1313
<div class="col-span-full">
1414
@livewire(\App\Filament\Widgets\RecentSpeedChartWidget::class)

0 commit comments

Comments
 (0)