統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Utility / CakeTimeTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (38.009 KB)

1
<?php
2
/**
3
 * CakeTimeTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
13
 * @link          http://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       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('CakeTime', 'Utility');
20

    
21
/**
22
 * CakeTimeTest class
23
 *
24
 * @package       Cake.Test.Case.View.Helper
25
 */
26
class 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
/**
425
 * testNiceShort method
426
 *
427
 * @return void
428
 */
429
        public function testNiceShort() {
430
                $time = time();
431
                $this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
432

    
433
                $time = time() - DAY;
434
                $this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
435

    
436
                $time = time() + DAY;
437
                $this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
438

    
439
                $time = strtotime('+6 days');
440
                $this->assertEquals('On ' . date('l F d, H:i', $time), $this->Time->niceShort($time));
441

    
442
                $time = strtotime('-6 days');
443
                $this->assertEquals(date('l F d, H:i', $time), $this->Time->niceShort($time));
444

    
445
                date_default_timezone_set('Europe/London');
446
                $result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
447
                $this->assertEquals('Jan 15th 2005, 11:00', $result);
448

    
449
                date_default_timezone_set('UTC');
450
                $result = $this->Time->niceShort(null, 'America/New_York');
451
                $expected = $this->Time->niceShort(time(), 'America/New_York');
452
                $this->assertEquals($expected, $result);
453

    
454
                $this->_restoreSystemTimezone();
455
        }
456

    
457
/**
458
 * testDaysAsSql method
459
 *
460
 * @return void
461
 */
462
        public function testDaysAsSql() {
463
                $begin = time();
464
                $end = time() + DAY;
465
                $field = 'my_field';
466
                $expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
467
                $this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
468
        }
469

    
470
/**
471
 * testDayAsSql method
472
 *
473
 * @return void
474
 */
475
        public function testDayAsSql() {
476
                $time = time();
477
                $field = 'my_field';
478
                $expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
479
                $this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
480
        }
481

    
482
/**
483
 * testToUnix method
484
 *
485
 * @return void
486
 */
487
        public function testToUnix() {
488
                $this->assertEquals(time(), $this->Time->toUnix(time()));
489
                $this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
490
                $this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
491
                $this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
492
                $this->assertEquals(false, $this->Time->toUnix(''));
493
                $this->assertEquals(false, $this->Time->toUnix(null));
494
        }
495

    
496
/**
497
 * testToServer method
498
 *
499
 * @return void
500
 */
501
        public function testToServer() {
502
                date_default_timezone_set('Europe/Paris');
503

    
504
                $time = time();
505
                $this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
506

    
507
                date_default_timezone_set('America/New_York');
508
                $time = time();
509
                date_default_timezone_set('Europe/Paris');
510
                $result = $this->Time->toServer($time, 'America/New_York');
511
                $this->assertEquals(date('Y-m-d H:i:s', $time), $result);
512

    
513
                date_default_timezone_set('Europe/Paris');
514
                $time = '2005-10-25 10:00:00';
515
                $result = $this->Time->toServer($time);
516
                $date = new DateTime($time, new DateTimeZone('UTC'));
517
                $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
518
                $expected = $date->format('Y-m-d H:i:s');
519
                $this->assertEquals($expected, $result);
520

    
521
                $time = '2002-01-01 05:15:30';
522
                $result = $this->Time->toServer($time, 'America/New_York');
523
                $date = new DateTime($time, new DateTimeZone('America/New_York'));
524
                $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
525
                $expected = $date->format('Y-m-d H:i:s');
526
                $this->assertEquals($expected, $result);
527

    
528
                $time = '2010-01-28T15:00:00+10:00';
529
                $result = $this->Time->toServer($time, 'America/New_York');
530
                $date = new DateTime($time);
531
                $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
532
                $expected = $date->format('Y-m-d H:i:s');
533
                $this->assertEquals($expected, $result);
534

    
535
                $date = new DateTime(null, new DateTimeZone('America/New_York'));
536
                $result = $this->Time->toServer($date, 'Pacific/Tahiti');
537
                $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
538
                $expected = $date->format('Y-m-d H:i:s');
539
                $this->assertEquals($expected, $result);
540

    
541
                $this->_restoreSystemTimezone();
542

    
543
                $time = time();
544
                $result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
545
                $expected = date('l jS \of F Y h:i:s A', $time);
546
                $this->assertEquals($expected, $result);
547

    
548
                $this->assertFalse($this->Time->toServer(time(), new Object()));
549

    
550
                date_default_timezone_set('UTC');
551

    
552
                $serverTime = new DateTime('2012-12-11 14:15:20');
553

    
554
                $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
555
                foreach ($timezones as $timezone) {
556
                        $result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
557
                        $tz = new DateTimeZone($timezone);
558
                        $this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
559
                }
560

    
561
                date_default_timezone_set('UTC');
562
                $date = new DateTime('now', new DateTimeZone('America/New_York'));
563

    
564
                $result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
565
                $date->setTimezone($this->Time->timezone());
566
                $expected = $date->format('Y-m-d H:i:s');
567
                $this->assertEquals($expected, $result);
568

    
569
                $this->_restoreSystemTimezone();
570
        }
