Merge branch 'MDL-68728-master-2' of git://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / date_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Tests core_date class.
19  *
20  * @package   core
21  * @copyright 2015 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  * @author    Petr Skoda <petr.skoda@totaralms.com>
24  */
26 defined('MOODLE_INTERNAL') || die();
28 /**
29  * Tests core_date class.
30  *
31  * @package   core
32  * @copyright 2015 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
33  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  * @author    Petr Skoda <petr.skoda@totaralms.com>
35  */
36 class core_date_testcase extends advanced_testcase {
37     public function test_get_default_php_timezone() {
38         $this->resetAfterTest();
40         $origtz = core_date::get_default_php_timezone();
41         $this->assertNotEmpty($origtz);
43         $this->setTimezone('Pacific/Auckland', 'Europe/Prague');
44         $this->assertSame('Europe/Prague', core_date::get_default_php_timezone());
46         $this->setTimezone('Pacific/Auckland', 'UTC');
47         $this->assertSame('UTC', core_date::get_default_php_timezone());
49         $this->setTimezone('Pacific/Auckland', 'GMT');
50         $this->assertSame('GMT', core_date::get_default_php_timezone());
51     }
53     public function test_normalise_timezone() {
54         $this->resetAfterTest();
56         $this->setTimezone('Pacific/Auckland');
57         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone('Pacific/Auckland'));
58         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone('99'));
59         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(99));
60         $this->assertSame('GMT', core_date::normalise_timezone('GMT'));
61         $this->assertSame('UTC', core_date::normalise_timezone('UTC'));
62         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone('xxxxxxxx'));
63         $this->assertSame('Europe/Berlin', core_date::normalise_timezone('Central European Time'));
64         $this->assertSame('Etc/GMT', core_date::normalise_timezone('0'));
65         $this->assertSame('Etc/GMT', core_date::normalise_timezone('0.0'));
66         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone(2));
67         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone('2.0'));
68         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone(-2));
69         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone('-2.0'));
70         $this->assertSame('Etc/GMT+4', core_date::normalise_timezone(-4));
71         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone('UTC+2'));
72         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone('UTC-2'));
73         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone('GMT+2'));
74         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone('GMT-2'));
75         $this->assertSame('Etc/GMT+12', core_date::normalise_timezone(-12));
76         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-13));
77         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-14));
78         $this->assertSame('Etc/GMT-12', core_date::normalise_timezone(12));
79         $this->assertSame('Etc/GMT-13', core_date::normalise_timezone(13));
80         $this->assertSame('Etc/GMT-14', core_date::normalise_timezone(14));
82         $this->assertSame('Asia/Kabul', core_date::normalise_timezone(4.5));
83         $this->assertSame('Asia/Kolkata', core_date::normalise_timezone(5.5));
84         $this->assertSame('Asia/Rangoon', core_date::normalise_timezone(6.5));
85         $this->assertSame('Australia/Darwin', core_date::normalise_timezone('9.5'));
87         $this->setTimezone('99', 'Pacific/Auckland');
88         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone('Pacific/Auckland'));
89         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone('99'));
90         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(99));
91         $this->assertSame('GMT', core_date::normalise_timezone('GMT'));
92         $this->assertSame('UTC', core_date::normalise_timezone('UTC'));
93         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone('xxxxxxxx'));
94         $this->assertSame('Europe/Berlin', core_date::normalise_timezone('Central European Time'));
95         $this->assertSame('Etc/GMT', core_date::normalise_timezone('0'));
96         $this->assertSame('Etc/GMT', core_date::normalise_timezone('0.0'));
97         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone(2));
98         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone('2.0'));
99         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone(-2));
100         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone('-2.0'));
101         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone('UTC+2'));
102         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone('UTC-2'));
103         $this->assertSame('Etc/GMT-2', core_date::normalise_timezone('GMT+2'));
104         $this->assertSame('Etc/GMT+2', core_date::normalise_timezone('GMT-2'));
105         $this->assertSame('Etc/GMT+12', core_date::normalise_timezone(-12));
106         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-13));
107         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-14));
108         $this->assertSame('Etc/GMT-12', core_date::normalise_timezone(12));
109         $this->assertSame('Etc/GMT-13', core_date::normalise_timezone(13));
110         $this->assertSame('Etc/GMT-14', core_date::normalise_timezone(14));
112         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
113         $tz = new DateTimeZone('Pacific/Auckland');
114         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone($tz));
115     }
117     public function test_windows_conversion() {
118         $file = __DIR__ . '/fixtures/timezonewindows.xml';
120         $contents = file_get_contents($file);
121         preg_match_all('/<mapZone other="([^"]+)" territory="001" type="([^"]+)"\/>/', $contents, $matches, PREG_SET_ORDER);
123         $this->assertCount(104, $matches); // NOTE: If the file contents change edit the core_date class and update this.
125         foreach ($matches as $match) {
126             $result = core_date::normalise_timezone($match[1]);
127             if ($result == $match[2]) {
128                 $this->assertSame($match[2], $result);
129             } else {
130                 $data = new DateTime('now', new DateTimeZone($match[2]));
131                 $expectedoffset = $data->getOffset();
132                 $data = new DateTime('now', new DateTimeZone($result));
133                 $resultoffset = $data->getOffset();
134                 $this->assertSame($expectedoffset, $resultoffset, "$match[1] is expected to be converted to $match[2] not $result");
135             }
136         }
137     }
139     /**
140      * Sanity test for PHP stuff.
141      */
142     public function test_php_gmt_offsets() {
143         $this->resetAfterTest();
145         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
147         for ($i = -12; $i < 0; $i++) {
148             $date = new DateTime('now', new DateTimeZone("Etc/GMT{$i}"));
149             $this->assertSame(- $i * 60 * 60, $date->getOffset());
150             $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("GMT{$i}")));
151             $this->assertSame($i * 60 * 60, $date->getOffset());
152             $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("UTC{$i}")));
153             $this->assertSame($i * 60 * 60, $date->getOffset());
154         }
156         $date = new DateTime('now', new DateTimeZone('Etc/GMT'));
157         $this->assertSame(0, $date->getOffset());
159         for ($i = 1; $i <= 12; $i++) {
160             $date = new DateTime('now', new DateTimeZone("Etc/GMT+{$i}"));
161             $this->assertSame(- $i * 60 * 60, $date->getOffset());
162             $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("GMT+{$i}")));
163             $this->assertSame($i * 60 * 60, $date->getOffset());
164             $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("UTC+{$i}")));
165             $this->assertSame($i * 60 * 60, $date->getOffset());
166         }
167     }
169     public function test_timezone_lang_strings() {
170         $phpzones = DateTimeZone::listIdentifiers();
171         $manager = get_string_manager();
172         foreach ($phpzones as $tz) {
173             $this->assertTrue($manager->string_exists(strtolower($tz), 'core_timezones'));
174         }
175     }
177     public function test_get_localised_timezone() {
178         $this->resetAfterTest();
180         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
182         $result = core_date::get_localised_timezone('Pacific/Auckland');
183         $this->assertSame('Pacific/Auckland', $result);
185         $result = core_date::get_localised_timezone('99');
186         $this->assertSame('Server timezone (Pacific/Auckland)', $result);
188         $result = core_date::get_localised_timezone(99);
189         $this->assertSame('Server timezone (Pacific/Auckland)', $result);
191         $result = core_date::get_localised_timezone('Etc/GMT-1');
192         $this->assertSame('UTC+1', $result);
194         $result = core_date::get_localised_timezone('Etc/GMT+2');
195         $this->assertSame('UTC-2', $result);
197         $result = core_date::get_localised_timezone('GMT');
198         $this->assertSame('UTC', $result);
200         $result = core_date::get_localised_timezone('Etc/GMT');
201         $this->assertSame('UTC', $result);
202     }
204     public function test_get_list_of_timezones() {
205         $this->resetAfterTest();
207         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
209         $phpzones = DateTimeZone::listIdentifiers();
211         $zones = core_date::get_list_of_timezones();
212         $this->assertSame(count($phpzones), count($zones));
213         foreach ($zones as $zone => $zonename) {
214             $this->assertSame($zone, $zonename); // The same in English!
215             $this->assertContains($zone, $phpzones); // No extras expected.
216         }
218         $this->assertSame($zones, core_date::get_list_of_timezones(null, false));
220         $nnzones = core_date::get_list_of_timezones(null, true);
221         $last = $nnzones['99'];
222         $this->assertSame('Server timezone (Pacific/Auckland)', $last);
223         unset($nnzones['99']);
224         $this->assertSame($zones, $nnzones);
226         $nnzones = core_date::get_list_of_timezones('99', false);
227         $last = $nnzones['99'];
228         $this->assertSame('Server timezone (Pacific/Auckland)', $last);
229         unset($nnzones['99']);
230         $this->assertSame($zones, $nnzones);
232         $xxzones = core_date::get_list_of_timezones('xx', false);
233         $xx = $xxzones['xx'];
234         $this->assertSame('Invalid timezone "xx"', $xx);
235         unset($xxzones['xx']);
236         $this->assertSame($zones, $xxzones);
238         $xxzones = core_date::get_list_of_timezones('1', false);
239         $xx = $xxzones['1'];
240         $this->assertSame('Invalid timezone "UTC+1.0"', $xx);
241         unset($xxzones['1']);
242         $this->assertSame($zones, $xxzones);
244         $xxzones = core_date::get_list_of_timezones('-1.5', false);
245         $xx = $xxzones['-1.5'];
246         $this->assertSame('Invalid timezone "UTC-1.5"', $xx);
247         unset($xxzones['-1.5']);
248         $this->assertSame($zones, $xxzones);
250     }
252     public function test_get_server_timezone() {
253         global $CFG;
254         $this->resetAfterTest();
256         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
257         $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
259         $this->setTimezone('Pacific/Auckland', 'Europe/Prague');
260         $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
262         $this->setTimezone('99', 'Pacific/Auckland');
263         $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
265         $this->setTimezone(99, 'Pacific/Auckland');
266         $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
268         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
269         unset($CFG->timezone);
270         $this->assertSame('Pacific/Auckland', core_date::get_server_timezone());
272         // Admin should fix the settings.
273         $this->setTimezone('xxx/zzzz', 'Europe/Prague');
274         $this->assertSame('Europe/Prague', core_date::get_server_timezone());
275     }
277     public function test_get_server_timezone_object() {
278         $this->resetAfterTest();
280         $zones = core_date::get_list_of_timezones();
281         foreach ($zones as $zone) {
282             $this->setTimezone($zone, 'Pacific/Auckland');
283             $tz = core_date::get_server_timezone_object();
284             $this->assertInstanceOf('DateTimeZone', $tz);
285             $this->assertSame($zone, $tz->getName());
286         }
287     }
289     public function test_set_default_server_timezone() {
290         global $CFG;
291         $this->resetAfterTest();
293         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
294         unset($CFG->timezone);
295         date_default_timezone_set('UTC');
296         core_date::set_default_server_timezone();
297         $this->assertSame('Pacific/Auckland', date_default_timezone_get());
299         $this->setTimezone('', 'Pacific/Auckland');
300         date_default_timezone_set('UTC');
301         core_date::set_default_server_timezone();
302         $this->assertSame('Pacific/Auckland', date_default_timezone_get());
304         $this->setTimezone('99', 'Pacific/Auckland');
305         date_default_timezone_set('UTC');
306         core_date::set_default_server_timezone();
307         $this->assertSame('Pacific/Auckland', date_default_timezone_get());
309         $this->setTimezone(99, 'Pacific/Auckland');
310         date_default_timezone_set('UTC');
311         core_date::set_default_server_timezone();
312         $this->assertSame('Pacific/Auckland', date_default_timezone_get());
314         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
315         $CFG->timezone = 'UTC';
316         core_date::set_default_server_timezone();
317         $this->assertSame('UTC', date_default_timezone_get());
319         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
320         $CFG->timezone = 'Australia/Perth';
321         core_date::set_default_server_timezone();
322         $this->assertSame('Australia/Perth', date_default_timezone_get());
324         $this->setTimezone('0', 'Pacific/Auckland');
325         date_default_timezone_set('UTC');
326         core_date::set_default_server_timezone();
327         $this->assertSame('Etc/GMT', date_default_timezone_get());
329         $this->setTimezone('1', 'Pacific/Auckland');
330         date_default_timezone_set('UTC');
331         core_date::set_default_server_timezone();
332         $this->assertSame('Etc/GMT-1', date_default_timezone_get());
334         $this->setTimezone(1, 'Pacific/Auckland');
335         date_default_timezone_set('UTC');
336         core_date::set_default_server_timezone();
337         $this->assertSame('Etc/GMT-1', date_default_timezone_get());
339         $this->setTimezone('1.0', 'Pacific/Auckland');
340         date_default_timezone_set('UTC');
341         core_date::set_default_server_timezone();
342         $this->assertSame('Etc/GMT-1', date_default_timezone_get());
343     }
345     public function legacyUserTimezoneProvider() {
346         return [
347             ['', 'Australia/Perth'],            // Fallback on default timezone.
348             ['-13.0', 'Australia/Perth'],       // Fallback on default timezone.
349             ['-12.5', 'Etc/GMT+12'],
350             ['-12.0', 'Etc/GMT+12'],
351             ['-11.5', 'Etc/GMT+11'],
352             ['-11.0', 'Etc/GMT+11'],
353             ['-10.5', 'Etc/GMT+10'],
354             ['-10.0', 'Etc/GMT+10'],
355             ['-9.5', 'Etc/GMT+9'],
356             ['-9.0', 'Etc/GMT+9'],
357             ['-8.5', 'Etc/GMT+8'],
358             ['-8.0', 'Etc/GMT+8'],
359             ['-7.5', 'Etc/GMT+7'],
360             ['-7.0', 'Etc/GMT+7'],
361             ['-6.5', 'Etc/GMT+6'],
362             ['-6.0', 'Etc/GMT+6'],
363             ['-5.5', 'Etc/GMT+5'],
364             ['-5.0', 'Etc/GMT+5'],
365             ['-4.5', 'Etc/GMT+4'],
366             ['-4.0', 'Etc/GMT+4'],
367             ['-3.5', 'Etc/GMT+3'],
368             ['-3.0', 'Etc/GMT+3'],
369             ['-2.5', 'Etc/GMT+2'],
370             ['-2.0', 'Etc/GMT+2'],
371             ['-1.5', 'Etc/GMT+1'],
372             ['-1.0', 'Etc/GMT+1'],
373             ['-0.5', 'Etc/GMT'],
374             ['0', 'Etc/GMT'],
375             ['0.0', 'Etc/GMT'],
376             ['0.5', 'Etc/GMT'],
377             ['1.0', 'Etc/GMT-1'],
378             ['1.5', 'Etc/GMT-1'],
379             ['2.0', 'Etc/GMT-2'],
380             ['2.5', 'Etc/GMT-2'],
381             ['3.0', 'Etc/GMT-3'],
382             ['3.5', 'Etc/GMT-3'],
383             ['4.0', 'Etc/GMT-4'],
384             ['4.5', 'Asia/Kabul'],
385             ['5.0', 'Etc/GMT-5'],
386             ['5.5', 'Asia/Kolkata'],
387             ['6.0', 'Etc/GMT-6'],
388             ['6.5', 'Asia/Rangoon'],
389             ['7.0', 'Etc/GMT-7'],
390             ['7.5', 'Etc/GMT-7'],
391             ['8.0', 'Etc/GMT-8'],
392             ['8.5', 'Etc/GMT-8'],
393             ['9.0', 'Etc/GMT-9'],
394             ['9.5', 'Australia/Darwin'],
395             ['10.0', 'Etc/GMT-10'],
396             ['10.5', 'Etc/GMT-10'],
397             ['11.0', 'Etc/GMT-11'],
398             ['11.5', 'Etc/GMT-11'],
399             ['12.0', 'Etc/GMT-12'],
400             ['12.5', 'Etc/GMT-12'],
401             ['13.0', 'Etc/GMT-13'],
402         ];
403     }
405     /**
406      * @dataProvider legacyUserTimezoneProvider
407      * @param string $tz The legacy timezone.
408      * @param string $expected The expected converted timezone.
409      */
410     public function test_get_legacy_user_timezone($tz, $expected) {
411         $this->setTimezone('Australia/Perth', 'Australia/Perth');
412         $this->assertEquals($expected, core_date::get_user_timezone($tz));
413     }
415     public function test_get_user_timezone() {
416         global $CFG, $USER;
417         $this->resetAfterTest();
419         // Null parameter.
421         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
422         $USER->timezone = 'Pacific/Auckland';
423         $CFG->forcetimezone = '99';
424         $this->assertSame('Pacific/Auckland', core_date::get_user_timezone(null));
425         $this->assertSame('Pacific/Auckland', core_date::get_user_timezone());
427         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
428         $USER->timezone = 'Pacific/Auckland';
429         $CFG->forcetimezone = 'Europe/Berlin';
430         $this->assertSame('Europe/Berlin', core_date::get_user_timezone(null));
431         $this->assertSame('Europe/Berlin', core_date::get_user_timezone());
433         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
434         $USER->timezone = 'Pacific/Auckland';
435         $CFG->forcetimezone = 'xxx/yyy';
436         $this->assertSame('Europe/Prague', core_date::get_user_timezone(null));
437         $this->assertSame('Europe/Prague', core_date::get_user_timezone());
439         $this->setTimezone('Europe/Prague', 'Pacific/Auckland');
440         $USER->timezone = 'abc/def';
441         $CFG->forcetimezone = '99';
442         $this->assertSame('Europe/Prague', core_date::get_user_timezone(null));
443         $this->assertSame('Europe/Prague', core_date::get_user_timezone());
445         $this->setTimezone('xxx/yyy', 'Europe/London');
446         $USER->timezone = 'abc/def';
447         $CFG->forcetimezone = 'Europe/Berlin';
448         $this->assertSame('Europe/Berlin', core_date::get_user_timezone(null));
449         $this->assertSame('Europe/Berlin', core_date::get_user_timezone());
451         $this->setTimezone('xxx/yyy', 'Europe/London');
452         $USER->timezone = 'abc/def';
453         $CFG->forcetimezone = 99;
454         $this->assertSame('Europe/London', core_date::get_user_timezone(null));
455         $this->assertSame('Europe/London', core_date::get_user_timezone());
457         // User object parameter.
458         $admin = get_admin();
460         $this->setTimezone('Europe/London');
461         $USER->timezone = 'Pacific/Auckland';
462         $CFG->forcetimezone = '99';
463         $admin->timezone = 'Australia/Perth';
464         $this->assertSame('Australia/Perth', core_date::get_user_timezone($admin));
466         $this->setTimezone('Europe/Prague');
467         $USER->timezone = 'Pacific/Auckland';
468         $CFG->forcetimezone = '99';
469         $admin->timezone = '99';
470         $this->assertSame('Europe/Prague', core_date::get_user_timezone($admin));
472         $this->setTimezone('99', 'Europe/London');
473         $USER->timezone = 'Pacific/Auckland';
474         $CFG->forcetimezone = '99';
475         $admin->timezone = '99';
476         $this->assertSame('Europe/London', core_date::get_user_timezone($admin));
478         $this->setTimezone('Europe/Prague');
479         $USER->timezone = 'Pacific/Auckland';
480         $CFG->forcetimezone = '99';
481         $admin->timezone = 'xx/zz';
482         $this->assertSame('Europe/Prague', core_date::get_user_timezone($admin));
484         $this->setTimezone('Europe/Prague');
485         $USER->timezone = 'Pacific/Auckland';
486         $CFG->forcetimezone = '99';
487         unset($admin->timezone);
488         $this->assertSame('Europe/Prague', core_date::get_user_timezone($admin));
490         $this->setTimezone('Europe/Prague');
491         $USER->timezone = 'Pacific/Auckland';
492         $CFG->forcetimezone = 'Europe/Berlin';
493         $admin->timezone = 'Australia/Perth';
494         $this->assertSame('Europe/Berlin', core_date::get_user_timezone($admin));
496         // Other scalar parameter.
498         $this->setTimezone('Europe/Prague');
499         $CFG->forcetimezone = '99';
501         $USER->timezone = 'Pacific/Auckland';
502         $this->assertSame('Pacific/Auckland', core_date::get_user_timezone('99'));
503         $this->assertSame('Etc/GMT-1', core_date::get_user_timezone('1'));
504         $this->assertSame('Etc/GMT+1', core_date::get_user_timezone(-1));
505         $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
506         $USER->timezone = '99';
507         $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
508         $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
509         $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
510         $USER->timezone = 'xxz/zzz';
511         $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
513         $this->setTimezone('99', 'Europe/Prague');
514         $CFG->forcetimezone = '99';
516         $USER->timezone = 'Pacific/Auckland';
517         $this->assertSame('Pacific/Auckland', core_date::get_user_timezone('99'));
518         $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
519         $USER->timezone = '99';
520         $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
521         $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
522         $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
523         $USER->timezone = 99;
524         $this->assertSame('Europe/London', core_date::get_user_timezone('Europe/London'));
525         $this->assertSame('Europe/Prague', core_date::get_user_timezone('xxx/zzz'));
526         $USER->timezone = 'xxz/zzz';
527         $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
529         $this->setTimezone('xxx', 'Europe/Prague');
530         $CFG->forcetimezone = '99';
531         $USER->timezone = 'xxx';
532         $this->assertSame('Europe/Prague', core_date::get_user_timezone('99'));
533         $this->assertSame('Europe/Prague', core_date::get_user_timezone(99));
534         $this->assertSame('Etc/GMT-1', core_date::get_user_timezone(1));
536         $this->setTimezone('Europe/Prague');
537         $CFG->forcetimezone = 'Pacific/Auckland';
538         $USER->timezone = 'Europe/London';
539         $this->assertSame('Pacific/Auckland', core_date::get_user_timezone(99));
540         $this->assertSame('Europe/Berlin', core_date::get_user_timezone('Europe/Berlin'));
542         // TZ object param.
544         $this->setTimezone('UTC');
545         $USER->timezone = 'Europe/London';
546         $CFG->forcetimezone = 99;
547         $tz = new DateTimeZone('Pacific/Auckland');
548         $this->assertSame('Pacific/Auckland', core_date::get_user_timezone($tz));
549     }
551     public function test_get_user_timezone_object() {
552         global $CFG, $USER;
553         $this->resetAfterTest();
555         $this->setTimezone('Pacific/Auckland');
556         $CFG->forcetimezone = '99';
558         $zones = core_date::get_list_of_timezones();
559         foreach ($zones as $zone) {
560             $USER->timezone = $zone;
561             $tz = core_date::get_user_timezone_object();
562             $this->assertInstanceOf('DateTimeZone', $tz);
563             $this->assertSame($zone, $tz->getName());
564         }
565     }