mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-27 07:44:04 +02:00
Fix browser timezone information being lost
This commit is contained in:
parent
ff04a2ea43
commit
2d265babf7
@ -15,13 +15,6 @@ class TimezoneDetect
|
||||
*/
|
||||
private static $success;
|
||||
|
||||
/**
|
||||
* Timezone offset in minutes
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static $offset = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@ -34,13 +27,6 @@ class TimezoneDetect
|
||||
*/
|
||||
public static $cookieName = 'icingaweb2-tzo';
|
||||
|
||||
/**
|
||||
* Timezone name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $timezone;
|
||||
|
||||
/**
|
||||
* Create new object and try to identify the timezone
|
||||
*/
|
||||
@ -51,30 +37,11 @@ class TimezoneDetect
|
||||
}
|
||||
|
||||
if (array_key_exists(self::$cookieName, $_COOKIE)) {
|
||||
$matches = array();
|
||||
if (preg_match('/\A(-?\d+)[\-,](\d+)\z/', $_COOKIE[self::$cookieName], $matches)) {
|
||||
$offset = $matches[1];
|
||||
$timezoneName = timezone_name_from_abbr('', (int) $offset, (int) $matches[2]);
|
||||
|
||||
self::$success = (bool) $timezoneName;
|
||||
if (self::$success) {
|
||||
self::$offset = $offset;
|
||||
self::$timezoneName = $timezoneName;
|
||||
}
|
||||
}
|
||||
self::$timezoneName = $_COOKIE[self::$cookieName];
|
||||
self::$success = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getOffset()
|
||||
{
|
||||
return self::$offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get timezone name
|
||||
*
|
||||
@ -102,6 +69,5 @@ class TimezoneDetect
|
||||
{
|
||||
self::$success = null;
|
||||
self::$timezoneName = null;
|
||||
self::$offset = 0;
|
||||
}
|
||||
}
|
||||
|
@ -4,28 +4,6 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Get the maximum timezone offset
|
||||
*
|
||||
* @returns {Number}
|
||||
*/
|
||||
Date.prototype.getStdTimezoneOffset = function() {
|
||||
var year = new Date().getFullYear();
|
||||
var offsetInJanuary = new Date(year, 0, 2).getTimezoneOffset();
|
||||
var offsetInJune = new Date(year, 5, 2).getTimezoneOffset();
|
||||
|
||||
return Math.max(offsetInJanuary, offsetInJune);
|
||||
};
|
||||
|
||||
/**
|
||||
* Test for daylight saving time zone
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Date.prototype.isDst = function() {
|
||||
return this.getStdTimezoneOffset() !== this.getTimezoneOffset();
|
||||
};
|
||||
|
||||
/**
|
||||
* Write timezone information into a cookie
|
||||
*
|
||||
@ -51,15 +29,11 @@
|
||||
* Write timezone information into cookie
|
||||
*/
|
||||
writeTimezone: function() {
|
||||
var date = new Date();
|
||||
var timezoneOffset = (date.getTimezoneOffset()*60) * -1;
|
||||
var dst = date.isDst();
|
||||
|
||||
if (this.readCookie(this.cookieName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.writeCookie(this.cookieName, timezoneOffset + '-' + Number(dst), 1);
|
||||
this.writeCookie(this.cookieName, Intl.DateTimeFormat().resolvedOptions().timeZone);
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Tests\Icinga\Util;
|
||||
|
||||
use Icinga\Test\BaseTestCase;
|
||||
use Icinga\Util\TimezoneDetect;
|
||||
|
||||
class TimezoneDetectTest extends BaseTestCase
|
||||
{
|
||||
public function testPositiveTimezoneOffsetSeparatedByComma()
|
||||
{
|
||||
$this->assertTimezoneDetection('3600,0', 'Europe/Paris');
|
||||
}
|
||||
|
||||
public function testPositiveTimezoneOffsetSeparatedByHyphen()
|
||||
{
|
||||
$this->assertTimezoneDetection('3600-0', 'Europe/Paris');
|
||||
}
|
||||
|
||||
public function testNegativeTimezoneOffsetSeparatedByComma()
|
||||
{
|
||||
$this->assertTimezoneDetection('-3600,0', 'Atlantic/Azores');
|
||||
}
|
||||
|
||||
public function testNegativeTimezoneOffsetSeparatedByHyphen()
|
||||
{
|
||||
$this->assertTimezoneDetection('-3600-0', 'Atlantic/Azores');
|
||||
}
|
||||
|
||||
protected function assertTimezoneDetection($cookieValue, $expectedTimezoneName)
|
||||
{
|
||||
$tzDetect = new TimezoneDetect();
|
||||
$tzDetect->reset();
|
||||
|
||||
$_COOKIE[TimezoneDetect::$cookieName] = $cookieValue;
|
||||
$tzDetect = new TimezoneDetect();
|
||||
$this->assertSame(
|
||||
$tzDetect->getTimezoneName(),
|
||||
$expectedTimezoneName,
|
||||
'Failed asserting that the timezone "' . $expectedTimezoneName
|
||||
. '" is being detected from the cookie value "' . $cookieValue . '"'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user