@@ -361,6 +361,7 @@ protected function processBatch(array $servers): void
361361 } catch (Exception $ e ) {
362362 $ serverId = $ this ->getServerIdentifier ($ server );
363363 logs ()->error ("Error updating server status for {$ serverId }: {$ e ->getMessage ()}" );
364+
364365 }
365366 }
366367 }
@@ -566,8 +567,8 @@ protected function getServerIdentifier(Server $server): string
566567
567568 protected function saveStatus (ServerStatus $ status ): void
568569 {
569- $ timezone = new DateTimeZone ( config ( ' app.timezone ' , ' UTC ' ) );
570- $ now = new DateTimeImmutable ('now ' , $ timezone );
570+ $ dbTimezone = $ this -> detectDatabaseTimezone ( );
571+ $ now = new DateTimeImmutable ('now ' , $ dbTimezone );
571572
572573 $ hourStart = $ now ->setTime ((int ) $ now ->format ('H ' ), 0 , 0 );
573574 $ hourEnd = $ hourStart ->modify ('+1 hour ' );
@@ -586,12 +587,7 @@ protected function saveStatus(ServerStatus $status): void
586587 } catch (ConstrainException $ e ) {
587588 $ this ->fixAutoIncrement ();
588589
589- $ existing = $ this ->findServerStatusInHourWindow ($ status ->server ->id , $ hourStart , $ hourEnd )
590- ?? ServerStatus::query ()
591- ->where ('server_id ' , $ status ->server ->id )
592- ->orderBy ('updated_at ' , 'DESC ' )
593- ->limit (1 )
594- ->fetchOne ();
590+ $ existing = $ this ->findServerStatusInHourWindow ($ status ->server ->id , $ hourStart , $ hourEnd );
595591
596592 if ($ existing !== null ) {
597593 $ this ->copyServerStatusSnapshot ($ status , $ existing );
@@ -604,6 +600,37 @@ protected function saveStatus(ServerStatus $status): void
604600 }
605601 }
606602
603+ private function detectDatabaseTimezone (): DateTimeZone
604+ {
605+ static $ tz = null ;
606+
607+ if ($ tz !== null ) {
608+ return $ tz ;
609+ }
610+
611+ try {
612+ $ db = app (\Flute \Core \Database \DatabaseConnection::class)->getDbal ()->database ();
613+ $ driver = $ db ->getDriver ();
614+ $ pdo = method_exists ($ driver , 'getPDO ' ) ? $ driver ->getPDO () : null ;
615+
616+ if ($ pdo ) {
617+ $ result = $ pdo ->query ("SELECT TIMESTAMPDIFF(SECOND, UTC_TIMESTAMP(), NOW()) " )->fetchColumn ();
618+ $ offsetSeconds = (int ) $ result ;
619+ $ sign = $ offsetSeconds >= 0 ? '+ ' : '- ' ;
620+ $ hours = abs (intdiv ($ offsetSeconds , 3600 ));
621+ $ minutes = abs (intdiv ($ offsetSeconds % 3600 , 60 ));
622+ $ tz = new DateTimeZone (sprintf ('%s%02d:%02d ' , $ sign , $ hours , $ minutes ));
623+
624+ return $ tz ;
625+ }
626+ } catch (Throwable ) {
627+ }
628+
629+ $ tz = new DateTimeZone ('UTC ' );
630+
631+ return $ tz ;
632+ }
633+
607634 private function fixAutoIncrement (): void
608635 {
609636 try {
0 commit comments