Respect a user's timezone preferences

refs #138
This commit is contained in:
Alexander A. Klimov 2018-02-26 15:33:12 +01:00
parent d10e275508
commit 7997be46ae
3 changed files with 4 additions and 81 deletions

View file

@ -2,13 +2,11 @@
namespace Icinga\Module\Graphite\Controllers;
use DateTimeZone;
use Icinga\Exception\Http\HttpBadRequestException;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Module\Graphite\Graphing\GraphingTrait;
use Icinga\Module\Graphite\Web\Controller\MonitoringAwareController;
use Icinga\Module\Graphite\Web\Widget\Graphs;
use Icinga\Util\TimezoneDetect;
use Icinga\Web\UrlParams;
class GraphController extends MonitoringAwareController
@ -110,16 +108,12 @@ class GraphController extends MonitoringAwareController
throw new HttpNotFoundException($this->translate('No such graph'));
case 1:
$timezoneDetect = new TimezoneDetect();
$charts[0]
->setFrom($this->graphParams['start'])
->setUntil($this->graphParams['end'])
->setWidth($this->graphParams['width'])
->setHeight($this->graphParams['height'])
->setShowLegend((bool) $this->graphParams['legend'])
->setTimeZone(new DateTimeZone(
$timezoneDetect->success() ? $timezoneDetect->getTimezoneName() : date_default_timezone_get()
))
->serveImage($this->getResponse());
default:

View file

@ -6,7 +6,6 @@ use DateInterval;
use DateTime;
use DateTimeZone;
use Icinga\Module\Graphite\Web\Form\Decorator\Proxy;
use Icinga\Util\TimezoneDetect;
use Icinga\Web\Form;
class CustomForm extends Form
@ -23,13 +22,6 @@ class CustomForm extends Form
*/
protected $timestamp = '/^(?:0|-?[1-9]\d*)$/';
/**
* The time zone of all dates and times
*
* @var DateTimeZone
*/
protected $timeZone;
/**
* Right now
*
@ -164,7 +156,7 @@ class CustomForm extends Form
list($date, $time) = explode(
'T',
DateTime::createFromFormat('U', $timestamp)
->setTimezone($this->getTimeZone())
->setTimezone(new DateTimeZone(date_default_timezone_get()))
->format($this->dateTimeFormat)
);
@ -209,8 +201,7 @@ class CustomForm extends Form
$dateTime = DateTime::createFromFormat(
$this->dateTimeFormat,
($date === '' ? $this->getNow()->format('Y-m-d') : $date)
. 'T' . ($time === '' ? $defaultTime : $time),
$this->getTimeZone()
. 'T' . ($time === '' ? $defaultTime : $time)
);
if ($dateTime === false) {
@ -226,36 +217,6 @@ class CustomForm extends Form
}
}
/**
* Get {@link timeZone}
*
* @return DateTimeZone
*/
public function getTimeZone()
{
if ($this->timeZone === null) {
$timezoneDetect = new TimezoneDetect();
$this->timeZone = new DateTimeZone(
$timezoneDetect->success() ? $timezoneDetect->getTimezoneName() : date_default_timezone_get()
);
}
return $this->timeZone;
}
/**
* Set {@link timeZone}
*
* @param DateTimeZone $timeZone
*
* @return $this
*/
public function setTimeZone(DateTimeZone $timeZone)
{
$this->timeZone = $timeZone;
return $this;
}
/**
* Get {@link now}
*
@ -264,7 +225,7 @@ class CustomForm extends Form
public function getNow()
{
if ($this->now === null) {
$this->now = (new DateTime())->setTimezone($this->getTimeZone());
$this->now = new DateTime();
}
return $this->now;

View file

@ -2,7 +2,6 @@
namespace Icinga\Module\Graphite\Graphing;
use DateTimeZone;
use Icinga\Module\Graphite\Util\MacroTemplate;
use Icinga\Web\Response;
use Icinga\Web\Url;
@ -66,13 +65,6 @@ class Chart
*/
protected $showLegend = true;
/**
* The chart's time zone
*
* @var DateTimeZone
*/
protected $timeZone;
/**
* Constructor
*
@ -118,7 +110,7 @@ class Chart
'width' => $this->width,
'height' => $this->height,
'hideLegend' => (string) ! $this->showLegend,
'tz' => $this->timeZone->getName(),
'tz' => date_default_timezone_get(),
'_salt' => time() . '.000',
'vTitle' => 'Percent',
'lineMode' => 'connected',
@ -297,28 +289,4 @@ class Chart
return $this;
}
/**
* Get time zone
*
* @return DateTimeZone
*/
public function getTimeZone()
{
return $this->timeZone;
}
/**
* Set time zone
*
* @param DateTimeZone $timeZone
*
* @return $this
*/
public function setTimeZone(DateTimeZone $timeZone)
{
$this->timeZone = $timeZone;
return $this;
}
}