From 120b24620ba81de27878e0b18ca09b626dcf3593 Mon Sep 17 00:00:00 2001 From: Joshua Date: Wed, 27 Jul 2022 09:08:29 +1000 Subject: [PATCH] Beta: Fix Timezone issues --- Dockerfile | 1 + config/version.conf | 3 +-- front/php/server/db.php | 9 ++++++++- front/php/server/devices.php | 4 +++- front/php/server/events.php | 2 +- front/php/server/util.php | 7 +++++++ 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index cd17f6c6..fc8df2a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,7 @@ COPY . /home/pi/pialert # Pi.Alert RUN python /home/pi/pialert/back/pialert.py update_vendors \ && sed -ie 's/= 80/= '${PORT}'/g' /etc/lighttpd/lighttpd.conf \ + && sed -i.bak 's/^\(TIMEZONE=\).*/\'${TZ}'/' /home/pi/pialert/config/pialert.conf \ && (crontab -l 2>/dev/null; cat /home/pi/pialert/install/pialert.cron) | crontab - # it's easy for permissions set in Git to be overridden, so doing it manually diff --git a/config/version.conf b/config/version.conf index 7110989f..5dc33c9d 100644 --- a/config/version.conf +++ b/config/version.conf @@ -1,4 +1,3 @@ VERSION = '3.5_leiweibau' VERSION_YEAR = '2022' -VERSION_DATE = '2022-07-07' -TZ = 'Europe/London' \ No newline at end of file +VERSION_DATE = '2022-07-07' \ No newline at end of file diff --git a/front/php/server/db.php b/front/php/server/db.php index c54f5e79..4bc80da8 100644 --- a/front/php/server/db.php +++ b/front/php/server/db.php @@ -8,6 +8,13 @@ // Puche 2021 pi.alert.application@gmail.com GNU GPLv3 //------------------------------------------------------------------------------ +// ## TimeZone processing +$config_file = "../config/pialert.conf"; +$config_file_lines = file($config_file); +$config_file_lines_timezone = array_values(preg_grep('/^TIMEZONE\s.*/', $config_file_lines)); +$timezone_line = explode("'", $config_file_lines_timezone[0]); +$Pia_TimeZone = $timezone_line[1]; +date_default_timezone_set($Pia_TimeZone); //------------------------------------------------------------------------------ // DB File Path @@ -58,4 +65,4 @@ function OpenDB () { } } -?> +?> \ No newline at end of file diff --git a/front/php/server/devices.php b/front/php/server/devices.php index 2be29ee7..5bd7073f 100644 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -116,8 +116,10 @@ function getDeviceData() { $row = $result -> fetchArray (SQLITE3_NUM); $deviceData['dev_DownAlerts'] = $row[0]; + // Get current date using php, sql datetime does not return time respective to timezone. + $currentdate = date("Y-m-d H:i:s"); // Presence hours - $sql = 'SELECT CAST(( MAX (0, SUM (julianday (IFNULL (ses_DateTimeDisconnection, DATETIME("now","localtime"))) + $sql = 'SELECT CAST(( MAX (0, SUM (julianday (IFNULL (ses_DateTimeDisconnection,"'. $currentdate .'" )) - julianday (CASE WHEN ses_DateTimeConnection < '. $periodDate .' THEN '. $periodDate .' ELSE ses_DateTimeConnection END)) *24 )) AS INT) FROM Sessions diff --git a/front/php/server/events.php b/front/php/server/events.php index 1e927ecc..df7a4b08 100644 --- a/front/php/server/events.php +++ b/front/php/server/events.php @@ -275,7 +275,7 @@ function getDevicePresence() { END AS ses_DateTimeConnectionCorrected, CASE - WHEN ses_EventTypeDisconnection = "" THEN + WHEN ses_EventTypeDisconnection = "" OR ses_EventTypeDisconnection = NULL THEN (SELECT MIN(ses_DateTimeConnection) FROM Sessions AS SES2 WHERE SES2.ses_MAC = SES1.ses_MAC AND SES2.ses_DateTimeConnection > SES1.ses_DateTimeConnection) ELSE ses_DateTimeDisconnection END AS ses_DateTimeDisconnectionCorrected diff --git a/front/php/server/util.php b/front/php/server/util.php index a4f345f3..ee4f8370 100644 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -8,6 +8,13 @@ // Puche 2021 pi.alert.application@gmail.com GNU GPLv3 //------------------------------------------------------------------------------ +// ## TimeZone processing +$config_file = "../config/pialert.conf"; +$config_file_lines = file($config_file); +$config_file_lines_timezone = array_values(preg_grep('/^TIMEZONE\s.*/', $config_file_lines)); +$timezone_line = explode("'", $config_file_lines_timezone[0]); +$Pia_TimeZone = $timezone_line[1]; +date_default_timezone_set($Pia_TimeZone); //------------------------------------------------------------------------------ // Formatting data functions