From 80cf39987a99b4b19acb6291c79853efb5801a6c Mon Sep 17 00:00:00 2001 From: Alan Xiong Date: Mon, 15 Apr 2024 05:15:19 +0800 Subject: [PATCH] Ensure that TZ setting can work properly in Docker. --- Dockerfile | 4 ++++ results/telemetry_db.php | 25 ++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 212779f..84d4061 100755 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,10 @@ RUN apt-get update && apt-get install -y \ && docker-php-ext-install -j$(nproc) gd pdo pdo_mysql pdo_pgsql pgsql \ && rm -rf /var/lib/apt/lists/* +# Set the timezone +ENV TZ=Asia/Shanghai +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + # Prepare files and folders RUN mkdir -p /speedtest/ diff --git a/results/telemetry_db.php b/results/telemetry_db.php index 46b8bf7..5de7a91 100755 --- a/results/telemetry_db.php +++ b/results/telemetry_db.php @@ -3,7 +3,15 @@ require_once 'idObfuscation.php'; define('TELEMETRY_SETTINGS_FILE', 'telemetry_settings.php'); - +$tz = getenv('TZ'); +if ($tz !== false) { + // If the environment variable TZ exists, set the default timezone for PHP to that value + date_default_timezone_set($tz); +} else { + // If the environment variable TZ does not exist, set a default timezone or handle the error + // Set it to Asia/Shanghai + date_default_timezone_set('Asia/Shanghai'); +} /** * @return PDO|false */ @@ -184,19 +192,22 @@ function insertSpeedtestUser($ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, } try { + $currentTimestamp = new DateTime('now'); + $formattedTimestamp = $currentTimestamp->format('Y-m-d H:i:s'); + $stmt = $pdo->prepare( 'INSERT INTO speedtest_users - (ip,ispinfo,extra,ua,lang,dl,ul,ping,jitter,log) - VALUES (?,?,?,?,?,?,?,?,?,?)' + (ip,ispinfo,extra,ua,lang,dl,ul,ping,jitter,log,timestamp) + VALUES (?,?,?,?,?,?,?,?,?,?,?)' ); $stmt->execute([ - $ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, $jitter, $log + $ip, $ispinfo, $extra, $ua, $lang, $dl, $ul, $ping, $jitter, $log, $formattedTimestamp // 在execute的参数中添加$formattedTimestamp ]); $id = $pdo->lastInsertId(); } catch (Exception $e) { - if($returnExceptionOnError){ - return $e; - } + if($returnExceptionOnError){ + return $e; + } return false; }