Framework: Use the DateTime Unix timestamp format in the DateFormat helper

refs #4440
This commit is contained in:
Eric Lippmann 2013-08-07 17:13:47 +02:00
parent 3122a75e73
commit a82d750d6a

View File

@ -5,7 +5,6 @@
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
use \DateTime; use \DateTime;
use \DateTimeZone;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Config as IcingaConfig; use Icinga\Application\Config as IcingaConfig;
@ -24,7 +23,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
/** /**
* Constructor * Constructor
* *
* Retrieve current request * Retrieve request
*/ */
public function __construct() public function __construct()
{ {
@ -42,53 +41,64 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
} }
/** /**
* Format date according to current user's format * Return date formatted according to given format respecting the user's timezone
*
* @param int $timestamp
* @param string $format
* @return string
*/
private function format($timestamp, $format)
{
// Using the Unix timestamp format to construct a new DateTime
$dt = new DateTime('@' . $timestamp, $this->getTimeZone());
return $dt->format($format);
}
/**
* Format date according to user's format
* *
* @param int $timestamp A unix timestamp * @param int $timestamp A unix timestamp
* @return string The formatted date string * @return string The formatted date string
*/ */
public function formatDate($timestamp) public function formatDate($timestamp)
{ {
$dt = new DateTime($timestamp, $this->getTimeZone()); return $this->format($timestamp, $this->getDateFormat());
return $dt->format($this->getDateFormat());
} }
/** /**
* Format time according to current user's format * Format time according to user's format
* *
* @param int $timestamp A unix timestamp * @param int $timestamp A unix timestamp
* @return string The formatted time string * @return string The formatted time string
*/ */
public function formatTime($timestamp) public function formatTime($timestamp)
{ {
$dt = new DateTime($timestamp, $this->getTimeZone()); return $this->format($timestamp, $this->getTimeFormat());
return $dt->format($this->getTimeFormat());
} }
/** /**
* Format datetime according to current user's format * Format datetime according to user's format
* *
* @param int $timestamp A unix timestamp * @param int $timestamp A unix timestamp
* @return string The formatted datetime string * @return string The formatted datetime string
*/ */
public function formatDateTime($timestamp) public function formatDateTime($timestamp)
{ {
$dt = new DateTime($timestamp, $this->getTimeZone()); return $this->format($timestamp, $this->getDateTimeFormat());
return $dt->format($this->getDateTimeFormat());
} }
/** /**
* Retrieve the current user's timezone * Retrieve the user's timezone
* *
* @return DateTimeZone * @return DateTimeZone
*/ */
private function getTimeZone() private function getTimeZone()
{ {
return new DateTimeZone($this->request->getUser()->getTimeZone()); return $this->request->getUser()->getTimeZone();
} }
/** /**
* Retrieve the current user's date format string * Retrieve the user's date format string
* *
* @return string * @return string
*/ */
@ -100,7 +110,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
} }
/** /**
* Retrieve the current user's time format string * Retrieve the user's time format string
* *
* @return string * @return string
*/ */
@ -112,7 +122,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
} }
/** /**
* Retrieve the current user's datetime format string * Retrieve the user's datetime format string
* *
* @return string * @return string
*/ */