Merge branch 'MDL-75553-311' of https://github.com/junpataleta/moodle into MOODLE_311...
[moodle.git] / lib / tests / date_legacy_test.php
blob542a02bd8b8d5a5e3fbeb1b7166e009d01d05962
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 namespace core;
19 /**
20 * Tests legacy Moodle date/time functions.
22 * @package core
23 * @copyright 2015 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * @author Petr Skoda <petr.skoda@totaralms.com>
27 class date_legacy_test extends \advanced_testcase {
28 public function test_settings() {
29 global $CFG;
30 $this->resetAfterTest();
32 $this->assertNotEmpty($CFG->timezone);
34 $this->assertSame('99', $CFG->forcetimezone);
36 $user = $this->getDataGenerator()->create_user();
37 $this->assertSame('99', $user->timezone);
40 public function test_get_user_timezone() {
41 global $CFG, $USER;
43 $this->resetAfterTest();
45 $user = $this->getDataGenerator()->create_user();
46 $this->setUser($user);
48 // All set to something.
50 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
51 $USER->timezone = 'Europe/Prague';
53 $tz = get_user_timezone();
54 $this->assertSame('Europe/Prague', $tz);
56 $tz = get_user_timezone(99);
57 $this->assertSame('Europe/Prague', $tz);
58 $tz = get_user_timezone('99');
59 $this->assertSame('Europe/Prague', $tz);
61 $tz = get_user_timezone('Europe/Berlin');
62 $this->assertSame('Europe/Berlin', $tz);
64 // User timezone not set.
66 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
67 $USER->timezone = '99';
69 $tz = get_user_timezone();
70 $this->assertSame('Pacific/Auckland', $tz);
72 $tz = get_user_timezone(99);
73 $this->assertSame('Pacific/Auckland', $tz);
74 $tz = get_user_timezone('99');
75 $this->assertSame('Pacific/Auckland', $tz);
77 $tz = get_user_timezone('Europe/Berlin');
78 $this->assertSame('Europe/Berlin', $tz);
80 // Server timezone not set.
82 $this->setTimezone('99', 'Pacific/Auckland');
83 $USER->timezone = 'Europe/Prague';
85 $tz = get_user_timezone();
86 $this->assertSame('Europe/Prague', $tz);
88 $tz = get_user_timezone(99);
89 $this->assertSame('Europe/Prague', $tz);
90 $tz = get_user_timezone('99');
91 $this->assertSame('Europe/Prague', $tz);
93 $tz = get_user_timezone('Europe/Berlin');
94 $this->assertSame('Europe/Berlin', $tz);
96 // Server and user timezone not set.
98 $this->setTimezone('99', 'Pacific/Auckland');
99 $USER->timezone = '99';
101 $tz = get_user_timezone();
102 $this->assertSame(99.0, $tz);
104 $tz = get_user_timezone(99);
105 $this->assertSame(99.0, $tz);
106 $tz = get_user_timezone('99');
107 $this->assertSame(99.0, $tz);
109 $tz = get_user_timezone('Europe/Berlin');
110 $this->assertSame('Europe/Berlin', $tz);
113 public function test_dst_offset_on() {
114 $time = gmmktime(1, 1, 1, 3, 1, 2015);
115 $this->assertSame(3600, dst_offset_on($time, 'Pacific/Auckland'));
116 $this->assertSame(0, dst_offset_on($time, 'Australia/Perth'));
117 $this->assertSame(1800, dst_offset_on($time, 'Australia/Lord_Howe'));
118 $this->assertSame(0, dst_offset_on($time, 'Europe/Prague'));
119 $this->assertSame(0, dst_offset_on($time, 'America/New_York'));
121 $time = gmmktime(1, 1, 1, 5, 1, 2015);
122 $this->assertSame(0, dst_offset_on($time, 'Pacific/Auckland'));
123 $this->assertSame(0, dst_offset_on($time, 'Australia/Perth'));
124 $this->assertSame(0, dst_offset_on($time, 'Australia/Lord_Howe'));
125 $this->assertSame(3600, dst_offset_on($time, 'Europe/Prague'));
126 $this->assertSame(3600, dst_offset_on($time, 'America/New_York'));
129 public function test_make_timestamp() {
130 global $CFG;
132 $this->resetAfterTest();
134 // There are quite a lot of problems, let's pick some less problematic zones for now.
135 $timezones = array('Europe/Prague', 'Europe/London', 'Australia/Perth', 'Pacific/Auckland', 'America/New_York', '99');
137 $dates = array(
138 array(2, 1, 0, 40, 40),
139 array(4, 3, 0, 30, 22),
140 array(9, 5, 0, 20, 19),
141 array(11, 28, 0, 10, 45),
143 $years = array(1999, 2009, 2014, 2018);
145 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
146 foreach ($timezones as $tz) {
147 foreach ($years as $year) {
148 foreach ($dates as $date) {
149 $result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
150 $expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
151 $expected->setDate($year, $date[0], $date[1]);
152 $expected->setTime($date[2], $date[3], $date[4]);
153 $this->assertSame($expected->getTimestamp(), $result,
154 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
159 $this->setTimezone('99', 'Pacific/Auckland');
160 foreach ($timezones as $tz) {
161 foreach ($years as $year) {
162 foreach ($dates as $date) {
163 $result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
164 $expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
165 $expected->setDate($year, $date[0], $date[1]);
166 $expected->setTime($date[2], $date[3], $date[4]);
167 $this->assertSame($expected->getTimestamp(), $result,
168 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
174 public function test_usergetdate() {
175 global $CFG;
177 $this->resetAfterTest();
179 // There are quite a lot of problems, let's pick some less problematic zones for now.
180 $timezones = array('Europe/Prague', 'Europe/London', 'Australia/Perth', 'Pacific/Auckland', 'America/New_York', '99');
182 $dates = array(
183 array(2, 1, 0, 40, 40),
184 array(4, 3, 0, 30, 22),
185 array(9, 5, 0, 20, 19),
186 array(11, 28, 0, 10, 45),
188 $years = array(1999, 2009, 2014, 2018);
190 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
191 foreach ($timezones as $tz) {
192 foreach ($years as $year) {
193 foreach ($dates as $date) {
194 $expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
195 $expected->setDate($year, $date[0], $date[1]);
196 $expected->setTime($date[2], $date[3], $date[4]);
197 $result = usergetdate($expected->getTimestamp(), $tz);
198 unset($result[0]); // Extra introduced by getdate().
199 $ex = array(
200 'seconds' => $date[4],
201 'minutes' => $date[3],
202 'hours' => $date[2],
203 'mday' => $date[1],
204 'wday' => (int)$expected->format('w'),
205 'mon' => $date[0],
206 'year' => $year,
207 'yday' => (int)$expected->format('z'),
208 'weekday' => $expected->format('l'),
209 'month' => $expected->format('F'),
211 $this->assertSame($ex, $result,
212 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
217 $this->setTimezone('99', 'Pacific/Auckland');
218 foreach ($timezones as $tz) {
219 foreach ($years as $year) {
220 foreach ($dates as $date) {
221 $expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
222 $expected->setDate($year, $date[0], $date[1]);
223 $expected->setTime($date[2], $date[3], $date[4]);
224 $result = usergetdate($expected->getTimestamp(), $tz);
225 unset($result[0]); // Extra introduced by getdate().
226 $ex = array(
227 'seconds' => $date[4],
228 'minutes' => $date[3],
229 'hours' => $date[2],
230 'mday' => $date[1],
231 'wday' => (int)$expected->format('w'),
232 'mon' => $date[0],
233 'year' => $year,
234 'yday' => (int)$expected->format('z'),
235 'weekday' => $expected->format('l'),
236 'month' => $expected->format('F'),
238 $this->assertSame($ex, $result,
239 'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
245 public function test_userdate() {
246 global $CFG;
248 $this->resetAfterTest();
250 $dates = array(
251 array(2, 1, 0, 40, 40),
252 array(4, 3, 0, 30, 22),
253 array(9, 5, 0, 20, 19),
254 array(11, 28, 0, 10, 45),
256 $years = array(1999, 2009, 2014, 2018);
258 $users = array();
259 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 99));
260 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Europe/Prague'));
261 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Pacific/Auckland'));
262 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Australia/Perth'));
263 $users[] = $this->getDataGenerator()->create_user(array('timezone' => 'America/New_York'));
265 $format = get_string('strftimedaydatetime', 'langconfig');
267 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
268 foreach ($years as $year) {
269 foreach ($dates as $date) {
270 $expected = new \DateTime('now', new \DateTimeZone('UTC'));
271 $expected->setDate($year, $date[0], $date[1]);
272 $expected->setTime($date[2], $date[3], $date[4]);
274 foreach ($users as $user) {
275 $this->setUser($user);
276 $expected->setTimezone(new \DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone)));
277 $result = userdate($expected->getTimestamp(), '', 99, false, false);
278 date_default_timezone_set($expected->getTimezone()->getName());
279 $ex = strftime($format, $expected->getTimestamp());
280 date_default_timezone_set($CFG->timezone);
281 $this->assertSame($ex, $result);
287 public function test_usertime() {
288 // This is a useless bad hack, it needs to be completely eliminated.
290 $time = gmmktime(1, 1, 1, 3, 1, 2015);
291 $this->assertSame($time - (60 * 60 * 1), usertime($time, '1'));
292 $this->assertSame($time - (60 * 60 * -1), usertime($time, '-1'));
293 $this->assertSame($time - (60 * 60 * 1), usertime($time, 'Europe/Prague'));
294 $this->assertSame($time - (60 * 60 * 8), usertime($time, 'Australia/Perth'));
295 $this->assertSame($time - (60 * 60 * 12), usertime($time, 'Pacific/Auckland'));
296 $this->assertSame($time - (60 * 60 * -5), usertime($time, 'America/New_York'));
298 $time = gmmktime(1, 1, 1, 5, 1, 2015);
299 $this->assertSame($time - (60 * 60 * 1), usertime($time, '1'));
300 $this->assertSame($time - (60 * 60 * -1), usertime($time, '-1'));
301 $this->assertSame($time - (60 * 60 * 1), usertime($time, 'Europe/Prague'));
302 $this->assertSame($time - (60 * 60 * 8), usertime($time, 'Australia/Perth'));
303 $this->assertSame($time - (60 * 60 * 12), usertime($time, 'Pacific/Auckland'));
304 $this->assertSame($time - (60 * 60 * -5), usertime($time, 'America/New_York'));
307 public function test_usertimezone() {
308 global $USER;
309 $this->resetAfterTest();
311 $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
313 $USER->timezone = 'Europe/Prague';
314 $this->assertSame('Europe/Prague', usertimezone());
316 $USER->timezone = '1';
317 $this->assertSame('UTC+1', usertimezone());
319 $USER->timezone = '0';
320 $this->assertSame('UTC', usertimezone());
322 $USER->timezone = '99';
323 $this->assertSame('Pacific/Auckland', usertimezone());
325 $USER->timezone = '99';
326 $this->assertSame('Europe/Berlin', usertimezone('Europe/Berlin'));
328 $USER->timezone = '99';
329 $this->assertSame('Pacific/Auckland', usertimezone('99'));
331 $USER->timezone = 'Europe/Prague';
332 $this->assertSame('Europe/Prague', usertimezone('99'));
334 // When passed an unknown non-whole hour TZ, verify we round to closest
335 // hour. (Possible for legacy reasons when old timezones go away).
336 $USER->timezone = '-9.23';
337 $this->assertSame('UTC-9', usertimezone('99'));