DbConnection: Set current user's timezone on the connection

resolves #3525
This commit is contained in:
Johannes Meyer 2019-04-16 15:57:27 +02:00
parent ed23d12c4f
commit 14b6e40562
1 changed files with 19 additions and 0 deletions

View File

@ -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
*