571

    
572
/**
573
 * testToAtom method
574
 *
575
 * @return void
576
 */
577
        public function testToAtom() {
578
                $this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
579
        }
580

    
581
/**
582
 * testToRss method
583
 *
584
 * @return void
585
 */
586
        public function testToRss() {
587
                $date = '2012-08-12 12:12:45';
588
                $time = strtotime($date);
589
                $this->assertEquals(date('r', $time), $this->Time->toRss($time));
590

    
591
                $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
592
                foreach ($timezones as $timezone) {
593
                        $yourTimezone = new DateTimeZone($timezone);
594
                        $yourTime = new DateTime($date, $yourTimezone);
595
                        $userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
596
                        $time = $yourTime->format('U');
597
                        $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone");
598
                        $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
599
                }
600
        }
601

    
602
/**
603
 * testFormat method
604
 *
605
 * @return void
606
 */
607
        public function testFormat() {
608
                $format = 'D-M-Y';
609
                $tz = date_default_timezone_get();
610
                $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
611
                foreach ($arr as $val) {
612
                        $this->assertEquals(date($format, $val), $this->Time->format($format, $val));
613
                        $this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
614
                }
615

    
616
                $result = $this->Time->format('Y-m-d', null, 'never');
617
                $this->assertEquals('never', $result);
618

    
619
                $result = $this->Time->format('Y-m-d', '');
620
                $this->assertSame('', $result);
621

    
622
                $result = $this->Time->format('Y-m-d', false);
623
                $this->assertSame('', $result);
624

    
625
                $result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
626
                $this->assertEquals('13-01-2012', $result);
627

    
628
                $result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
629
                $this->assertEquals('invalid', $result);
630

    
631
                $result = $this->Time->format('0000-00-00', '%d-%m-%Y', 'invalid');
632
                $this->assertEquals('invalid', $result);
633
        }
634

    
635
/**
636
 * testOfGmt method
637
 *
638
 * @return void
639
 */
640
        public function testGmt() {
641
                $hour = 3;
642
                $min = 4;
643
                $sec = 2;
644
                $month = 5;
645
                $day = 14;
646
                $year = 2007;
647
                $time = mktime($hour, $min, $sec, $month, $day, $year);
648
                $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
649
                $this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
650

    
651
                $hour = date('H');
652
                $min = date('i');
653
                $sec = date('s');
654
                $month = date('m');
655
                $day = date('d');
656
                $year = date('Y');
657
                $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
658
                $this->assertEquals($expected, $this->Time->gmt(null));
659
        }
660

    
661
/**
662
 * testIsToday method
663
 *
664
 * @return void
665
 */
666
        public function testIsToday() {
667
                $result = $this->Time->isToday('+1 day');
668
                $this->assertFalse($result);
669
                $result = $this->Time->isToday('+1 days');
670
                $this->assertFalse($result);
671
                $result = $this->Time->isToday('+0 day');
672
                $this->assertTrue($result);
673
                $result = $this->Time->isToday('-1 day');
674
                $this->assertFalse($result);
675
        }
676

    
677
/**
678
 * testIsFuture method
679
 *
680
 * @return void
681
 */
