diff --git a/library/Icinga/Application/ApplicationBootstrap.php b/library/Icinga/Application/ApplicationBootstrap.php index 342ffd88d..df8d6a0e1 100644 --- a/library/Icinga/Application/ApplicationBootstrap.php +++ b/library/Icinga/Application/ApplicationBootstrap.php @@ -456,13 +456,8 @@ abstract class ApplicationBootstrap protected function setupTimezone() { $timeZoneString = $this->config->global !== null ? $this->config->global->get('timezone', 'UTC') : 'UTC'; - try { - $tz = new DateTimeZone($timeZoneString); - } catch (Exception $e) { - throw new ConfigurationError(t('Invalid timezone') . ' "' . $timeZoneString . '"'); - } date_default_timezone_set($timeZoneString); - DateTimeFactory::setConfig(array('timezone' => $tz)); + DateTimeFactory::setConfig(array('timezone' => $timeZoneString)); return $this; } diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php index 9b6c6463b..6ce858d10 100644 --- a/library/Icinga/Application/Web.php +++ b/library/Icinga/Application/Web.php @@ -337,13 +337,12 @@ class Web extends ApplicationBootstrap } try { - $tz = new DateTimeZone($userTimezone); - } catch (Exception $e) { + DateTimeFactory::setConfig(array('timezone' => $userTimezone)); + date_default_timezone_set($userTimezone); + } catch (ConfigurationError $e) { return parent::setupTimezone(); } - date_default_timezone_set($userTimezone); - DateTimeFactory::setConfig(array('timezone' => $tz)); return $this; } diff --git a/library/Icinga/Test/BaseTestCase.php b/library/Icinga/Test/BaseTestCase.php index 847559739..e426dce9c 100644 --- a/library/Icinga/Test/BaseTestCase.php +++ b/library/Icinga/Test/BaseTestCase.php @@ -22,7 +22,6 @@ namespace { namespace Icinga\Test { use \Exception; - use \DateTimeZone; use \RuntimeException; use \Mockery; use \Zend_Config; @@ -113,7 +112,7 @@ namespace Icinga\Test { public static function setupTimezone() { date_default_timezone_set('UTC'); - DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); + DateTimeFactory::setConfig(array('timezone' => 'UTC')); } /** diff --git a/library/Icinga/Util/DateTimeFactory.php b/library/Icinga/Util/DateTimeFactory.php index 9dd854aa7..413af958c 100644 --- a/library/Icinga/Util/DateTimeFactory.php +++ b/library/Icinga/Util/DateTimeFactory.php @@ -29,8 +29,9 @@ namespace Icinga\Util; -use \DateTime; -use \DateTimeZone; +use Exception; +use DateTime; +use DateTimeZone; use Icinga\Util\ConfigAwareFactory; use Icinga\Exception\ConfigurationError; @@ -55,10 +56,13 @@ class DateTimeFactory implements ConfigAwareFactory */ public static function setConfig($config) { - if (!array_key_exists('timezone', $config)) { - throw new ConfigurationError(t('"DateTimeFactory" expects a valid time zone to be set via "setConfig"')); + try { + $tz = new DateTimeZone(isset($config['timezone']) ? $config['timezone'] : ''); + } catch (Exception $e) { + throw new ConfigurationError('"DateTimeFactory" expects a valid time zone be set via "setConfig"'); } - self::$timeZone = $config['timezone']; + + self::$timeZone = $tz; } /** diff --git a/test/php/application/views/helpers/DateFormatTest.php b/test/php/application/views/helpers/DateFormatTest.php index 04e5e6a90..8de8442f6 100644 --- a/test/php/application/views/helpers/DateFormatTest.php +++ b/test/php/application/views/helpers/DateFormatTest.php @@ -5,7 +5,6 @@ namespace Tests\Icinga\Views\Helper; use Mockery; -use DateTimeZone; use Zend_View_Helper_DateFormat; use Icinga\Test\BaseTestCase; use Icinga\Application\Config; @@ -26,12 +25,12 @@ class DateFormatTest extends BaseTestCase { parent::tearDown(); Config::$configDir = $this->oldConfigDir; - DateTimeFactory::setConfig(array('timezone' => new DateTimeZone(date_default_timezone_get()))); + DateTimeFactory::setConfig(array('timezone' => date_default_timezone_get())); } public function testFormatReturnsCorrectDateWithTimezoneApplied() { - DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('Europe/Berlin'))); + DateTimeFactory::setConfig(array('timezone' => 'Europe/Berlin')); $helper = new Zend_View_Helper_DateFormat($this->getRequestMock()); $this->assertEquals(