Skip to content

Commit

Permalink
Make sure UTC is always used internally (#713)
Browse files Browse the repository at this point in the history
* force app timezone to be UTC

* remove asDateTime overwrite

* add custom column to display dates in user timezone

* use `APP_TIMEZONE` as default timezone for new users

* revert accidental pinting
  • Loading branch information
Boy132 authored Nov 15, 2024
1 parent 408897c commit 54039e2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 44 deletions.
7 changes: 3 additions & 4 deletions app/Filament/Resources/ApiKeyResource/Pages/ListApiKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Filament\Resources\ApiKeyResource;
use App\Models\ApiKey;
use App\Tables\Columns\DateTimeColumn;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Actions\CreateAction;
Expand Down Expand Up @@ -35,15 +36,13 @@ public function table(Table $table): Table
->hidden()
->searchable(),

TextColumn::make('last_used_at')
DateTimeColumn::make('last_used_at')
->label('Last Used')
->placeholder('Not Used')
->dateTime()
->sortable(),

TextColumn::make('created_at')
DateTimeColumn::make('created_at')
->label('Created')
->dateTime()
->sortable(),

TextColumn::make('user.username')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Database;
use App\Services\Databases\DatabasePasswordService;
use App\Tables\Columns\DateTimeColumn;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function table(Table $table): Table
->icon('tabler-brand-docker')
->url(fn (Database $database) => route('filament.admin.resources.servers.edit', ['record' => $database->server_id])),
TextColumn::make('max_connections'),
TextColumn::make('created_at')->dateTime(),
DateTimeColumn::make('created_at'),
])
->actions([
DeleteAction::make(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Resources\DatabaseResource\Pages;

use App\Filament\Resources\DatabaseResource;
use App\Tables\Columns\DateTimeColumn;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Actions\BulkActionGroup;
Expand Down Expand Up @@ -34,12 +35,10 @@ public function table(Table $table): Table
TextColumn::make('max_connections')
->numeric()
->sortable(),
TextColumn::make('created_at')
->dateTime()
DateTimeColumn::make('created_at')
->sortable()
->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('updated_at')
->dateTime()
DateTimeColumn::make('updated_at')
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
Expand Down
35 changes: 0 additions & 35 deletions app/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace App\Models;

use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Container\Container;
Expand All @@ -16,7 +12,6 @@
use Illuminate\Database\Eloquent\Model as IlluminateModel;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Validation\Validator;
use InvalidArgumentException;

abstract class Model extends IlluminateModel
{
Expand Down Expand Up @@ -69,36 +64,6 @@ public function getRouteKeyName(): string
return 'uuid';
}

protected function asDateTime($value): Carbon
{
$timezone = auth()->user()?->timezone ?? config('app.timezone', 'UTC');

if ($value instanceof CarbonInterface) {
return Date::instance($value->timezone($timezone));
}

if ($value instanceof DateTimeInterface) {
return Date::parse($value->format('Y-m-d H:i:s.u'), $timezone);
}

if (is_numeric($value)) {
return Date::createFromTimestamp($value, $timezone);
}

if ($this->isStandardDateFormat($value)) {
return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->timezone($timezone)->startOfDay());
}

$format = $this->getDateFormat();
try {
$date = Date::createFromFormat($format, $value)->timezone($timezone);
} catch (InvalidArgumentException) {
$date = false;
}

return $date ?: Date::parse($value);
}

/**
* Returns the validator instance used by this model.
*/
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ protected static function booted(): void
static::creating(function (self $user) {
$user->uuid = Str::uuid()->toString();

$user->timezone = env('APP_TIMEZONE', 'UTC');

return true;
});

Expand Down
15 changes: 15 additions & 0 deletions app/Tables/Columns/DateTimeColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Tables\Columns;

use Filament\Tables\Columns\TextColumn;

class DateTimeColumn extends TextColumn
{
protected function setUp(): void
{
parent::setUp();

$this->dateTime(timezone: auth()->user()?->timezone ?? config('app.timezone', 'UTC'));
}
}
2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

'version' => 'canary',

'timezone' => 'UTC',

'exceptions' => [
'report_all' => env('APP_REPORT_ALL_EXCEPTIONS', false),
],
Expand Down

0 comments on commit 54039e2

Please sign in to comment.