diff --git a/app/Filament/Admin/Widgets/ServerChartsWidget.php b/app/Filament/Admin/Widgets/ServerChartsWidget.php index dffae96..d40fd81 100644 --- a/app/Filament/Admin/Widgets/ServerChartsWidget.php +++ b/app/Filament/Admin/Widgets/ServerChartsWidget.php @@ -34,7 +34,8 @@ class ServerChartsWidget extends Widget $this->refreshKey++; $data = $this->getData(); $config = $this->rangeConfig($this->range); - $label = now()->format($config['label_format']); + $sysstat = new SysstatMetrics; + $label = now($sysstat->timezoneName())->format($config['label_format']); $this->dispatch('server-charts-updated', [ 'cpu' => $data['cpu']['usage'] ?? 0, diff --git a/app/Services/SysstatMetrics.php b/app/Services/SysstatMetrics.php index ecc9c30..98539b4 100644 --- a/app/Services/SysstatMetrics.php +++ b/app/Services/SysstatMetrics.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Services; use Carbon\CarbonImmutable; +use DateTimeZone; use Symfony\Component\Process\Process; class SysstatMetrics @@ -18,7 +19,8 @@ class SysstatMetrics return []; } - $end = CarbonImmutable::now()->second(0); + $timezone = $this->systemTimezone(); + $end = CarbonImmutable::now($timezone)->second(0); if ($intervalSeconds >= 3600) { $end = $end->minute(0); } @@ -40,7 +42,8 @@ class SysstatMetrics */ public function latest(): ?array { - $end = CarbonImmutable::now(); + $timezone = $this->systemTimezone(); + $end = CarbonImmutable::now($timezone); $start = $end->subMinutes(15); $samples = $this->readSamples($start, $end); @@ -63,6 +66,11 @@ class SysstatMetrics ]; } + public function timezoneName(): string + { + return $this->systemTimezone()->getName(); + } + /** * @return array */ @@ -227,7 +235,7 @@ class SysstatMetrics return null; } - $value = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $date.' '.$time, config('app.timezone')); + $value = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $date.' '.$time, $this->systemTimezone()); if ($value === false) { return null; } @@ -235,6 +243,37 @@ class SysstatMetrics return $value; } + private function systemTimezone(): DateTimeZone + { + static $timezone = null; + + if ($timezone instanceof DateTimeZone) { + return $timezone; + } + + $name = getenv('TZ') ?: null; + if (! $name && is_file('/etc/timezone')) { + $name = trim((string) file_get_contents('/etc/timezone')); + } + if (! $name && is_link('/etc/localtime')) { + $target = readlink('/etc/localtime'); + if (is_string($target) && str_contains($target, '/zoneinfo/')) { + $name = substr($target, strpos($target, '/zoneinfo/') + 10); + } + } + if (! $name) { + $name = config('app.timezone') ?: date_default_timezone_get(); + } + + try { + $timezone = new DateTimeZone($name); + } catch (\Exception $e) { + $timezone = new DateTimeZone('UTC'); + } + + return $timezone; + } + /** * @param array $source */