icingaweb2/application/views/helpers/DateFormat.php

129 lines
2.9 KiB
PHP
Raw Normal View History

<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
use \DateTime;
use \Icinga\Application\Icinga;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Util\DateTimeFactory;
/**
* 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
{
/**
* Current request
* @var Zend_Controller_Request_Abstract
*/
private $request;
/**
* Constructor
*
* Retrieve request
*/
public function __construct()
{
$this->request = Icinga::app()->getFrontController()->getRequest();
}
/**
* Helper entry point
*
* @return self
*/
public function dateFormat()
{
return $this;
}
/**
* Return date formatted according to given format respecting the user's timezone
*
* @param int $timestamp
* @param string $format
* @return string
*/
public function format($timestamp, $format)
{
$dt = DateTimeFactory::create();
$dt->setTimestamp($timestamp);
return $dt->format($format);
}
/**
* Format date according to user's format
*
* @param int $timestamp A unix timestamp
* @return string The formatted date string
*/
public function formatDate($timestamp)
{
return $this->format($timestamp, $this->getDateFormat());
}
/**
* Format time according to user's format
*
* @param int $timestamp A unix timestamp
* @return string The formatted time string
*/
public function formatTime($timestamp)
{
return $this->format($timestamp, $this->getTimeFormat());
}
/**
* Format datetime according to user's format
*
* @param int $timestamp A unix timestamp
* @return string The formatted datetime string
*/
public function formatDateTime($timestamp)
{
return $this->format($timestamp, $this->getDateTimeFormat());
}
/**
* Retrieve the user's date format string
*
* @return string
*/
public function getDateFormat()
{
return $this->request->getUser()->getPreferences()->get(
'dateFormat', IcingaConfig::app()->global->get('dateFormat', 'Y-m-d')
);
}
/**
* Retrieve the user's time format string
*
* @return string
*/
public function getTimeFormat()
{
return $this->request->getUser()->getPreferences()->get(
'timeFormat', IcingaConfig::app()->global->get('timeFormat', 'H:i:s')
);
}
/**
* Retrieve the user's datetime format string
*
* @return string
*/
public function getDateTimeFormat()
{
return $this->getDateFormat() . ' ' . $this->getTimeFormat();
}
}
// @codingStandardsIgnoreStop