diff --git a/library/Icinga/Util/TimezoneDetect.php b/library/Icinga/Util/TimezoneDetect.php index 4967c7f57..03d051366 100644 --- a/library/Icinga/Util/TimezoneDetect.php +++ b/library/Icinga/Util/TimezoneDetect.php @@ -3,6 +3,8 @@ namespace Icinga\Util; +use DateTimeZone; + /** * Retrieve timezone information from cookie */ @@ -15,13 +17,6 @@ class TimezoneDetect */ private static $success; - /** - * Timezone offset in minutes - * - * @var int - */ - private static $offset = 0; - /** * @var string */ @@ -34,13 +29,6 @@ class TimezoneDetect */ public static $cookieName = 'icingaweb2-tzo'; - /** - * Timezone name - * - * @var string - */ - private static $timezone; - /** * Create new object and try to identify the timezone */ @@ -50,31 +38,14 @@ class TimezoneDetect return; } - 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; - } - } + if (in_array($_COOKIE[self::$cookieName] ?? null, DateTimeZone::listIdentifiers(), true)) { + self::$timezoneName = $_COOKIE[self::$cookieName]; + self::$success = true; + } else { + self::$success = false; } } - /** - * Get offset - * - * @return int - */ - public function getOffset() - { - return self::$offset; - } - /** * Get timezone name * @@ -102,6 +73,5 @@ class TimezoneDetect { self::$success = null; self::$timezoneName = null; - self::$offset = 0; } } diff --git a/public/js/icinga/timezone.js b/public/js/icinga/timezone.js index 1c2647bec..c92c57746 100644 --- a/public/js/icinga/timezone.js +++ b/public/js/icinga/timezone.js @@ -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); }, /** @@ -67,17 +41,9 @@ * * @param {String} name * @param {String} value - * @param {Number} days */ - writeCookie: function(name, value, days) { - var expires = ''; - - if (days) { - var date = new Date(); - date.setTime(date.getTime()+(days*24*60*60*1000)); - var expires = '; expires=' + date.toGMTString(); - } - document.cookie = name + '=' + value + expires + '; path=/'; + writeCookie: function(name, value) { + document.cookie = name + '=' + value + '; path=/'; }, /** diff --git a/test/php/library/Icinga/Util/TimezoneDetectTest.php b/test/php/library/Icinga/Util/TimezoneDetectTest.php index 9df04720c..7548d4a22 100644 --- a/test/php/library/Icinga/Util/TimezoneDetectTest.php +++ b/test/php/library/Icinga/Util/TimezoneDetectTest.php @@ -8,38 +8,42 @@ use Icinga\Util\TimezoneDetect; class TimezoneDetectTest extends BaseTestCase { - public function testPositiveTimezoneOffsetSeparatedByComma() + public function testInvalidTimezoneNameInCookie(): void { - $this->assertTimezoneDetection('3600,0', 'Europe/Paris'); + $tzDetect = new TimezoneDetect(); + $tzDetect->reset(); + + $_COOKIE[TimezoneDetect::$cookieName] = 'ABC'; + $tzDetect = new TimezoneDetect(); + $this->assertFalse( + $tzDetect->success(), + false, + 'Failed to assert invalid timezone name is detected' + ); + + $this->assertNull( + $tzDetect->getTimezoneName(), + 'Failed to assert that the timezone name will not be set for invalid timezone' + ); } - 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) + public function testValidTimezoneNameInCookie(): void { $tzDetect = new TimezoneDetect(); $tzDetect->reset(); - $_COOKIE[TimezoneDetect::$cookieName] = $cookieValue; + $_COOKIE[TimezoneDetect::$cookieName] = "Europe/Berlin"; $tzDetect = new TimezoneDetect(); + $this->assertTrue( + $tzDetect->success(), + true, + 'Failed to assert that the valid timezone name is detected' + ); + $this->assertSame( $tzDetect->getTimezoneName(), - $expectedTimezoneName, - 'Failed asserting that the timezone "' . $expectedTimezoneName - . '" is being detected from the cookie value "' . $cookieValue . '"' + "Europe/Berlin", + 'Failed to assert that the valid timezone name was correctly set' ); } }