parent
891d36dbd7
commit
e10193f570
|
@ -42,17 +42,19 @@ class DateTimeFactory implements ConfigAwareFactory
|
|||
{
|
||||
/**
|
||||
* Time zone used throughout DateTime object creation
|
||||
*
|
||||
* @var DateTimeZone
|
||||
*/
|
||||
private static $timeZone;
|
||||
protected static $timeZone;
|
||||
|
||||
/**
|
||||
* Set the factory's config
|
||||
*
|
||||
* Set the factory's time zone via key timezone in the given config array
|
||||
*
|
||||
* @param array $config
|
||||
* @throws \Icinga\Exception\ConfigurationError if the given config is not valid
|
||||
* @param array $config An array with key 'timezone'
|
||||
*
|
||||
* @throws ConfigurationError if the given array misses the key 'timezone'
|
||||
*/
|
||||
public static function setConfig($config)
|
||||
{
|
||||
|
@ -66,14 +68,16 @@ class DateTimeFactory implements ConfigAwareFactory
|
|||
}
|
||||
|
||||
/**
|
||||
* Return new DateTime object using the given format, time and set time zone
|
||||
* Return new DateTime object using the given format, time and set timezone
|
||||
*
|
||||
* Wraps DateTime::createFromFormat()
|
||||
*
|
||||
* @param string $format
|
||||
* @param string $time
|
||||
* @param DateTimeZone $timeZone
|
||||
*
|
||||
* @return DateTime
|
||||
*
|
||||
* @see DateTime::createFromFormat()
|
||||
*/
|
||||
public static function parse($time, $format, DateTimeZone $timeZone = null)
|
||||
|
@ -88,7 +92,9 @@ class DateTimeFactory implements ConfigAwareFactory
|
|||
*
|
||||
* @param string $time
|
||||
* @param DateTimeZone $timeZone
|
||||
*
|
||||
* @return DateTime
|
||||
*
|
||||
* @see DateTime::__construct()
|
||||
*/
|
||||
public static function create($time = 'now', DateTimeZone $timeZone = null)
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Tests\Icinga\Util;
|
||||
|
||||
use DateTimeZone;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
use Icinga\Util\DateTimeFactory;
|
||||
|
||||
class DateTimeFactoryTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ConfigurationError
|
||||
*/
|
||||
public function testWhetherSetConfigThrowsAnExceptionWhenTimezoneMissing()
|
||||
{
|
||||
DateTimeFactory::setConfig(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ConfigurationError
|
||||
*/
|
||||
public function testWhetherSetConfigThrowsAnExceptionWhenTimezoneInvalid()
|
||||
{
|
||||
DateTimeFactory::setConfig(array('timezone' => 'invalid'));
|
||||
}
|
||||
|
||||
public function testWhetherParseWorksWithASpecificTimezone()
|
||||
{
|
||||
$dt = DateTimeFactory::parse('17-04-14 17:00', 'd-m-y H:i', new DateTimeZone('Europe/Berlin'));
|
||||
$dt->setTimezone(new DateTimeZone('UTC'));
|
||||
|
||||
$this->assertEquals(
|
||||
'15',
|
||||
$dt->format('H'),
|
||||
'DateTimeFactory::parse does not properly parse a given datetime or does not respect the given timezone'
|
||||
);
|
||||
}
|
||||
|
||||
public function testWhetherParseWorksWithoutASpecificTimezone()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'15',
|
||||
DateTimeFactory::parse('17-04-14 15:00', 'd-m-y H:i')->format('H'),
|
||||
'DateTimeFactory::parse does not properly parse a given datetime'
|
||||
);
|
||||
}
|
||||
|
||||
public function testWhetherCreateWorksWithASpecificTimezone()
|
||||
{
|
||||
$dt = DateTimeFactory::create('2014-04-17 5PM', new DateTimeZone('Europe/Berlin'));
|
||||
$dt->setTimezone(new DateTimeZone('UTC'));
|
||||
|
||||
$this->assertEquals(
|
||||
'15',
|
||||
$dt->format('H'),
|
||||
'DateTimeFactory::create does not properly parse a given datetime or does not respect the given timezone'
|
||||
);
|
||||
}
|
||||
|
||||
public function testWhetherCreateWorksWithoutASpecificTimezone()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'15',
|
||||
DateTimeFactory::create('2014-04-17 3PM')->format('H'),
|
||||
'DateTimeFactory::create does not properly parse a given datetime'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue