MDL-50704 date: Fix invalid timezone mapping for +14
authorFrederic Massart <fred@moodle.com>
Mon, 9 May 2016 05:27:25 +0000 (13:27 +0800)
committerFrederic Massart <fred@moodle.com>
Mon, 9 May 2016 09:28:38 +0000 (17:28 +0800)
The value Etc/GMT-14 was added to the list of known timezones,
and Etc/GMT+13 & Etc/GMT+14 were removed as they are not valid.

Tests were added to ensure that legacy user timezones are mapped
to known timezones. Other tests were changed to reflect the changes
with regards to Etc/GMT+13 and +-14.

lib/classes/date.php
lib/tests/date_test.php
lib/tests/moodlelib_test.php

index 3685809..a2b4a6b 100644 (file)
@@ -72,8 +72,12 @@ class core_date {
 
         if (is_numeric($currentvalue)) {
             // UTC offset.
-            $modifier = ($currentvalue > 0) ? '+' : '';
-            $a = 'UTC' . $modifier . number_format($currentvalue, 1);
+            if ($currentvalue == 0) {
+                $a = 'UTC';
+            } else {
+                $modifier = ($currentvalue > 0) ? '+' : '';
+                $a = 'UTC' . $modifier . number_format($currentvalue, 1);
+            }
             $timezones[$currentvalue] = get_string('timezoneinvalid', 'core_admin', $a);
         } else {
             // Some string we don't recognise.
@@ -508,7 +512,7 @@ class core_date {
         );
 
         // Legacy GMT fallback.
-        for ($i = -14; $i <= 13; $i++) {
+        for ($i = -12; $i <= 14; $i++) {
             $off = abs($i);
             if ($i < 0) {
                 $mapto = 'Etc/GMT+' . $off;
index 3e4d908..2cd0d9f 100644 (file)
@@ -77,7 +77,7 @@ class core_date_testcase extends advanced_testcase {
         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-14));
         $this->assertSame('Etc/GMT-12', core_date::normalise_timezone(12));
         $this->assertSame('Etc/GMT-13', core_date::normalise_timezone(13));
-        $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(14));
+        $this->assertSame('Etc/GMT-14', core_date::normalise_timezone(14));
 
         $this->assertSame('Asia/Kabul', core_date::normalise_timezone(4.5));
         $this->assertSame('Asia/Kolkata', core_date::normalise_timezone(5.5));
@@ -107,7 +107,7 @@ class core_date_testcase extends advanced_testcase {
         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-14));
         $this->assertSame('Etc/GMT-12', core_date::normalise_timezone(12));
         $this->assertSame('Etc/GMT-13', core_date::normalise_timezone(13));
-        $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(14));
+        $this->assertSame('Etc/GMT-14', core_date::normalise_timezone(14));
 
         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
         $tz = new DateTimeZone('Pacific/Auckland');
@@ -334,6 +334,76 @@ class core_date_testcase extends advanced_testcase {
         $this->assertSame('Etc/GMT-1', date_default_timezone_get());
     }
 
+    public function legacyUserTimezoneProvider() {
+        return [
+            ['', 'Australia/Perth'],            // Fallback on default timezone.
+            ['-13.0', 'Australia/Perth'],       // Fallback on default timezone.
+            ['-12.5', 'Etc/GMT+12'],
+            ['-12.0', 'Etc/GMT+12'],
+            ['-11.5', 'Etc/GMT+11'],
+            ['-11.0', 'Etc/GMT+11'],
+            ['-10.5', 'Etc/GMT+10'],
+            ['-10.0', 'Etc/GMT+10'],
+            ['-9.5', 'Etc/GMT+9'],
+            ['-9.0', 'Etc/GMT+9'],
+            ['-8.5', 'Etc/GMT+8'],
+            ['-8.0', 'Etc/GMT+8'],
+            ['-7.5', 'Etc/GMT+7'],
+            ['-7.0', 'Etc/GMT+7'],
+            ['-6.5', 'Etc/GMT+6'],
+            ['-6.0', 'Etc/GMT+6'],
+            ['-5.5', 'Etc/GMT+5'],
+            ['-5.0', 'Etc/GMT+5'],
+            ['-4.5', 'Etc/GMT+4'],
+            ['-4.0', 'Etc/GMT+4'],
+            ['-3.5', 'Etc/GMT+3'],
+            ['-3.0', 'Etc/GMT+3'],
+            ['-2.5', 'Etc/GMT+2'],
+            ['-2.0', 'Etc/GMT+2'],
+            ['-1.5', 'Etc/GMT+1'],
+            ['-1.0', 'Etc/GMT+1'],
+            ['-0.5', 'Etc/GMT'],
+            ['0', 'Etc/GMT'],
+            ['0.0', 'Etc/GMT'],
+            ['0.5', 'Etc/GMT'],
+            ['1.0', 'Etc/GMT-1'],
+            ['1.5', 'Etc/GMT-1'],
+            ['2.0', 'Etc/GMT-2'],
+            ['2.5', 'Etc/GMT-2'],
+            ['3.0', 'Etc/GMT-3'],
+            ['3.5', 'Etc/GMT-3'],
+            ['4.0', 'Etc/GMT-4'],
+            ['4.5', 'Asia/Kabul'],
+            ['5.0', 'Etc/GMT-5'],
+            ['5.5', 'Asia/Kolkata'],
+            ['6.0', 'Etc/GMT-6'],
+            ['6.5', 'Asia/Rangoon'],
+            ['7.0', 'Etc/GMT-7'],
+            ['7.5', 'Etc/GMT-7'],
+            ['8.0', 'Etc/GMT-8'],
+            ['8.5', 'Etc/GMT-8'],
+            ['9.0', 'Etc/GMT-9'],
+            ['9.5', 'Australia/Darwin'],
+            ['10.0', 'Etc/GMT-10'],
+            ['10.5', 'Etc/GMT-10'],
+            ['11.0', 'Etc/GMT-11'],
+            ['11.5', 'Etc/GMT-11'],
+            ['12.0', 'Etc/GMT-12'],
+            ['12.5', 'Etc/GMT-12'],
+            ['13.0', 'Etc/GMT-13'],
+        ];
+    }
+
+    /**
+     * @dataProvider legacyUserTimezoneProvider
+     * @param string $tz The legacy timezone.
+     * @param string $expected The expected converted timezone.
+     */
+    public function test_get_legacy_user_timezone($tz, $expected) {
+        $this->setTimezone('Australia/Perth', 'Australia/Perth');
+        $this->assertEquals($expected, core_date::get_user_timezone($tz));
+    }
+
     public function test_get_user_timezone() {
         global $CFG, $USER;
         $this->resetAfterTest();
index fb9d5ae..34e05bc 100644 (file)
@@ -1582,7 +1582,7 @@ class core_moodlelib_testcase extends advanced_testcase {
                 'expectedoutput' => '1309485600'
             ),
             array(
-                'usertimezone' => '14', // Server time.
+                'usertimezone' => '-14', // Server time.
                 'year' => '2011',
                 'month' => '7',
                 'day' => '1',