From 14b6e4056231e12b4826432dfd7d5c60796e08b2 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 16 Apr 2019 15:57:27 +0200 Subject: [PATCH] DbConnection: Set current user's timezone on the connection resolves #3525 --- library/Icinga/Data/Db/DbConnection.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/library/Icinga/Data/Db/DbConnection.php b/library/Icinga/Data/Db/DbConnection.php index e1b59011c..3681d9908 100644 --- a/library/Icinga/Data/Db/DbConnection.php +++ b/library/Icinga/Data/Db/DbConnection.php @@ -3,6 +3,8 @@ namespace Icinga\Data\Db; +use DateTime; +use DateTimeZone; use Exception; use Icinga\Data\Inspectable; use Icinga\Data\Inspection; @@ -205,6 +207,7 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp $driverOptions[PDO::MYSQL_ATTR_INIT_COMMAND] .= ', NAMES ' . $adapterParamaters['charset']; unset($adapterParamaters['charset']); } + $driverOptions[PDO::MYSQL_ATTR_INIT_COMMAND] .= ', time_zone=\'' . $this->getTimezoneOffset() . '\''; $driverOptions[PDO::MYSQL_ATTR_INIT_COMMAND] .=';'; $defaultPort = 3306; break; @@ -287,6 +290,22 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp return $this; } + /** + * Get offset from the current default timezone to GMT + * + * @return string + */ + public function getTimezoneOffset() + { + $tz = new DateTimeZone(date_default_timezone_get()); + $offset = $tz->getOffset(new DateTime()); + $prefix = $offset >= 0 ? '+' : '-'; + $offset = abs($offset); + $hours = (int) floor($offset / 3600); + $minutes = (int) floor(($offset % 3600) / 60); + return sprintf('%s%d:%02d', $prefix, $hours, $minutes); + } + /** * Count all rows of the result set *