Framework/DateTime: No longer use the Unix timestamp format since this ignores the time zone

refs #4440
This commit is contained in:
Eric Lippmann 2013-08-12 14:18:58 +02:00
parent 9f22905837
commit 2c217d1d06
3 changed files with 22 additions and 33 deletions

View File

@ -5,11 +5,14 @@
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
use \DateTime; use \DateTime;
use Icinga\Application\Icinga; use \Icinga\Application\Icinga;
use Icinga\Application\Config as IcingaConfig; use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Util\DateTimeFactory;
/** /**
* Helper to format date and time * Helper to format date and time. Utilizes DateTimeFactory to ensure time zone awareness
*
* @see \Icinga\Util\DateTimeFactory::create()
*/ */
class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
{ {
@ -49,8 +52,8 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
*/ */
private function format($timestamp, $format) private function format($timestamp, $format)
{ {
// Using the Unix timestamp format to construct a new DateTime $dt = DateTimeFactory::create();
$dt = new DateTime('@' . $timestamp, $this->getTimeZone()); $dt->setTimestamp($timestamp);
return $dt->format($format); return $dt->format($format);
} }
@ -87,16 +90,6 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
return $this->format($timestamp, $this->getDateTimeFormat()); return $this->format($timestamp, $this->getDateTimeFormat());
} }
/**
* Retrieve the user's timezone
*
* @return DateTimeZone
*/
private function getTimeZone()
{
return $this->request->getUser()->getTimeZone();
}
/** /**
* Retrieve the user's date format string * Retrieve the user's date format string
* *

View File

@ -85,20 +85,20 @@ class DateTimePicker extends Zend_Form_Element_Xhtml
} }
if ($this->isUnixTimestamp($value)) { if ($this->isUnixTimestamp($value)) {
// Using the Unix timestamp format to construct a new DateTime $dt = DateTimeFactory::create();
$value = '@' . $value; $dt->setTimestamp($value);
} } else {
try {
try { $dt = DateTimeFactory::create($value);
$dt = DateTimeFactory::create($value); } catch (Exception $e) {
} catch (Exception $e) { $this->addErrorMessage(
$this->addErrorMessage( _(
_( 'Failed to parse datetime string. See '
'Failed to parse datetime string. See ' . 'http://www.php.net/manual/en/datetime.formats.php for valid formats'
. 'http://www.php.net/manual/en/datetime.formats.php for valid formats' )
) );
); return false;
return false; }
} }
$this->setValue($dt->getTimestamp()); $this->setValue($dt->getTimestamp());

View File

@ -64,10 +64,6 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
$dt->isValid('2013-07-12 08:03:43'), $dt->isValid('2013-07-12 08:03:43'),
'Using a valid date/time string must not fail' 'Using a valid date/time string must not fail'
); );
$this->assertTrue(
$dt->isValid('@' . 1373616223),
'Using the Unix timestamp format must not fail'
);
$this->assertTrue( $this->assertTrue(
$dt->isValid(1373616223), $dt->isValid(1373616223),
'Using valid Unix timestamps must not fail' 'Using valid Unix timestamps must not fail'