682
        public function testIsFuture() {
683
                $this->assertTrue($this->Time->isFuture('+1 month'));
684
                $this->assertTrue($this->Time->isFuture('+1 days'));
685
                $this->assertTrue($this->Time->isFuture('+1 minute'));
686
                $this->assertTrue($this->Time->isFuture('+1 second'));
687

    
688
                $this->assertFalse($this->Time->isFuture('-1 second'));
689
                $this->assertFalse($this->Time->isFuture('-1 day'));
690
                $this->assertFalse($this->Time->isFuture('-1 week'));
691
                $this->assertFalse($this->Time->isFuture('-1 month'));
692
        }
693

    
694
/**
695
 * testIsPast method
696
 *
697
 * @return void
698
 */
699
        public function testIsPast() {
700
                $this->assertFalse($this->Time->isPast('+1 month'));
701
                $this->assertFalse($this->Time->isPast('+1 days'));
702
                $this->assertFalse($this->Time->isPast('+1 minute'));
703
                $this->assertFalse($this->Time->isPast('+1 second'));
704

    
705
                $this->assertTrue($this->Time->isPast('-1 second'));
706
                $this->assertTrue($this->Time->isPast('-1 day'));
707
                $this->assertTrue($this->Time->isPast('-1 week'));
708
                $this->assertTrue($this->Time->isPast('-1 month'));
709
        }
710

    
711
/**
712
 * testIsThisWeek method
713
 *
714
 * @return void
715
 */
716
        public function testIsThisWeek() {
717
                // A map of days which goes from -1 day of week to +1 day of week
718
                $map = array(
719
                        'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
720
                        'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
721
                        'Sun' => array(-7, 1)
722
                );
723
                $days = $map[date('D')];
724

    
725
                for ($day = $days[0] + 1; $day < $days[1]; $day++) {
726
                        $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
727
                }
728
                $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
729
                $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
730
        }
731

    
732
/**
733
 * testIsThisMonth method
734
 *
735
 * @return void
736
 */
737
        public function testIsThisMonth() {
738
                $result = $this->Time->isThisMonth('+0 day');
739
                $this->assertTrue($result);
740
                $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
741
                $this->assertTrue($result);
742
                $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
743
                $this->assertFalse($result);
744
                $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
745
                $this->assertFalse($result);
746
        }
747

    
748
/**
749
 * testIsThisYear method
750
 *
751
 * @return void
752
 */
753
        public function testIsThisYear() {
754
                $result = $this->Time->isThisYear('+0 day');
755
                $this->assertTrue($result);
756
                $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
757
                $this->assertTrue($result);
758
        }
759

    
760
/**
761
 * testWasYesterday method
762
 *
763
 * @return void
764
 */
765
        public function testWasYesterday() {
766
                $result = $this->Time->wasYesterday('+1 day');
767
                $this->assertFalse($result);
768
                $result = $this->Time->wasYesterday('+1 days');
769
                $this->assertFalse($result);
770
                $result = $this->Time->wasYesterday('+0 day');
771
                $this->assertFalse($result);
772
                $result = $this->Time->wasYesterday('-1 day');
773
                $this->assertTrue($result);
774
                $result = $this->Time->wasYesterday('-1 days');
775
                $this->assertTrue($result);
776
                $result = $this->Time->wasYesterday('-2 days');
777
                $this->assertFalse($result);
778
        }
779

    
780
/**
781
 * testIsTomorrow method
782
 *
783
 * @return void
784
 */
785
        public function testIsTomorrow() {
786
                $result = $this->Time->isTomorrow('+1 day');
787
                $this->assertTrue($result);
788
                $result = $this->Time->isTomorrow('+1 days');
789
                $this->assertTrue($result);
790
                $result = $this->Time->isTomorrow('+0 day');
791
                $this->assertFalse($result);
792
                $result = $this->Time->isTomorrow('-1 day');
793
                $this->assertFalse($result);
794
        }
795

    
796
/**
797
 * testWasWithinLast method
798
 *
799
 * @return void
800
 */
801
        public function testWasWithinLast() {
802
                $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
803
                $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
804
                $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
805
                $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
806
                $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
807
                $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
808
                $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
809
                $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
810

    
811
                $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
812
                $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
813
                $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
814
                $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
815
                $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
816
                $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
817
                $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
818
                $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
819

    
820
                $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
821
                $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
822
                $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
823
                $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
824
                $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
825
                $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
826

    
827
                $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
828
                $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
829
                $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
830
                $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
831
                $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
832
                $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
833
                $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
834

    
835
                $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
836
                $this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
837
                $this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
838
                $this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
839
        }
