Set the DB timezone on each connection to match the APP_TIMEZONE value

This commit is contained in:
Dane Everitt 2020-10-25 15:07:11 -07:00
parent 8c6327fd32
commit 996fb5b46f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 29 additions and 0 deletions

25
app/Helpers/Time.php Normal file
View file

@ -0,0 +1,25 @@
<?php
namespace Pterodactyl\Helpers;
use Carbon\CarbonImmutable;
final class Time
{
/**
* Gets the time offset from the provided timezone relative to UTC as a number. This
* is used in the database configuration since we can't always rely on there being support
* for named timezones in MySQL.
*
* Returns the timezone as a string like +08:00 or -05:00 depending on the app timezone.
*
* @param string $timezone
* @return string
*/
public static function getMySQLTimezoneOffset(string $timezone): string
{
$offset = round(CarbonImmutable::now($timezone)->getTimezone()->getOffset(CarbonImmutable::now('UTC')) / 3600);
return sprintf('%s%s:00', $offset > 0 ? '+' : '-', str_pad(abs($offset), 2, '0', STR_PAD_LEFT));
}
}

View file

@ -1,5 +1,7 @@
<?php
use Pterodactyl\Helpers\Time;
return [
/*
|--------------------------------------------------------------------------
@ -43,6 +45,7 @@ return [
'collation' => 'utf8mb4_unicode_ci',
'prefix' => env('DB_PREFIX', ''),
'strict' => env('DB_STRICT_MODE', false),
'timezone' => env('DB_TIMEZONE', Time::getMySQLTimezoneOffset(env('APP_TIMEZONE')))
],
/*
@ -65,6 +68,7 @@ return [
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'timezone' => env('DB_TIMEZONE', Time::getMySQLTimezoneOffset(env('APP_TIMEZONE')))
],
],