1<?php
2/**
3 * CakeTimeTest file
4 *
5 * CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
6 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7 *
8 * Licensed under The MIT License
9 * For full copyright and license information, please see the LICENSE.txt
10 * Redistributions of files must retain the above copyright notice
11 *
12 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13 * @link          https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14 * @package       Cake.Test.Case.View.Helper
15 * @since         CakePHP(tm) v 1.2.0.4206
16 * @license       https://opensource.org/licenses/mit-license.php MIT License
17 */
18
19App::uses('CakeTime', 'Utility');
20
21/**
22 * CakeTimeTest class
23 *
24 * @package       Cake.Test.Case.View.Helper
25 */
26class CakeTimeTest extends CakeTestCase {
27
28/**
29 * Default system timezone identifier
30 *
31 * @var string
32 */
33	protected $_systemTimezoneIdentifier = null;
34
35/**
36 * setUp method
37 *
38 * @return void
39 */
40	public function setUp() {
41		parent::setUp();
42		$this->Time = new CakeTime();
43		$this->_systemTimezoneIdentifier = date_default_timezone_get();
44		Configure::write('Config.language', 'eng');
45	}
46
47/**
48 * tearDown method
49 *
50 * @return void
51 */
52	public function tearDown() {
53		parent::tearDown();
54		unset($this->Time);
55		$this->_restoreSystemTimezone();
56	}
57
58/**
59 * Restored the original system timezone
60 *
61 * @param string $timezoneIdentifier Timezone string
62 * @return void
63 */
64	protected function _restoreSystemTimezone() {
65		date_default_timezone_set($this->_systemTimezoneIdentifier);
66	}
67
68/**
69 * testToQuarter method
70 *
71 * @return void
72 */
73	public function testToQuarter() {
74		$result = $this->Time->toQuarter('2007-12-25');
75		$this->assertSame(4, $result);
76
77		$result = $this->Time->toQuarter('2007-9-25');
78		$this->assertSame(3, $result);
79
80		$result = $this->Time->toQuarter('2007-3-25');
81		$this->assertSame(1, $result);
82
83		$result = $this->Time->toQuarter('2007-3-25', true);
84		$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
85
86		$result = $this->Time->toQuarter('2007-5-25', true);
87		$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
88
89		$result = $this->Time->toQuarter('2007-8-25', true);
90		$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
91
92		$result = $this->Time->toQuarter('2007-12-25', true);
93		$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
94	}
95
96/**
97 * provider for timeAgoInWords() tests
98 *
99 * @return array
100 */
101	public static function timeAgoProvider() {
102		return array(
103			array('-12 seconds', '12 seconds ago'),
104			array('-12 minutes', '12 minutes ago'),
105			array('-2 hours', '2 hours ago'),
106			array('-1 day', '1 day ago'),
107			array('-2 days', '2 days ago'),
108			array('-2 days -3 hours', '2 days, 3 hours ago'),
109			array('-1 week', '1 week ago'),
110			array('-2 weeks -2 days', '2 weeks, 2 days ago'),
111			array('+1 week', 'in 1 week'),
112			array('+1 week 1 day', 'in 1 week, 1 day'),
113			array('+2 weeks 2 day', 'in 2 weeks, 2 days'),
114			array('2007-9-24', 'on 24/9/07'),
115			array('now', 'just now'),
116		);
117	}
118
119/**
120 * testTimeAgoInWords method
121 *
122 * @dataProvider timeAgoProvider
123 * @return void
124 */
125	public function testTimeAgoInWords($input, $expected) {
126		$result = $this->Time->timeAgoInWords($input);
127		$this->assertEquals($expected, $result);
128	}
129
130/**
131 * provider for timeAgo with an end date.
132 *
133 * @return void
134 */
135	public function timeAgoEndProvider() {
136		return array(
137			array(
138				'+4 months +2 weeks +3 days',
139				'in 4 months, 2 weeks, 3 days',
140				'8 years'
141			),
142			array(
143				'+4 months +2 weeks +1 day',
144				'in 4 months, 2 weeks, 1 day',
145				'8 years'
146			),
147			array(
148				'+3 months +2 weeks',
149				'in 3 months, 2 weeks',
150				'8 years'
151			),
152			array(
153				'+3 months +2 weeks +1 day',
154				'in 3 months, 2 weeks, 1 day',
155				'8 years'
156			),
157			array(
158				'+1 months +1 week +1 day',
159				'in 1 month, 1 week, 1 day',
160				'8 years'
161			),
162			array(
163				'+2 months +2 days',
164				'in 2 months, 2 days',
165				'on ' . date('j/n/y', strtotime('+2 months +2 days'))
166			),
167			array(
168				'+2 months +12 days',
169				'in 2 months, 1 week, 5 days',
170				'3 months'
171			),
172		);
173	}
174
175/**
176 * test the end option for timeAgoInWords
177 *
178 * @dataProvider timeAgoEndProvider
179 * @return void
180 */
181	public function testTimeAgoInWordsEnd($input, $expected, $end) {
182		$result = $this->Time->timeAgoInWords(
183			$input, array('end' => $end)
184		);
185		$this->assertEquals($expected, $result);
186	}
187
188/**
189 * test the custom string options for timeAgoInWords
190 *
191 * @return void
192 */
193	public function testTimeAgoInWordsCustomStrings() {
194		$result = $this->Time->timeAgoInWords(
195			strtotime('-8 years -4 months -2 weeks -3 days'),
196			array('relativeString' => 'at least %s ago', 'accuracy' => array('year' => 'year'), 'end' => '+10 years')
197		);
198		$expected = 'at least 8 years ago';
199		$this->assertEquals($expected, $result);
200
201		$result = $this->Time->timeAgoInWords(
202			strtotime('+8 years +4 months +2 weeks +3 days'),
203			array('relativeStringFuture' => 'not in the next %s', 'accuracy' => array('year' => 'year'), 'end' => '+10 years')
204		);
205		$expected = 'not in the next 8 years';
206		$this->assertEquals($expected, $result);
207
208		$result = $this->Time->timeAgoInWords(
209			strtotime('+4 months +2 weeks +3 days'),
210			array('absoluteString' => 'exactly on %s', 'accuracy' => array('year' => 'year'), 'end' => '+2 months')
211		);
212		$expected = 'exactly on ' . date('j/n/y', strtotime('+4 months +2 weeks +3 days'));
213		$this->assertEquals($expected, $result);
214	}
215
216/**
217 * Test the accuracy option for timeAgoInWords()
218 *
219 * @return void
220 */
221	public function testTimeAgoInWordsAccuracy() {
222		$result = $this->Time->timeAgoInWords(
223			strtotime('+8 years +4 months +2 weeks +3 days'),
224			array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
225		);
226		$expected = 'in 8 years';
227		$this->assertEquals($expected, $result);
228
229		$result = $this->Time->timeAgoInWords(
230			strtotime('+8 years +4 months +2 weeks +3 days'),
231			array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
232		);
233		$expected = 'in 8 years, 4 months';
234		$this->assertEquals($expected, $result);
235
236		$result = $this->Time->timeAgoInWords(
237			strtotime('+8 years +4 months +2 weeks +3 days'),
238			array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
239		);
240		$expected = 'in 8 years, 4 months, 2 weeks';
241		$this->assertEquals($expected, $result);
242
243		$result = $this->Time->timeAgoInWords(
244			strtotime('+8 years +4 months +2 weeks +3 days'),
245			array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
246		);
247		$expected = 'in 8 years, 4 months, 2 weeks, 3 days';
248		$this->assertEquals($expected, $result);
249
250		$result = $this->Time->timeAgoInWords(
251			strtotime('+1 years +5 weeks'),
252			array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
253		);
254		$expected = 'in 1 year';
255		$this->assertEquals($expected, $result);
256
257		$result = $this->Time->timeAgoInWords(
258			strtotime('+58 minutes'),
259			array('accuracy' => 'hour')
260		);
261		$expected = 'in about an hour';
262		$this->assertEquals($expected, $result);
263
264		$result = $this->Time->timeAgoInWords(
265			strtotime('+23 hours'),
266			array('accuracy' => 'day')
267		);
268		$expected = 'in about a day';
269		$this->assertEquals($expected, $result);
270	}
271
272/**
273 * Test the format option of timeAgoInWords() with date() and strftime compatible strings
274 *
275 * @return void
276 */
277	public function testTimeAgoInWordsWithFormat() {
278		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
279		$this->assertEquals('on 2007-09-25', $result);
280
281		$result = $this->Time->timeAgoInWords('2007-9-25', '%x');
282		$this->assertEquals('on ' . strftime('%x', strtotime('2007-9-25')), $result);
283
284		$result = $this->Time->timeAgoInWords(
285			strtotime('+2 weeks +2 days'),
286			'Y-m-d'
287		);
288		$this->assertRegExp('/^in 2 weeks, [1|2] day(s)?$/', $result);
289
290		$result = $this->Time->timeAgoInWords(
291			strtotime('+2 weeks +2 days'),
292			'%x'
293		);
294		$this->assertRegExp('/^in 2 weeks, [1|2] day(s)?$/', $result);
295
296		$result = $this->Time->timeAgoInWords(
297			strtotime('+2 months +2 days'),
298			array('end' => '1 month', 'format' => 'Y-m-d')
299		);
300		$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
301
302		$result = $this->Time->timeAgoInWords(
303			strtotime('+2 months +2 days'),
304			array('end' => '1 month', 'format' => '%x')
305		);
306		$this->assertEquals('on ' . strftime('%x', strtotime('+2 months +2 days')), $result);
307	}
308
309/**
310 * test timeAgoInWords() with negative values.
311 *
312 * @return void
313 */
314	public function testTimeAgoInWordsNegativeValues() {
315		$result = $this->Time->timeAgoInWords(
316			strtotime('-2 months -2 days'),
317			array('end' => '3 month')
318		);
319		$this->assertEquals('2 months, 2 days ago', $result);
320
321		$result = $this->Time->timeAgoInWords(
322			strtotime('-2 months -2 days'),
323			array('end' => '3 month')
324		);
325		$this->assertEquals('2 months, 2 days ago', $result);
326
327		$result = $this->Time->timeAgoInWords(
328			strtotime('-2 months -2 days'),
329			array('end' => '1 month', 'format' => 'Y-m-d')
330		);
331		$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
332
333		$result = $this->Time->timeAgoInWords(
334			strtotime('-2 years -5 months -2 days'),
335			array('end' => '3 years')
336		);
337		$this->assertEquals('2 years, 5 months, 2 days ago', $result);
338
339		$result = $this->Time->timeAgoInWords(
340			strtotime('-2 weeks -2 days'),
341			'Y-m-d'
342		);
343		$this->assertEquals('2 weeks, 2 days ago', $result);
344
345		$time = strtotime('-3 years -12 months');
346		$result = $this->Time->timeAgoInWords($time);
347		$expected = 'on ' . date('j/n/y', $time);
348		$this->assertEquals($expected, $result);
349
350		$result = $this->Time->timeAgoInWords(
351			strtotime('-1 month -1 week -6 days'),
352			array('end' => '1 year', 'accuracy' => array('month' => 'month'))
353		);
354		$this->assertEquals('1 month ago', $result);
355
356		$timestamp = strtotime('-1 years -2 weeks -3 days');
357		$result = $this->Time->timeAgoInWords(
358			$timestamp,
359			array('accuracy' => array('year' => 'year'))
360		);
361		$expected = 'on ' . date('j/n/y', $timestamp);
362		$this->assertEquals($expected, $result);
363
364		$result = $this->Time->timeAgoInWords(
365			strtotime('-13 months -5 days'),
366			array('end' => '2 years')
367		);
368		$this->assertEquals('1 year, 1 month, 5 days ago', $result);
369
370		$result = $this->Time->timeAgoInWords(
371			strtotime('-58 minutes'),
372			array('accuracy' => 'hour')
373		);
374		$this->assertEquals('about an hour ago', $result);
375
376		$result = $this->Time->timeAgoInWords(
377			strtotime('-23 hours'),
378			array('accuracy' => 'day')
379		);
380		$this->assertEquals('about a day ago', $result);
381	}
382
383/**
384 * testNice method
385 *
386 * @return void
387 */
388	public function testNice() {
389		$time = time() + 2 * DAY;
390		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
391
392		$time = time() - 2 * DAY;
393		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
394
395		$time = time();
396		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
397
398		$time = 0;
399		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
400
401		$time = null;
402		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
403
404		$time = time();
405		$this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
406		$this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
407
408		$this->Time->niceFormat = '%Y-%d-%m';
409		$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
410		$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);
411
412		CakeTime::$niceFormat = '%Y-%d-%m %H:%M';
413		$this->assertEquals(date('Y-d-m H:i', $time), $this->Time->nice($time));
414		$this->assertEquals('%Y-%d-%m %H:%M', $this->Time->niceFormat);
415
416		date_default_timezone_set('UTC');
417		$result = $this->Time->nice(null, 'America/New_York');
418		$expected = $this->Time->nice(time(), 'America/New_York');
419		$this->assertEquals(substr($expected, 0, -1), substr($result, 0, -1));
420
421		$this->_restoreSystemTimezone();
422	}
423
424	public function testNiceTimezoneConversion() {
425		date_default_timezone_set('Europe/Copenhagen'); // server timezone
426		$clientTimeZone = new DateTimeZone('Asia/Bangkok');
427		$clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone);
428		// Convert to UTC.
429		$actual = CakeTime::nice($clientDateTime, 'UTC', '%Y-%m-%d %H:%M:%S');
430		$clientDateTime->setTimezone(new DateTimeZone('UTC'));
431		$expected = $clientDateTime->format('Y-m-d H:i:s');
432		$this->assertEquals($expected, $actual);
433		$this->_restoreSystemTimezone();
434	}
435
436/**
437 * testNiceShort method
438 *
439 * @return void
440 */
441	public function testNiceShort() {
442		$time = time();
443		$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
444
445		$time = time() - DAY;
446		$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
447
448		$time = time() + DAY;
449		$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
450
451		$time = strtotime('+6 days');
452		$this->assertEquals('On ' . date('l F d, H:i', $time), $this->Time->niceShort($time));
453
454		$time = strtotime('-6 days');
455		$this->assertEquals(date('l F d, H:i', $time), $this->Time->niceShort($time));
456
457		date_default_timezone_set('Europe/London');
458		$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
459		$this->assertEquals('Jan 15th 2005, 11:00', $result);
460
461		date_default_timezone_set('UTC');
462		$result = $this->Time->niceShort(null, 'America/New_York');
463		$expected = $this->Time->niceShort(time(), 'America/New_York');
464		$this->assertEquals($expected, $result);
465
466		$this->_restoreSystemTimezone();
467	}
468
469/**
470 * testNiceShort translations
471 *
472 * @return void
473 */
474	public function testNiceShortI18n() {
475		$restore = setlocale(LC_ALL, 0);
476		setlocale(LC_ALL, 'es_ES');
477		$time = strtotime('2015-01-07 03:05:00');
478		$this->assertEquals('ene 7th 2015, 03:05', $this->Time->niceShort($time));
479		setlocale(LC_ALL, $restore);
480	}
481
482/**
483 * testDaysAsSql method
484 *
485 * @return void
486 */
487	public function testDaysAsSql() {
488		$begin = time();
489		$end = time() + DAY;
490		$field = 'my_field';
491		$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
492		$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
493	}
494
495/**
496 * testDayAsSql method
497 *
498 * @return void
499 */
500	public function testDayAsSql() {
501		$time = time();
502		$field = 'my_field';
503		$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
504		$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
505	}
506
507/**
508 * testToUnix method
509 *
510 * @return void
511 */
512	public function testToUnix() {
513		$this->assertEquals(time(), $this->Time->toUnix(time()));
514		$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
515		$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
516		$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
517		$this->assertEquals(false, $this->Time->toUnix(''));
518		$this->assertEquals(false, $this->Time->toUnix(null));
519	}
520
521/**
522 * testToServer method
523 *
524 * @return void
525 */
526	public function testToServer() {
527		date_default_timezone_set('Europe/Paris');
528
529		$time = time();
530		$this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
531
532		date_default_timezone_set('America/New_York');
533		$time = time();
534		date_default_timezone_set('Europe/Paris');
535		$result = $this->Time->toServer($time, 'America/New_York');
536		$this->assertEquals(date('Y-m-d H:i:s', $time), $result);
537
538		date_default_timezone_set('Europe/Paris');
539		$time = '2005-10-25 10:00:00';
540		$result = $this->Time->toServer($time);
541		$date = new DateTime($time, new DateTimeZone('UTC'));
542		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
543		$expected = $date->format('Y-m-d H:i:s');
544		$this->assertEquals($expected, $result);
545
546		$time = '2002-01-01 05:15:30';
547		$result = $this->Time->toServer($time, 'America/New_York');
548		$date = new DateTime($time, new DateTimeZone('America/New_York'));
549		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
550		$expected = $date->format('Y-m-d H:i:s');
551		$this->assertEquals($expected, $result);
552
553		$time = '2010-01-28T15:00:00+10:00';
554		$result = $this->Time->toServer($time, 'America/New_York');
555		$date = new DateTime($time);
556		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
557		$expected = $date->format('Y-m-d H:i:s');
558		$this->assertEquals($expected, $result);
559
560		$date = new DateTime(null, new DateTimeZone('America/New_York'));
561		$result = $this->Time->toServer($date, 'Pacific/Tahiti');
562		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
563		$expected = $date->format('Y-m-d H:i:s');
564		$this->assertEquals($expected, $result);
565
566		$this->_restoreSystemTimezone();
567
568		$time = time();
569		$result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
570		$expected = date('l jS \of F Y h:i:s A', $time);
571		$this->assertEquals($expected, $result);
572
573		$this->assertFalse($this->Time->toServer(time(), new CakeObject()));
574
575		date_default_timezone_set('UTC');
576
577		$serverTime = new DateTime('2012-12-11 14:15:20');
578
579		$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
580		foreach ($timezones as $timezone) {
581			$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
582			$tz = new DateTimeZone($timezone);
583			$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
584		}
585
586		date_default_timezone_set('UTC');
587		$date = new DateTime('now', new DateTimeZone('America/New_York'));
588
589		$result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
590		$date->setTimezone($this->Time->timezone());
591		$expected = $date->format('Y-m-d H:i:s');
592		$this->assertEquals($expected, $result);
593
594		$this->_restoreSystemTimezone();
595	}
596
597/**
598 * testToAtom method
599 *
600 * @return void
601 */
602	public function testToAtom() {
603		$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
604	}
605
606/**
607 * testToRss method
608 *
609 * @return void
610 */
611	public function testToRss() {
612		$date = '2012-08-12 12:12:45';
613		$time = strtotime($date);
614		$this->assertEquals(date('r', $time), $this->Time->toRss($time));
615
616		$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
617		foreach ($timezones as $timezone) {
618			$yourTimezone = new DateTimeZone($timezone);
619			$yourTime = new DateTime($date, $yourTimezone);
620			$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
621			$time = $yourTime->format('U');
622			$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone");
623			$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
624		}
625	}
626
627/**
628 * testFormat method
629 *
630 * @return void
631 */
632	public function testFormat() {
633		$format = 'D-M-Y';
634		$tz = date_default_timezone_get();
635		$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
636		foreach ($arr as $val) {
637			$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
638			$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
639		}
640
641		$result = $this->Time->format('Y-m-d', null, 'never');
642		$this->assertEquals('never', $result);
643
644		$result = $this->Time->format('Y-m-d', '');
645		$this->assertSame('', $result);
646
647		$result = $this->Time->format('Y-m-d', false);
648		$this->assertSame('', $result);
649
650		$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
651		$this->assertEquals('13-01-2012', $result);
652
653		$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
654		$this->assertEquals('invalid', $result);
655
656		$result = $this->Time->format('0000-00-00', '%d-%m-%Y', 'invalid');
657		$this->assertEquals('invalid', $result);
658	}
659
660/**
661 * testOfGmt method
662 *
663 * @return void
664 */
665	public function testGmt() {
666		$hour = 3;
667		$min = 4;
668		$sec = 2;
669		$month = 5;
670		$day = 14;
671		$year = 2007;
672		$time = mktime($hour, $min, $sec, $month, $day, $year);
673		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
674		$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
675
676		$hour = date('H');
677		$min = date('i');
678		$sec = date('s');
679		$month = date('m');
680		$day = date('d');
681		$year = date('Y');
682		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
683		$this->assertEquals($expected, $this->Time->gmt(null));
684	}
685
686/**
687 * testIsToday method
688 *
689 * @return void
690 */
691	public function testIsToday() {
692		$result = $this->Time->isToday('+1 day');
693		$this->assertFalse($result);
694		$result = $this->Time->isToday('+1 days');
695		$this->assertFalse($result);
696		$result = $this->Time->isToday('+0 day');
697		$this->assertTrue($result);
698		$result = $this->Time->isToday('-1 day');
699		$this->assertFalse($result);
700	}
701
702/**
703 * testIsFuture method
704 *
705 * @return void
706 */
707	public function testIsFuture() {
708		$this->assertTrue($this->Time->isFuture('+1 month'));
709		$this->assertTrue($this->Time->isFuture('+1 days'));
710		$this->assertTrue($this->Time->isFuture('+1 minute'));
711		$this->assertTrue($this->Time->isFuture('+1 second'));
712
713		$this->assertFalse($this->Time->isFuture('-1 second'));
714		$this->assertFalse($this->Time->isFuture('-1 day'));
715		$this->assertFalse($this->Time->isFuture('-1 week'));
716		$this->assertFalse($this->Time->isFuture('-1 month'));
717	}
718
719/**
720 * testIsPast method
721 *
722 * @return void
723 */
724	public function testIsPast() {
725		$this->assertFalse($this->Time->isPast('+1 month'));
726		$this->assertFalse($this->Time->isPast('+1 days'));
727		$this->assertFalse($this->Time->isPast('+1 minute'));
728		$this->assertFalse($this->Time->isPast('+1 second'));
729
730		$this->assertTrue($this->Time->isPast('-1 second'));
731		$this->assertTrue($this->Time->isPast('-1 day'));
732		$this->assertTrue($this->Time->isPast('-1 week'));
733		$this->assertTrue($this->Time->isPast('-1 month'));
734	}
735
736/**
737 * testIsThisWeek method
738 *
739 * @return void
740 */
741	public function testIsThisWeek() {
742		// A map of days which goes from -1 day of week to +1 day of week
743		$map = array(
744			'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
745			'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
746			'Sun' => array(-7, 1)
747		);
748		$days = $map[date('D')];
749
750		for ($day = $days[0] + 1; $day < $days[1]; $day++) {
751			$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
752		}
753		$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
754		$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
755	}
756
757/**
758 * testIsThisMonth method
759 *
760 * @return void
761 */
762	public function testIsThisMonth() {
763		$result = $this->Time->isThisMonth('+0 day');
764		$this->assertTrue($result);
765		$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
766		$this->assertTrue($result);
767		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
768		$this->assertFalse($result);
769		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
770		$this->assertFalse($result);
771	}
772
773/**
774 * testIsThisYear method
775 *
776 * @return void
777 */
778	public function testIsThisYear() {
779		$result = $this->Time->isThisYear('+0 day');
780		$this->assertTrue($result);
781		$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
782		$this->assertTrue($result);
783	}
784
785/**
786 * testWasYesterday method
787 *
788 * @return void
789 */
790	public function testWasYesterday() {
791		$result = $this->Time->wasYesterday('+1 day');
792		$this->assertFalse($result);
793		$result = $this->Time->wasYesterday('+1 days');
794		$this->assertFalse($result);
795		$result = $this->Time->wasYesterday('+0 day');
796		$this->assertFalse($result);
797		$result = $this->Time->wasYesterday('-1 day');
798		$this->assertTrue($result);
799		$result = $this->Time->wasYesterday('-1 days');
800		$this->assertTrue($result);
801		$result = $this->Time->wasYesterday('-2 days');
802		$this->assertFalse($result);
803	}
804
805/**
806 * testIsTomorrow method
807 *
808 * @return void
809 */
810	public function testIsTomorrow() {
811		$result = $this->Time->isTomorrow('+1 day');
812		$this->assertTrue($result);
813		$result = $this->Time->isTomorrow('+1 days');
814		$this->assertTrue($result);
815		$result = $this->Time->isTomorrow('+0 day');
816		$this->assertFalse($result);
817		$result = $this->Time->isTomorrow('-1 day');
818		$this->assertFalse($result);
819	}
820
821/**
822 * testWasWithinLast method
823 *
824 * @return void
825 */
826	public function testWasWithinLast() {
827		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
828		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
829		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
830		$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
831		$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
832		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
833		$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
834		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
835
836		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
837		$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
838		$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
839		$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
840		$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
841		$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
842		$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
843		$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
844
845		$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
846		$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
847		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
848		$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
849		$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
850		$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
851
852		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
853		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
854		$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
855		$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
856		$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
857		$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
858		$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
859
860		$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
861		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
862		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
863		$this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
864	}
865
866/**
867 * testWasWithinLast method
868 *
869 * @return void
870 */
871	public function testIsWithinNext() {
872		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
873		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
874		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
875		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
876		$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
877		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
878		$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
879		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
880
881		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
882		$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
883		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
884		$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
885		$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
886		$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
887		$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
888		$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
889
890		$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
891		$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
892		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
893		$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
894		$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
895		$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
896
897		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
898		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
899		$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
900		$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
901		$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
902		$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
903		$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
904
905		$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
906		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 hour'));
907		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 minute'));
908		$this->assertFalse($this->Time->isWithinNext('1   ', '-23 hours -59 minutes -59 seconds'));
909
910		$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
911		$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
912	}
913
914/**
915 * testUserOffset method
916 *
917 * @return void
918 */
919	public function testUserOffset() {
920		$timezoneServer = new DateTimeZone(date_default_timezone_get());
921		$timeServer = new DateTime('now', $timezoneServer);
922		$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
923
924		$expected = time();
925		$result = $this->Time->fromString(time(), $yourTimezone);
926		$this->assertWithinMargin($expected, $result, 1);
927
928		$result = $this->Time->fromString(time(), $timezoneServer->getName());
929		$this->assertWithinMargin($expected, $result, 1);
930
931		$result = $this->Time->fromString(time(), $timezoneServer);
932		$this->assertWithinMargin($expected, $result, 1);
933
934		Configure::write('Config.timezone', $timezoneServer->getName());
935		$result = $this->Time->fromString(time());
936		$this->assertWithinMargin($expected, $result, 1);
937		Configure::delete('Config.timezone');
938	}
939
940/**
941 * test fromString()
942 *
943 * @return void
944 */
945	public function testFromString() {
946		$result = $this->Time->fromString('');
947		$this->assertFalse($result);
948
949		$result = $this->Time->fromString(0, 0);
950		$this->assertFalse($result);
951
952		$result = $this->Time->fromString('+1 hour');
953		$expected = strtotime('+1 hour');
954		$this->assertWithinMargin($expected, $result, 1);
955
956		$timezone = date('Z', time());
957		$result = $this->Time->fromString('+1 hour', $timezone);
958		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
959		$this->assertWithinMargin($expected, $result, 1);
960
961		$timezone = date_default_timezone_get();
962		$result = $this->Time->fromString('+1 hour', $timezone);
963		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
964		$this->assertWithinMargin($expected, $result, 1);
965
966		date_default_timezone_set('UTC');
967		$date = new DateTime('now', new DateTimeZone('Europe/London'));
968		$this->Time->fromString($date);
969		$this->assertEquals('Europe/London', $date->getTimeZone()->getName());
970
971		$this->_restoreSystemTimezone();
972	}
973
974/**
975 * test fromString() with a DateTime object as the dateString
976 *
977 * @return void
978 */
979	public function testFromStringWithDateTime() {
980		date_default_timezone_set('UTC');
981		$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
982		$result = $this->Time->fromString($date, 'UTC');
983		$date->setTimezone(new DateTimeZone('UTC'));
984		$expected = $date->format('U') + $date->getOffset();
985		$this->assertWithinMargin($expected, $result, 1);
986		$this->_restoreSystemTimezone();
987	}
988
989	public function testFromStringWithDateTimeAsia() {
990		date_default_timezone_set('Australia/Melbourne');
991		$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
992		$result = $this->Time->fromString($date, 'Asia/Kuwait');
993		$date->setTimezone(new DateTimeZone('Asia/Kuwait'));
994		$expected = $date->format('U') + $date->getOffset();
995		$this->assertWithinMargin($expected, $result, 1);
996		$this->_restoreSystemTimezone();
997	}
998
999	public function testFromStringTimezoneConversionToUTC() {
1000		date_default_timezone_set('Europe/Copenhagen'); // server timezone
1001		$clientTimeZone = new DateTimeZone('Asia/Bangkok');
1002		$clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone);
1003		// Convert to UTC.
1004		$actual = CakeTime::fromString($clientDateTime, 'UTC');
1005		$clientDateTime->setTimezone(new DateTimeZone('UTC'));
1006		$expected = $clientDateTime->getTimestamp() + $clientDateTime->getOffset(); // 1548903600
1007		$this->assertEquals($expected, $actual);
1008		$this->_restoreSystemTimezone();
1009	}
1010
1011	public function testFromStringUTCtoCopenhagen() {
1012		date_default_timezone_set('UTC'); // server timezone
1013		$clientTimeZone = new DateTimeZone('UTC');
1014		$clientDateTime = new DateTime('2012-01-01 10:00:00', $clientTimeZone);
1015		$actual = CakeTime::fromString($clientDateTime, 'Europe/Copenhagen');
1016		$clientDateTime->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1017		$expected = $clientDateTime->getTimestamp() + $clientDateTime->getOffset(); // 1325415600
1018		$this->assertEquals($expected, $actual);
1019		$this->_restoreSystemTimezone();
1020	}
1021
1022/**
1023 * Test that datetimes in the default timezone are not modified.
1024 *
1025 * @return void
1026 */
1027	public function testFromStringWithDateTimeNoConversion() {
1028		Configure::write('Config.timezone', date_default_timezone_get());
1029		$date = new DateTime('2013-04-09');
1030		$result = $this->Time->fromString($date);
1031		$this->assertEquals($result, $date->format('U'));
1032	}
1033
1034	public function testConvertToBangkok() {
1035		$serverTimeZoneName = 'Europe/Copenhagen';
1036		date_default_timezone_set($serverTimeZoneName);
1037
1038		$serverTimeZone = new DateTimeZone($serverTimeZoneName);
1039		$DateTime = new DateTime('2019-01-31 04:00:00', $serverTimeZone);
1040		$serverTimestamp = $DateTime->getTimestamp() + $DateTime->getOffset(); // 1548907200
1041
1042		$clientTimeZoneName = 'Asia/Bangkok';
1043		$clientTimeZone = new DateTimeZone($clientTimeZoneName);
1044		$DateTime->setTimezone($clientTimeZone);
1045		$expected = $DateTime->getTimestamp() + $DateTime->getOffset(); // 1548928800
1046
1047		$actual = CakeTime::convert($serverTimestamp, $clientTimeZoneName);
1048		$this->assertEquals($expected, $actual);
1049		$this->_restoreSystemTimezone();
1050	}
1051
1052/**
1053 * test converting time specifiers using a time definition localfe file
1054 *
1055 * @return void
1056 */
1057	public function testConvertSpecifiers() {
1058		App::build(array(
1059			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
1060		), App::RESET);
1061		Configure::write('Config.language', 'time_test');
1062		$time = strtotime('Thu Jan 14 11:43:39 2010');
1063
1064		$result = $this->Time->convertSpecifiers('%a', $time);
1065		$expected = 'jue';
1066		$this->assertEquals($expected, $result);
1067
1068		$result = $this->Time->convertSpecifiers('%A', $time);
1069		$expected = 'jueves';
1070		$this->assertEquals($expected, $result);
1071
1072		$result = $this->Time->convertSpecifiers('%c', $time);
1073		$expected = 'jue %d ene %Y %H:%M:%S %Z';
1074		$this->assertEquals($expected, $result);
1075
1076		$result = $this->Time->convertSpecifiers('%C', $time);
1077		$expected = '20';
1078		$this->assertEquals($expected, $result);
1079
1080		$result = $this->Time->convertSpecifiers('%D', $time);
1081		$expected = '%m/%d/%y';
1082		$this->assertEquals($expected, $result);
1083
1084		$result = $this->Time->convertSpecifiers('%b', $time);
1085		$expected = 'ene';
1086		$this->assertEquals($expected, $result);
1087
1088		$result = $this->Time->convertSpecifiers('%h', $time);
1089		$expected = 'ene';
1090		$this->assertEquals($expected, $result);
1091
1092		$result = $this->Time->convertSpecifiers('%B', $time);
1093		$expected = 'enero';
1094		$this->assertEquals($expected, $result);
1095
1096		$result = $this->Time->convertSpecifiers('%n', $time);
1097		$expected = "\n";
1098		$this->assertEquals($expected, $result);
1099
1100		$result = $this->Time->convertSpecifiers('%n', $time);
1101		$expected = "\n";
1102		$this->assertEquals($expected, $result);
1103
1104		$result = $this->Time->convertSpecifiers('%p', $time);
1105		$expected = 'AM';
1106		$this->assertEquals($expected, $result);
1107
1108		$result = $this->Time->convertSpecifiers('%P', $time);
1109		$expected = 'am';
1110		$this->assertEquals($expected, $result);
1111
1112		$result = $this->Time->convertSpecifiers('%r', $time);
1113		$expected = '%I:%M:%S AM';
1114		$this->assertEquals($expected, $result);
1115
1116		$result = $this->Time->convertSpecifiers('%R', $time);
1117		$expected = '11:43';
1118		$this->assertEquals($expected, $result);
1119
1120		$result = $this->Time->convertSpecifiers('%t', $time);
1121		$expected = "\t";
1122		$this->assertEquals($expected, $result);
1123
1124		$result = $this->Time->convertSpecifiers('%T', $time);
1125		$expected = '%H:%M:%S';
1126		$this->assertEquals($expected, $result);
1127
1128		$result = $this->Time->convertSpecifiers('%u', $time);
1129		$expected = 4;
1130		$this->assertEquals($expected, $result);
1131
1132		$result = $this->Time->convertSpecifiers('%x', $time);
1133		$expected = '%d/%m/%y';
1134		$this->assertEquals($expected, $result);
1135
1136		$result = $this->Time->convertSpecifiers('%X', $time);
1137		$expected = '%H:%M:%S';
1138		$this->assertEquals($expected, $result);
1139	}
1140
1141/**
1142 * test convert %e on Windows.
1143 *
1144 * @return void
1145 */
1146	public function testConvertPercentE() {
1147		$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run Windows tests on non-Windows OS.');
1148
1149		$time = strtotime('Thu Jan 14 11:43:39 2010');
1150		$result = $this->Time->convertSpecifiers('%e', $time);
1151		$expected = '14';
1152		$this->assertEquals($expected, $result);
1153
1154		$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
1155		$expected = ' 1';
1156		$this->assertEquals($expected, $result);
1157	}
1158
1159/**
1160 * test formatting dates taking in account preferred i18n locale file
1161 *
1162 * @return void
1163 */
1164	public function testI18nFormat() {
1165		App::build(array(
1166			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
1167		), App::RESET);
1168		Configure::write('Config.language', 'time_test');
1169
1170		$time = strtotime('Thu Jan 14 13:59:28 2010');
1171
1172		$result = $this->Time->i18nFormat($time);
1173		$expected = '14/01/10';
1174		$this->assertEquals($expected, $result);
1175
1176		$result = $this->Time->i18nFormat($time, '%c');
1177		$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
1178		$this->assertEquals($expected, $result);
1179
1180		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
1181		$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
1182		$this->assertEquals($expected, $result);
1183
1184		$time = strtotime('Wed Jan 13 13:59:28 2010');
1185
1186		$result = $this->Time->i18nFormat($time);
1187		$expected = '13/01/10';
1188		$this->assertEquals($expected, $result);
1189
1190		$result = $this->Time->i18nFormat($time, '%c');
1191		$expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
1192		$this->assertEquals($expected, $result);
1193
1194		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
1195		$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
1196		$this->assertEquals($expected, $result);
1197
1198		$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
1199		$expected = 'Date invalid';
1200		$this->assertEquals($expected, $result);
1201	}
1202
1203	public function testI18nFormatTimezoneConversionToUTC() {
1204		date_default_timezone_set('Europe/Copenhagen'); // server timezone
1205		$clientTimeZone = new DateTimeZone('Asia/Bangkok');
1206		$clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone);
1207		// Convert to UTC.
1208		$actual = CakeTime::i18nFormat($clientDateTime, '%Y-%m-%d %H:%M:%S', false, 'UTC');
1209		$clientDateTime->setTimezone(new DateTimeZone('UTC'));
1210		$expected = $clientDateTime->format('Y-m-d H:i:s');
1211		$this->assertEquals($expected, $actual);
1212		$this->_restoreSystemTimezone();
1213	}
1214
1215	public function testI18nFormatUTCtoCopenhagen() {
1216		date_default_timezone_set('UTC');
1217		$clientTimeZone = new DateTimeZone('UTC');
1218		$clientDateTime = new DateTime('2012-01-01 10:00:00', $clientTimeZone);
1219		$actual = CakeTime::i18nFormat($clientDateTime, '%Y-%m-%d %H:%M', false, 'Europe/Copenhagen');
1220		$clientDateTime->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1221		$expected = $clientDateTime->format('Y-m-d H:i');
1222		$this->assertEquals($expected, $actual);
1223	}
1224
1225/**
1226 * test new format() syntax which inverts first and second parameters
1227 *
1228 * @return void
1229 */
1230	public function testFormatNewSyntax() {
1231		$time = time();
1232		$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
1233		$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
1234	}
1235
1236/**
1237 * testListTimezones
1238 *
1239 * @return void
1240 */
1241	public function testListTimezones() {
1242		$this->skipIf(
1243			version_compare(PHP_VERSION, '5.4.0', '<='),
1244			'This test requires newer libicu which is in php5.4+'
1245		);
1246		$return = CakeTime::listTimezones();
1247		$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1248		$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
1249		$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
1250		$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
1251		$this->assertTrue(isset($return['UTC']['UTC']));
1252		$this->assertFalse(isset($return['Cuba']));
1253		$this->assertFalse(isset($return['US']));
1254
1255		$return = CakeTime::listTimezones('#^Asia/#');
1256		$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1257		$this->assertFalse(isset($return['Pacific']));
1258
1259		$return = CakeTime::listTimezones(null, null, array('abbr' => true));
1260		$this->assertTrue(isset($return['Asia']['Asia/Jakarta']));
1261		$this->assertEquals('Jakarta - WIB', $return['Asia']['Asia/Jakarta']);
1262		$this->assertEquals('Regina - CST', $return['America']['America/Regina']);
1263
1264		$return = CakeTime::listTimezones(null, null, array(
1265			'abbr' => true,
1266			'before' => ' (',
1267			'after' => ')',
1268		));
1269		$this->assertEquals('Jayapura (WIT)', $return['Asia']['Asia/Jayapura']);
1270		$this->assertEquals('Regina (CST)', $return['America']['America/Regina']);
1271
1272		$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
1273		$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
1274		$this->assertTrue(isset($return['Pacific/Tahiti']));
1275
1276		if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
1277			$return = CakeTime::listTimezones(DateTimeZone::ASIA);
1278			$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1279			$this->assertFalse(isset($return['Pacific']));
1280
1281			$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
1282			$this->assertTrue(isset($return['Pacific/Honolulu']));
1283			$this->assertFalse(isset($return['Asia/Bangkok']));
1284		}
1285	}
1286
1287/**
1288 * Tests that using CakeTime::format() with the correct sytax actually converts
1289 * from one timezone to the other correctly
1290 *
1291 * @return void
1292 */
1293	public function testCorrectTimezoneConversionAsString() {
1294		date_default_timezone_set('UTC');
1295		$date = '2012-01-01 10:00:00';
1296		$converted = CakeTime::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
1297		$expected = new DateTime($date);
1298		$expected->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1299		$this->assertEquals($expected->format('Y-m-d H:i'), $converted);
1300	}
1301
1302	public function testCorrectTimezoneConversionAsObject() {
1303		date_default_timezone_set('UTC');
1304		$clientTimeZone = new DateTimeZone('UTC');
1305		$date = '2012-01-01 10:00:00';
1306		$clientDateTime = new DateTime($date, $clientTimeZone);
1307		$converted = CakeTime::format($clientDateTime, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
1308		$clientDateTime->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1309		$expected = $clientDateTime->format('Y-m-d H:i');
1310		$this->assertEquals($expected, $converted);
1311	}
1312
1313	public function testFormatTimezoneConversionToUTC() {
1314		date_default_timezone_set('Europe/Copenhagen'); // server timezone
1315		$clientTimeZone = new DateTimeZone('Asia/Bangkok');
1316		$clientDateTime = new DateTime('2019-01-31 10:00:00', $clientTimeZone);
1317		// Convert to UTC.
1318		$actual = CakeTime::format($clientDateTime, '%Y-%m-%d %H:%M:%S', false, 'UTC');
1319		$clientDateTime->setTimezone(new DateTimeZone('UTC'));
1320		$expected = $clientDateTime->format('Y-m-d H:i:s');
1321		$this->assertEquals($expected, $actual);
1322		$this->_restoreSystemTimezone();
1323	}
1324
1325}
1326