840

    
841
/**
842
 * testWasWithinLast method
843
 *
844
 * @return void
845
 */
846
        public function testIsWithinNext() {
847
                $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
848
                $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
849
                $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
850
                $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
851
                $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
852
                $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
853
                $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
854
                $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
855

    
856
                $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
857
                $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
858
                $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
859
                $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
860
                $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
861
                $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
862
                $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
863
                $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
864

    
865
                $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
866
                $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
867
                $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
868
                $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
869
                $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
870
                $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
871

    
872
                $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
873
                $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
874
                $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
875
                $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
876
                $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
877
                $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
878
                $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
879

    
880
                $this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
881
                $this->assertFalse($this->Time->isWithinNext('1   ', '-1 hour'));
882
                $this->assertFalse($this->Time->isWithinNext('1   ', '-1 minute'));
883
                $this->assertFalse($this->Time->isWithinNext('1   ', '-23 hours -59 minutes -59 seconds'));
884

    
885
                $this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
886
                $this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
887
        }
888

    
889
/**
890
 * testUserOffset method
891
 *
892
 * @return void
893
 */
894
        public function testUserOffset() {
895
                $timezoneServer = new DateTimeZone(date_default_timezone_get());
896
                $timeServer = new DateTime('now', $timezoneServer);
897
                $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
898

    
899
                $expected = time();
900
                $result = $this->Time->fromString(time(), $yourTimezone);
901
                $this->assertWithinMargin($expected, $result, 1);
902

    
903
                $result = $this->Time->fromString(time(), $timezoneServer->getName());
904
                $this->assertWithinMargin($expected, $result, 1);
905

    
906
                $result = $this->Time->fromString(time(), $timezoneServer);
907
                $this->assertWithinMargin($expected, $result, 1);
908

    
909
                Configure::write('Config.timezone', $timezoneServer->getName());
910
                $result = $this->Time->fromString(time());
911
                $this->assertWithinMargin($expected, $result, 1);
912
                Configure::delete('Config.timezone');
913
        }
914

    
915
/**
916
 * test fromString()
917
 *
918
 * @return void
919
 */
920
        public function testFromString() {
921
                $result = $this->Time->fromString('');
922
                $this->assertFalse($result);
923

    
924
                $result = $this->Time->fromString(0, 0);
925
                $this->assertFalse($result);
926

    
927
                $result = $this->Time->fromString('+1 hour');
928
                $expected = strtotime('+1 hour');
929
                $this->assertWithinMargin($expected, $result, 1);
930

    
931
                $timezone = date('Z', time());
932
                $result = $this->Time->fromString('+1 hour', $timezone);
933
                $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
934
                $this->assertWithinMargin($expected, $result, 1);
935

    
936
                $timezone = date_default_timezone_get();
937
                $result = $this->Time->fromString('+1 hour', $timezone);
938
                $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
939
                $this->assertWithinMargin($expected, $result, 1);
940

    
941
                date_default_timezone_set('UTC');
942
                $date = new DateTime('now', new DateTimeZone('Europe/London'));
943
                $this->Time->fromString($date);
944
                $this->assertEquals('Europe/London', $date->getTimeZone()->getName());
945

    
946
                $this->_restoreSystemTimezone();
947
        }
948

    
949
/**
950
 * test fromString() with a DateTime object as the dateString
951
 *
952
 * @return void
953
 */
954
        public function testFromStringWithDateTime() {
955
                date_default_timezone_set('UTC');
956

    
957
                $date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
958
                $result = $this->Time->fromString($date, 'UTC');
959
                $date->setTimezone(new DateTimeZone('UTC'));
960
                $expected = $date->format('U') + $date->getOffset();
961

    
962
                $this->assertWithinMargin($expected, $result, 1);
963

    
964
                date_default_timezone_set('Australia/Melbourne');
965

    
966
                $date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
967
                $result = $this->Time->fromString($date, 'Asia/Kuwait');
968

    
969
                $date->setTimezone(new DateTimeZone('Asia/Kuwait'));
970
                $expected = $date->format('U') + $date->getOffset();
971
                $this->assertWithinMargin($expected, $result, 1);
972

    
973
                $this->_restoreSystemTimezone();
974
        }
