Merge branch 'MDL-51434-master' of git://github.com/jleyva/moodle
[moodle.git] / lib / tests / date_legacy_test.php
blobd495bcd1ccfada074a70830c0ec2de5401086140
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.
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 legacy Moodle date/time functions.
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>
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Tests legacy Moodle date/time functions.
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>
36 class core_date_legacy_testcase extends advanced_testcase {
37 public function test_settings() {
38 global $CFG;
39 $this->resetAfterTest();
41 $this->assertNotEmpty($CFG->timezone);
43 $this->assertSame('99', $CFG->forcetimezone);
45 $user = $this->getDataGenerator()->create_user();
46 $this->assertSame('99', $user->timezone);
49 public function test_get_list_of_timezones() {
50 // Use timezones that are not problematic, this way we may test before
51 // and after the big tz rewrite.
52 $list = get_list_of_timezones();
53 $this->assertDebuggingCalled();
54 $this->assertArrayHasKey('Europe/London', $list);
55 $this->assertArrayHasKey('Pacific/Auckland', $list);
56 $this->assertArrayHasKey('America/New_York', $list);
57 $this->assertArrayHasKey('Europe/Berlin', $list);
58 $this->assertArrayHasKey('Europe/Prague', $list);
59 $this->assertArrayHasKey('Australia/Perth', $list);
60 $this->assertArrayHasKey('Australia/Lord_Howe', $list);
63 public function test_get_user_timezone() {
64 global $CFG, $USER;
66 $this->resetAfterTest();
68 $user = $this->getDataGenerator()->create_user();
69 $this->setUser($user);
71 // All set to something.
73 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
74 $USER->timezone = 'Europe/Prague';
76 $tz = get_user_timezone();
77 $this->assertSame('Europe/Prague', $tz);
79 $tz = get_user_timezone(99);
80 $this->assertSame('Europe/Prague', $tz);
81 $tz = get_user_timezone('99');
82 $this->assertSame('Europe/Prague', $tz);
84 $tz = get_user_timezone('Europe/Berlin');
85 $this->assertSame('Europe/Berlin', $tz);
87 // User timezone not set.
89 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
90 $USER->timezone = '99';
92 $tz = get_user_timezone();
93 $this->assertSame('Pacific/Auckland', $tz);
95 $tz = get_user_timezone(99);
96 $this->assertSame('Pacific/Auckland', $tz);
97 $tz = get_user_timezone('99');
98 $this->assertSame('Pacific/Auckland', $tz);
100 $tz = get_user_timezone('Europe/Berlin');
101 $this->assertSame('Europe/Berlin', $tz);
103 // Server timezone not set.
105 $this->setTimezone('99', 'Pacific/Auckland');
106 $USER->timezone = 'Europe/Prague';
108 $tz = get_user_timezone();
109 $this->assertSame('Europe/Prague', $tz);
111 $tz = get_user_timezone(99);
112 $this->assertSame('Europe/Prague', $tz);
113 $tz = get_user_timezone('99');
114 $this->assertSame('Europe/Prague', $tz);
116 $tz = get_user_timezone('Europe/Berlin');
117 $this->assertSame('Europe/Berlin', $tz);
119 // Server and user timezone not set.
121 $this->setTimezone('99', 'Pacific/Auckland');
122 $USER->timezone = '99';
124 $tz = get_user_timezone();
125 $this->assertSame(99.0, $tz);
127 $tz = get_user_timezone(99);
128 $this->assertSame(99.0, $tz);
129 $tz = get_user_timezone('99');
130 $this->assertSame(99.0, $tz);
132 $tz = get_user_timezone('Europe/Berlin');
133 $this->assertSame('Europe/Berlin', $tz);
136 public function test_get_timezone_offset() {
137 // This is a useless function, the timezone offset may be changing!
138 $this->assertSame(60 * 60 * -5, get_timezone_offset('America/New_York'));
139 $this->assertDebuggingCalled();
140 $this->assertSame(60 * 60 * -1, get_timezone_offset(-1));
141 $this->assertSame(60 * 60 * 1, get_timezone_offset(1));
142 $this->assertSame(60 * 60 * 1, get_timezone_offset('Europe/Prague'));
143 $this->assertSame(60 * 60 * 8, get_timezone_offset('Australia/Perth'));
144 $this->assertSame(60 * 60 * 12, get_timezone_offset('Pacific/Auckland'));
146 // Known half an hour offsets.
147 $this->assertEquals(60 * 60 * -4.5, get_timezone_offset('-4.5'));
148 $this->assertEquals(60 * 60 * -4.5, get_timezone_offset('America/Caracas'));
149 $this->assertEquals(60 * 60 * 4.5, get_timezone_offset('4.5'));
150 $this->assertEquals(60 * 60 * 4.5, get_timezone_offset('Asia/Kabul'));
151 $this->assertEquals(60 * 60 * 5.5, get_timezone_offset('5.5'));
152 $this->assertEquals(60 * 60 * 5.5, get_timezone_offset('Asia/Kolkata'));
153 $this->assertEquals(60 * 60 * 6.5, get_timezone_offset('6.5'));
154 $this->assertEquals(60 * 60 * 6.5, get_timezone_offset('Asia/Rangoon'));
155 $this->assertEquals(60 * 60 * 9.5, get_timezone_offset('9.5'));
156 $this->assertEquals(60 * 60 * 9.5, get_timezone_offset('Australia/Darwin'));
157 $this->assertEquals(60 * 60 * 11.5, get_timezone_offset('11.5'));
158 $this->assertEquals(60 * 60 * 11.5, get_timezone_offset('Pacific/Norfolk'));
160 $this->resetDebugging();
163 public function test_get_user_timezone_offset() {
164 // This is a useless function, the timezone offset may be changing!
165 $this->assertSame(-5.0, get_user_timezone_offset('America/New_York'));
166 $this->assertDebuggingCalled();
167 $this->assertSame(-1.0, get_user_timezone_offset(-1));
168 $this->assertSame(1.0, get_user_timezone_offset(1));
169 $this->assertSame(1.0, get_user_timezone_offset('Europe/Prague'));
170 $this->assertSame(8.0, get_user_timezone_offset('Australia/Perth'));
171 $this->assertSame(12.0, get_user_timezone_offset('Pacific/Auckland'));
173 $this->resetDebugging();
176 public function test_dst_offset_on() {
177 $time = gmmktime(1, 1, 1, 3, 1, 2015);
178 $this->assertSame(3600, dst_offset_on($time, 'Pacific/Auckland'));
179 $this->assertSame(0, dst_offset_on($time, 'Australia/Perth'));
180 $this->assertSame(1800, dst_offset_on($time, 'Australia/Lord_Howe'));
181 $this->assertSame(0, dst_offset_on($time, 'Europe/Prague'));
182 $this->assertSame(0, dst_offset_on($time, 'America/New_York'));
184 $time = gmmktime(1, 1, 1, 5, 1, 2015);
185 $this->assertSame(0, dst_offset_on($time, 'Pacific/Auckland'));
186 $this->assertSame(0, dst_offset_on($time, 'Australia/Perth'));
187 $this->assertSame(0, dst_offset_on($time, 'Australia/Lord_Howe'));
188 $this->assertSame(3600, dst_offset_on($time, 'Europe/Prague'));
189 $this->assertSame(3600, dst_offset_on($time, 'America/New_York'));
192 public function test_make_timestamp() {
193 global $CFG;
195 $this->resetAfterTest();
197 // There are quite a lot of problems, let's pick some less problematic zones for now.
198 $timezones = array('Europe/Prague', 'Europe/London', 'Australia/Perth', 'Pacific/Auckland', 'America/New_York', '99');
200 $dates = array(
201 array(2, 1, 0, 40, 40),
202 array(4, 3, 0, 30, 22),
203 array(9, 5, 0, 20, 19),
204 array(11, 28, 0, 10, 45),
206 $years = array(1999, 2009, 2014, 2018);
208 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
209 foreach ($timezones as $tz) {
210 foreach ($years as $year) {
211 foreach ($dates as $date) {
212 $result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
213 $expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
214 $expected->setDate($year, $date[0], $date[1]);
215 $expected->setTime($date[2], $date[3], $date[4]);
216 $this->assertSame($expected->getTimestamp(), $result,
217 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
222 $this->setTimezone('99', 'Pacific/Auckland');
223 foreach ($timezones as $tz) {
224 foreach ($years as $year) {
225 foreach ($dates as $date) {
226 $result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
227 $expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
228 $expected->setDate($year, $date[0], $date[1]);
229 $expected->setTime($date[2], $date[3], $date[4]);
230 $this->assertSame($expected->getTimestamp(), $result,
231 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
237 public function test_usergetdate() {
238 global $CFG;
240 $this->resetAfterTest();
242 // There are quite a lot of problems, let's pick some less problematic zones for now.
243 $timezones = array('Europe/Prague', 'Europe/London', 'Australia/Perth', 'Pacific/Auckland', 'America/New_York', '99');
245 $dates = array(
246 array(2, 1, 0, 40, 40),
247 array(4, 3, 0, 30, 22),
248 array(9, 5, 0, 20, 19),
249 array(11, 28, 0, 10, 45),
251 $years = array(1999, 2009, 2014, 2018);
253 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
254 foreach ($timezones as $tz) {
255 foreach ($years as $year) {
256 foreach ($dates as $date) {
257 $expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
258 $expected->setDate($year, $date[0], $date[1]);
259 $expected->setTime($date[2], $date[3], $date[4]);
260 $result = usergetdate($expected->getTimestamp(), $tz);
261 unset($result[0]); // Extra introduced by getdate().
262 $ex = array(
263 'seconds' => $date[4],
264 'minutes' => $date[3],
265 'hours' => $date[2],
266 'mday' => $date[1],
267 'wday' => (int)$expected->format('w'),
268 'mon' => $date[0],
269 'year' => $year,
270 'yday' => (int)$expected->format('z'),
271 'weekday' => $expected->format('l'),
272 'month' => $expected->format('F'),
274 $this->assertSame($ex, $result,
275 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
280 $this->setTimezone('99', 'Pacific/Auckland');
281 foreach ($timezones as $tz) {
282 foreach ($years as $year) {
283 foreach ($dates as $date) {
284 $expected = new DateTime('now', new DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
285 $expected->setDate($year, $date[0], $date[1]);
286 $expected->setTime($date[2], $date[3], $date[4]);
287 $result = usergetdate($expected->getTimestamp(), $tz);
288 unset($result[0]); // Extra introduced by getdate().
289 $ex = array(
290 'seconds' => $date[4],
291 'minutes' => $date[3],
292 'hours' => $date[2],
293 'mday' => $date[1],
294 'wday' => (int)$expected->format('w'),
295 'mon' => $date[0],
296 'year' => $year,
297 'yday' => (int)$expected->format('z'),
298 'weekday' => $expected->format('l'),
299 'month' => $expected->format('F'),
301 $this->assertSame($ex, $result,
302 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
308 public function test_userdate() {
309 global $CFG;
311 $this->resetAfterTest();
313 $dates = array(
314 array(2, 1, 0, 40, 40),
315 array(4, 3, 0, 30, 22),
316 array(9, 5, 0, 20, 19),
317 array(11, 28, 0, 10, 45),
319 $years = array(1999, 2009, 2014, 2018);
321 $users = array();
322 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 99));
323 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Europe/Prague'));
324 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Pacific/Auckland'));
325 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Australia/Perth'));
326 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'America/New_York'));
328 $format = get_string('strftimedaydatetime', 'langconfig');
330 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
331 foreach ($years as $year) {
332 foreach ($dates as $date) {
333 $expected = new DateTime('now', new DateTimeZone('UTC'));
334 $expected->setDate($year, $date[0], $date[1]);
335 $expected->setTime($date[2], $date[3], $date[4]);
337 foreach ($users as $user) {
338 $this->setUser($user);
339 $expected->setTimezone(new DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone)));
340 $result = userdate($expected->getTimestamp(), '', 99, false, false);
341 date_default_timezone_set($expected->getTimezone()->getName());
342 $ex = strftime($format, $expected->getTimestamp());
343 date_default_timezone_set($CFG->timezone);
344 $this->assertSame($ex, $result);
350 public function test_usertime() {
351 // This is a useless bad hack, it needs to be completely eliminated.
353 $time = gmmktime(1, 1, 1, 3, 1, 2015);
354 $this->assertSame($time - (60 * 60 * 1), usertime($time, '1'));
355 $this->assertSame($time - (60 * 60 * -1), usertime($time, '-1'));
356 $this->assertSame($time - (60 * 60 * 1), usertime($time, 'Europe/Prague'));
357 $this->assertSame($time - (60 * 60 * 8), usertime($time, 'Australia/Perth'));
358 $this->assertSame($time - (60 * 60 * 12), usertime($time, 'Pacific/Auckland'));
359 $this->assertSame($time - (60 * 60 * -5), usertime($time, 'America/New_York'));
361 $time = gmmktime(1, 1, 1, 5, 1, 2015);
362 $this->assertSame($time - (60 * 60 * 1), usertime($time, '1'));
363 $this->assertSame($time - (60 * 60 * -1), usertime($time, '-1'));
364 $this->assertSame($time - (60 * 60 * 1), usertime($time, 'Europe/Prague'));
365 $this->assertSame($time - (60 * 60 * 8), usertime($time, 'Australia/Perth'));
366 $this->assertSame($time - (60 * 60 * 12), usertime($time, 'Pacific/Auckland'));
367 $this->assertSame($time - (60 * 60 * -5), usertime($time, 'America/New_York'));
370 public function test_usertimezone() {
371 global $USER;
372 $this->resetAfterTest();
374 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
376 $USER->timezone = 'Europe/Prague';
377 $this->assertSame('Europe/Prague', usertimezone());
379 $USER->timezone = '1';
380 $this->assertSame('UTC+1', usertimezone());
382 $USER->timezone = '0';
383 $this->assertSame('UTC', usertimezone());
385 $USER->timezone = '99';
386 $this->assertSame('Pacific/Auckland', usertimezone());
388 $USER->timezone = '99';
389 $this->assertSame('Europe/Berlin', usertimezone('Europe/Berlin'));
391 $USER->timezone = '99';
392 $this->assertSame('Pacific/Auckland', usertimezone('99'));
394 $USER->timezone = 'Europe/Prague';
395 $this->assertSame('Europe/Prague', usertimezone('99'));