Make DateTimeFactory expecting a string instead of a DateTimeZone

This commit is contained in:
Johannes Meyer 2014-04-17 16:38:56 +02:00
parent 1db9b1ede4
commit 891d36dbd7
5 changed files with 16 additions and 20 deletions

View File

@ -456,13 +456,8 @@ abstract class ApplicationBootstrap
protected function setupTimezone() protected function setupTimezone()
{ {
$timeZoneString = $this->config->global !== null ? $this->config->global->get('timezone', 'UTC') : 'UTC'; $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); date_default_timezone_set($timeZoneString);
DateTimeFactory::setConfig(array('timezone' => $tz)); DateTimeFactory::setConfig(array('timezone' => $timeZoneString));
return $this; return $this;
} }

View File

@ -337,13 +337,12 @@ class Web extends ApplicationBootstrap
} }
try { try {
$tz = new DateTimeZone($userTimezone); DateTimeFactory::setConfig(array('timezone' => $userTimezone));
} catch (Exception $e) { date_default_timezone_set($userTimezone);
} catch (ConfigurationError $e) {
return parent::setupTimezone(); return parent::setupTimezone();
} }
date_default_timezone_set($userTimezone);
DateTimeFactory::setConfig(array('timezone' => $tz));
return $this; return $this;
} }

View File

@ -22,7 +22,6 @@ namespace {
namespace Icinga\Test { namespace Icinga\Test {
use \Exception; use \Exception;
use \DateTimeZone;
use \RuntimeException; use \RuntimeException;
use \Mockery; use \Mockery;
use \Zend_Config; use \Zend_Config;
@ -113,7 +112,7 @@ namespace Icinga\Test {
public static function setupTimezone() public static function setupTimezone()
{ {
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => 'UTC'));
} }
/** /**

View File

@ -29,8 +29,9 @@
namespace Icinga\Util; namespace Icinga\Util;
use \DateTime; use Exception;
use \DateTimeZone; use DateTime;
use DateTimeZone;
use Icinga\Util\ConfigAwareFactory; use Icinga\Util\ConfigAwareFactory;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
@ -55,10 +56,13 @@ class DateTimeFactory implements ConfigAwareFactory
*/ */
public static function setConfig($config) public static function setConfig($config)
{ {
if (!array_key_exists('timezone', $config)) { try {
throw new ConfigurationError(t('"DateTimeFactory" expects a valid time zone to be set via "setConfig"')); $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;
} }
/** /**

View File

@ -5,7 +5,6 @@
namespace Tests\Icinga\Views\Helper; namespace Tests\Icinga\Views\Helper;
use Mockery; use Mockery;
use DateTimeZone;
use Zend_View_Helper_DateFormat; use Zend_View_Helper_DateFormat;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Application\Config; use Icinga\Application\Config;
@ -26,12 +25,12 @@ class DateFormatTest extends BaseTestCase
{ {
parent::tearDown(); parent::tearDown();
Config::$configDir = $this->oldConfigDir; 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() 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()); $helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
$this->assertEquals( $this->assertEquals(