975

    
976
/**
977
 * Test that datetimes in the default timezone are not modified.
978
 *
979
 * @return void
980
 */
981
        public function testFromStringWithDateTimeNoConversion() {
982
                Configure::write('Config.timezone', date_default_timezone_get());
983
                $date = new DateTime('2013-04-09');
984
                $result = $this->Time->fromString($date);
985
                $this->assertEquals($result, $date->format('U'));
986
        }
987

    
988
/**
989
 * test converting time specifiers using a time definition localfe file
990
 *
991
 * @return void
992
 */
993
        public function testConvertSpecifiers() {
994
                App::build(array(
995
                        'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
996
                ), App::RESET);
997
                Configure::write('Config.language', 'time_test');
998
                $time = strtotime('Thu Jan 14 11:43:39 2010');
999

    
1000
                $result = $this->Time->convertSpecifiers('%a', $time);
1001
                $expected = 'jue';
1002
                $this->assertEquals($expected, $result);
1003

    
1004
                $result = $this->Time->convertSpecifiers('%A', $time);
1005
                $expected = 'jueves';
1006
                $this->assertEquals($expected, $result);
1007

    
1008
                $result = $this->Time->convertSpecifiers('%c', $time);
1009
                $expected = 'jue %d ene %Y %H:%M:%S %Z';
1010
                $this->assertEquals($expected, $result);
1011

    
1012
                $result = $this->Time->convertSpecifiers('%C', $time);
1013
                $expected = '20';
1014
                $this->assertEquals($expected, $result);
1015

    
1016
                $result = $this->Time->convertSpecifiers('%D', $time);
1017
                $expected = '%m/%d/%y';
1018
                $this->assertEquals($expected, $result);
1019

    
1020
                $result = $this->Time->convertSpecifiers('%b', $time);
1021
                $expected = 'ene';
1022
                $this->assertEquals($expected, $result);
1023

    
1024
                $result = $this->Time->convertSpecifiers('%h', $time);
1025
                $expected = 'ene';
1026
                $this->assertEquals($expected, $result);
1027

    
1028
                $result = $this->Time->convertSpecifiers('%B', $time);
1029
                $expected = 'enero';
1030
                $this->assertEquals($expected, $result);
1031

    
1032
                $result = $this->Time->convertSpecifiers('%n', $time);
1033
                $expected = "\n";
1034
                $this->assertEquals($expected, $result);
1035

    
1036
                $result = $this->Time->convertSpecifiers('%n', $time);
1037
                $expected = "\n";
1038
                $this->assertEquals($expected, $result);
1039

    
1040
                $result = $this->Time->convertSpecifiers('%p', $time);
1041
                $expected = 'AM';
1042
                $this->assertEquals($expected, $result);
1043

    
1044
                $result = $this->Time->convertSpecifiers('%P', $time);
1045
                $expected = 'am';
1046
                $this->assertEquals($expected, $result);
1047

    
1048
                $result = $this->Time->convertSpecifiers('%r', $time);
1049
                $expected = '%I:%M:%S AM';
1050
                $this->assertEquals($expected, $result);
1051

    
1052
                $result = $this->Time->convertSpecifiers('%R', $time);
1053
                $expected = '11:43';
1054
                $this->assertEquals($expected, $result);
1055

    
1056
                $result = $this->Time->convertSpecifiers('%t', $time);
1057
                $expected = "\t";
1058
                $this->assertEquals($expected, $result);
1059

    
1060
                $result = $this->Time->convertSpecifiers('%T', $time);
1061
                $expected = '%H:%M:%S';
1062
                $this->assertEquals($expected, $result);
1063

    
1064
                $result = $this->Time->convertSpecifiers('%u', $time);
1065
                $expected = 4;
1066
                $this->assertEquals($expected, $result);
1067

    
1068
                $result = $this->Time->convertSpecifiers('%x', $time);
1069
                $expected = '%d/%m/%y';
1070
                $this->assertEquals($expected, $result);
1071

    
1072
                $result = $this->Time->convertSpecifiers('%X', $time);
1073
                $expected = '%H:%M:%S';
1074
                $this->assertEquals($expected, $result);
1075
        }
1076

    
1077
/**
1078
 * test convert %e on Windows.
1079
 *
1080
 * @return void
1081
 */
1082
        public function testConvertPercentE() {
1083
                $this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run Windows tests on non-Windows OS.');
1084

    
1085
                $time = strtotime('Thu Jan 14 11:43:39 2010');
1086
                $result = $this->Time->convertSpecifiers('%e', $time);
1087
                $expected = '14';
1088
                $this->assertEquals($expected, $result);
1089

    
1090
                $result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
1091
                $expected = ' 1';
1092
                $this->assertEquals($expected, $result);
1093
        }
1094

    
1095
/**
1096
 * test formatting dates taking in account preferred i18n locale file
1097
 *
1098
 * @return void
1099
 */
1100
        public function testI18nFormat() {
1101
                App::build(array(
1102
                        'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
1103
                ), App::RESET);
1104
                Configure::write('Config.language', 'time_test');
1105

    
1106
                $time = strtotime('Thu Jan 14 13:59:28 2010');
1107

    
1108
                $result = $this->Time->i18nFormat($time);
1109
                $expected = '14/01/10';
1110
                $this->assertEquals($expected, $result);
1111

    
1112
                $result = $this->Time->i18nFormat($time, '%c');
1113
                $expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
1114
                $this->assertEquals($expected, $result);
1115

    
1116
                $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
1117
                $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
1118
                $this->assertEquals($expected, $result);
1119

    
1120
                $time = strtotime('Wed Jan 13 13:59:28 2010');
1121

    
1122
                $result = $this->Time->i18nFormat($time);
1123
                $expected = '13/01/10';
1124
                $this->assertEquals($expected, $result);
1125

    
1126
                $result = $this->Time->i18nFormat($time, '%c');
1127
                $expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
1128
                $this->assertEquals($expected, $result);
1129

    
1130
                $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
1131
                $expected = 'Time is 01:59:28 PM, and date is 13/01/10';
1132
                $this->assertEquals($expected, $result);
1133

    
1134
                $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
1135
                $expected = 'Date invalid';
1136
                $this->assertEquals($expected, $result);
1137
        }
1138

    
1139
/**
1140
 * test new format() syntax which inverts first and second parameters
1141
 *
1142
 * @return void
1143
 */
1144
        public function testFormatNewSyntax() {
1145
                $time = time();
1146
                $this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
1147
                $this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
1148
        }
1149

    
1150
/**
1151
 * testListTimezones
1152
 *
1153
 * @return void
1154
 */
1155
        public function testListTimezones() {
1156
                $return = CakeTime::listTimezones();
1157
                $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1158
                $this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
1159
                $this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
1160
                $this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
1161
                $this->assertTrue(isset($return['UTC']['UTC']));
1162
                $this->assertFalse(isset($return['Cuba']));
1163
                $this->assertFalse(isset($return['US']));
1164

    
1165
                $return = CakeTime::listTimezones('#^Asia/#');
1166
                $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1167
                $this->assertFalse(isset($return['Pacific']));
1168

    
1169
                $return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
1170
                $this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
1171
                $this->assertTrue(isset($return['Pacific/Tahiti']));
1172

    
1173
                if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
1174
                        $return = CakeTime::listTimezones(DateTimeZone::ASIA);
1175
                        $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1176
                        $this->assertFalse(isset($return['Pacific']));
1177

    
1178
                        $return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
1179
                        $this->assertTrue(isset($return['Pacific/Honolulu']));
1180
                        $this->assertFalse(isset($return['Asia/Bangkok']));
1181
                }
1182
        }
1183

    
1184
/**
1185
 * Tests that using CakeTime::format() with the correct sytax actually converts
1186
 * from one timezone to the other correctly
1187
 *
1188
 * @return void
1189
 */
1190
        public function testCorrectTimezoneConversion() {
1191
                date_default_timezone_set('UTC');
1192
                $date = '2012-01-01 10:00:00';
1193
                $converted = CakeTime::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
1194
                $expected = new DateTime($date);
1195
                $expected->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1196
                $this->assertEquals($expected->format('Y-m-d H:i'), $converted);
1197
        }
1198

    
1199
}