7
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice
13
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
15
* @package Cake.Test.Case.View.Helper
16
* @since CakePHP(tm) v 1.2.0.4206
17
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19
App::uses('CakeTime', 'Utility');
24
* @package Cake.Test.Case.View.Helper
26
class CakeTimeTest extends CakeTestCase {
29
* Default system timezone identifier
33
protected $_systemTimezoneIdentifier = null;
40
public function setUp() {
41
$this->Time = new CakeTime();
42
$this->_systemTimezoneIdentifier = date_default_timezone_get();
50
public function tearDown() {
52
$this->_restoreSystemTimezone();
56
* Restored the original system timezone
58
* @param string $timezoneIdentifier Timezone string
61
protected function _restoreSystemTimezone() {
62
date_default_timezone_set($this->_systemTimezoneIdentifier);
66
* testToQuarter method
70
public function testToQuarter() {
71
$result = $this->Time->toQuarter('2007-12-25');
72
$this->assertEquals(4, $result);
74
$result = $this->Time->toQuarter('2007-9-25');
75
$this->assertEquals(3, $result);
77
$result = $this->Time->toQuarter('2007-3-25');
78
$this->assertEquals(1, $result);
80
$result = $this->Time->toQuarter('2007-3-25', true);
81
$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
83
$result = $this->Time->toQuarter('2007-5-25', true);
84
$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
86
$result = $this->Time->toQuarter('2007-8-25', true);
87
$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
89
$result = $this->Time->toQuarter('2007-12-25', true);
90
$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
94
* provider for timeAgoInWords() tests
98
public static function timeAgoProvider() {
100
array('-12 seconds', '12 seconds ago'),
101
array('-12 minutes', '12 minutes ago'),
102
array('-2 hours', '2 hours ago'),
103
array('-1 day', '1 day ago'),
104
array('-2 days', '2 days ago'),
105
array('-2 days -3 hours', '2 days, 3 hours ago'),
106
array('-1 week', '1 week ago'),
107
array('-2 weeks -2 days', '2 weeks, 2 days ago'),
108
array('+1 week', '1 week'),
109
array('+1 week 1 day', '1 week, 1 day'),
110
array('+2 weeks 2 day', '2 weeks, 2 days'),
111
array('2007-9-24', 'on 24/9/07'),
112
array('now', 'just now'),
117
* testTimeAgoInWords method
119
* @dataProvider timeAgoProvider
122
public function testTimeAgoInWords($input, $expected) {
123
$result = $this->Time->timeAgoInWords($input);
124
$this->assertEquals($expected, $result);
128
* provider for timeAgo with an end date.
132
public function timeAgoEndProvider() {
135
'+4 months +2 weeks +3 days',
136
'4 months, 2 weeks, 3 days',
140
'+4 months +2 weeks +1 day',
141
'4 months, 2 weeks, 1 day',
145
'+3 months +2 weeks',
150
'+3 months +2 weeks +1 day',
151
'3 months, 2 weeks, 1 day',
155
'+1 months +1 week +1 day',
156
'1 month, 1 week, 1 day',
162
'on ' . date('j/n/y', strtotime('+2 months +2 days'))
165
'+2 months +12 days',
166
'2 months, 1 week, 5 days',
173
* test the end option for timeAgoInWords
175
* @dataProvider timeAgoEndProvider
178
public function testTimeAgoInWordsEnd($input, $expected, $end) {
179
$result = $this->Time->timeAgoInWords(
180
$input, array('end' => $end)
182
$this->assertEquals($expected, $result);
186
* Test the accuracy option for timeAgoInWords()
190
public function testTimeAgoInWordsAccuracy() {
191
$result = $this->Time->timeAgoInWords(
192
strtotime('+8 years +4 months +2 weeks +3 days'),
193
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
195
$expected = '8 years';
196
$this->assertEquals($expected, $result);
198
$result = $this->Time->timeAgoInWords(
199
strtotime('+8 years +4 months +2 weeks +3 days'),
200
array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
202
$expected = '8 years, 4 months';
203
$this->assertEquals($expected, $result);
205
$result = $this->Time->timeAgoInWords(
206
strtotime('+8 years +4 months +2 weeks +3 days'),
207
array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
209
$expected = '8 years, 4 months, 2 weeks';
210
$this->assertEquals($expected, $result);
212
$result = $this->Time->timeAgoInWords(
213
strtotime('+8 years +4 months +2 weeks +3 days'),
214
array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
216
$expected = '8 years, 4 months, 2 weeks, 3 days';
217
$this->assertEquals($expected, $result);
219
$result = $this->Time->timeAgoInWords(
220
strtotime('+1 years +5 weeks'),
221
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
223
$expected = '1 year';
224
$this->assertEquals($expected, $result);
228
* Test the format option of timeAgoInWords()
232
public function testTimeAgoInWordsWithFormat() {
233
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
234
$this->assertEquals('on 2007-09-25', $result);
236
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
237
$this->assertEquals('on 2007-09-25', $result);
239
$result = $this->Time->timeAgoInWords(
240
strtotime('+2 weeks +2 days'),
243
$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
245
$result = $this->Time->timeAgoInWords(
246
strtotime('+2 months +2 days'),
247
array('end' => '1 month', 'format' => 'Y-m-d')
249
$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
253
* test timeAgoInWords() with negative values.
257
public function testTimeAgoInWordsNegativeValues() {
258
$result = $this->Time->timeAgoInWords(
259
strtotime('-2 months -2 days'),
260
array('end' => '3 month')
262
$this->assertEquals('2 months, 2 days ago', $result);
264
$result = $this->Time->timeAgoInWords(
265
strtotime('-2 months -2 days'),
266
array('end' => '3 month')
268
$this->assertEquals('2 months, 2 days ago', $result);
270
$result = $this->Time->timeAgoInWords(
271
strtotime('-2 months -2 days'),
272
array('end' => '1 month', 'format' => 'Y-m-d')
274
$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
276
$result = $this->Time->timeAgoInWords(
277
strtotime('-2 years -5 months -2 days'),
278
array('end' => '3 years')
280
$this->assertEquals('2 years, 5 months, 2 days ago', $result);
282
$result = $this->Time->timeAgoInWords(
283
strtotime('-2 weeks -2 days'),
286
$this->assertEquals('2 weeks, 2 days ago', $result);
288
$time = strtotime('-3 years -12 months');
289
$result = $this->Time->timeAgoInWords($time);
290
$expected = 'on ' . date('j/n/y', $time);
291
$this->assertEquals($expected, $result);
293
$result = $this->Time->timeAgoInWords(
294
strtotime('-1 month -1 week -6 days'),
295
array('end' => '1 year', 'accuracy' => array('month' => 'month'))
297
$this->assertEquals('1 month ago', $result);
299
$timestamp = strtotime('-1 years -2 weeks -3 days');
300
$result = $this->Time->timeAgoInWords(
302
array('accuracy' => array('year' => 'year'))
304
$expected = 'on ' . date('j/n/y', $timestamp);
305
$this->assertEquals($expected, $result);
307
$result = $this->Time->timeAgoInWords(
308
strtotime('-13 months -5 days'),
309
array('end' => '2 years')
311
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
319
public function testNice() {
320
$time = time() + 2 * DAY;
321
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
323
$time = time() - 2 * DAY;
324
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
327
$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
330
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
333
$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
336
$this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
337
$this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
339
$this->Time->niceFormat = '%Y-%d-%m';
340
$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
341
$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);
343
CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
344
$this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time));
345
$this->assertEquals('%Y-%d-%m %H:%M:%S', $this->Time->niceFormat);
347
date_default_timezone_set('UTC');
348
$result = $this->Time->nice(null, 'America/New_York');
349
$expected = $this->Time->nice(time(), 'America/New_York');
350
$this->assertEquals($expected, $result);
352
$this->_restoreSystemTimezone();
356
* testNiceShort method
360
public function testNiceShort() {
362
$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
364
$time = time() - DAY;
365
$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
367
$time = time() + DAY;
368
$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
370
date_default_timezone_set('Europe/London');
371
$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
372
$this->assertEquals('Jan 15th 2005, 11:00', $result);
374
date_default_timezone_set('UTC');
375
$result = $this->Time->niceShort(null, 'America/New_York');
376
$expected = $this->Time->niceShort(time(), 'America/New_York');
377
$this->assertEquals($expected, $result);
379
$this->_restoreSystemTimezone();
383
* testDaysAsSql method
387
public function testDaysAsSql() {
391
$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
392
$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
396
* testDayAsSql method
400
public function testDayAsSql() {
403
$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
404
$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
412
public function testToUnix() {
413
$this->assertEquals(time(), $this->Time->toUnix(time()));
414
$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
415
$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
416
$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
417
$this->assertEquals(false, $this->Time->toUnix(''));
418
$this->assertEquals(false, $this->Time->toUnix(null));
422
* testToServer method
426
public function testToServer() {
427
date_default_timezone_set('Europe/Paris');
430
$this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
432
date_default_timezone_set('America/New_York');
434
date_default_timezone_set('Europe/Paris');
435
$result = $this->Time->toServer($time, 'America/New_York');
436
$this->assertEquals(date('Y-m-d H:i:s', $time), $result);
438
date_default_timezone_set('Europe/Paris');
439
$time = '2005-10-25 10:00:00';
440
$result = $this->Time->toServer($time);
441
$date = new DateTime($time, new DateTimeZone('UTC'));
442
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
443
$expected = $date->format('Y-m-d H:i:s');
444
$this->assertEquals($expected, $result);
446
$time = '2002-01-01 05:15:30';
447
$result = $this->Time->toServer($time, 'America/New_York');
448
$date = new DateTime($time, new DateTimeZone('America/New_York'));
449
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
450
$expected = $date->format('Y-m-d H:i:s');
451
$this->assertEquals($expected, $result);
453
$time = '2010-01-28T15:00:00+10:00';
454
$result = $this->Time->toServer($time, 'America/New_York');
455
$date = new DateTime($time);
456
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
457
$expected = $date->format('Y-m-d H:i:s');
458
$this->assertEquals($expected, $result);
460
$date = new DateTime(null, new DateTimeZone('America/New_York'));
461
$result = $this->Time->toServer($date, 'Pacific/Tahiti');
462
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
463
$expected = $date->format('Y-m-d H:i:s');
464
$this->assertEquals($expected, $result);
466
$this->_restoreSystemTimezone();
469
$result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
470
$expected = date('l jS \of F Y h:i:s A', $time);
471
$this->assertEquals($expected, $result);
473
$this->assertFalse($this->Time->toServer(time(), new Object()));
475
date_default_timezone_set('UTC');
477
$serverTime = new DateTime('now');
479
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
480
foreach ($timezones as $timezone) {
481
$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
482
$tz = new DateTimeZone($timezone);
483
$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
486
date_default_timezone_set('UTC');
487
$date = new DateTime('now', new DateTimeZone('America/New_York'));
489
$result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
490
$date->setTimezone($this->Time->timezone());
491
$expected = $date->format('Y-m-d H:i:s');
492
$this->assertEquals($expected, $result);
494
$this->_restoreSystemTimezone();
502
public function testToAtom() {
503
$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
511
public function testToRss() {
512
$this->assertEquals(date('r'), $this->Time->toRss(time()));
514
if (!$this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
515
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
516
foreach ($timezones as $timezone) {
517
$yourTimezone = new DateTimeZone($timezone);
518
$yourTime = new DateTime('now', $yourTimezone);
519
$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
520
$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $userOffset));
521
$this->assertEquals($yourTime->format('r'), $this->Time->toRss(time(), $timezone));
531
public function testFormat() {
533
$tz = date_default_timezone_get();
534
$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
535
foreach ($arr as $val) {
536
$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
537
$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
540
$result = $this->Time->format('Y-m-d', null, 'never');
541
$this->assertEquals('never', $result);
543
$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
544
$this->assertEquals('13-01-2012', $result);
546
$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
547
$this->assertEquals('invalid', $result);
555
public function testGmt() {
562
$time = mktime($hour, $min, $sec, $month, $day, $year);
563
$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
564
$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
572
$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
573
$this->assertEquals($expected, $this->Time->gmt(null));
581
public function testIsToday() {
582
$result = $this->Time->isToday('+1 day');
583
$this->assertFalse($result);
584
$result = $this->Time->isToday('+1 days');
585
$this->assertFalse($result);
586
$result = $this->Time->isToday('+0 day');
587
$this->assertTrue($result);
588
$result = $this->Time->isToday('-1 day');
589
$this->assertFalse($result);
593
* testIsThisWeek method
597
public function testIsThisWeek() {
598
// A map of days which goes from -1 day of week to +1 day of week
600
'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
601
'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
602
'Sun' => array(-7, 1)
604
$days = $map[date('D')];
606
for ($day = $days[0] + 1; $day < $days[1]; $day++) {
607
$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
609
$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
610
$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
614
* testIsThisMonth method
618
public function testIsThisMonth() {
619
$result = $this->Time->isThisMonth('+0 day');
620
$this->assertTrue($result);
621
$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
622
$this->assertTrue($result);
623
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
624
$this->assertFalse($result);
625
$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
626
$this->assertFalse($result);
630
* testIsThisYear method
634
public function testIsThisYear() {
635
$result = $this->Time->isThisYear('+0 day');
636
$this->assertTrue($result);
637
$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
638
$this->assertTrue($result);
642
* testWasYesterday method
646
public function testWasYesterday() {
647
$result = $this->Time->wasYesterday('+1 day');
648
$this->assertFalse($result);
649
$result = $this->Time->wasYesterday('+1 days');
650
$this->assertFalse($result);
651
$result = $this->Time->wasYesterday('+0 day');
652
$this->assertFalse($result);
653
$result = $this->Time->wasYesterday('-1 day');
654
$this->assertTrue($result);
655
$result = $this->Time->wasYesterday('-1 days');
656
$this->assertTrue($result);
657
$result = $this->Time->wasYesterday('-2 days');
658
$this->assertFalse($result);
662
* testIsTomorrow method
666
public function testIsTomorrow() {
667
$result = $this->Time->isTomorrow('+1 day');
668
$this->assertTrue($result);
669
$result = $this->Time->isTomorrow('+1 days');
670
$this->assertTrue($result);
671
$result = $this->Time->isTomorrow('+0 day');
672
$this->assertFalse($result);
673
$result = $this->Time->isTomorrow('-1 day');
674
$this->assertFalse($result);
678
* testWasWithinLast method
682
public function testWasWithinLast() {
683
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
684
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
685
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
686
$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
687
$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
688
$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
689
$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
690
$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
692
$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
693
$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
694
$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
695
$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
696
$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
697
$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
698
$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
699
$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
701
$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
702
$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
703
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
704
$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
705
$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
706
$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
708
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
709
$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
710
$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
711
$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
712
$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
713
$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
714
$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
716
$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
717
$this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
718
$this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
719
$this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
723
* testWasWithinLast method
727
public function testIsWithinNext() {
728
$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
729
$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
730
$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
731
$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
732
$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
733
$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
734
$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
735
$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
737
$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
738
$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
739
$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
740
$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
741
$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
742
$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
743
$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
744
$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
746
$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
747
$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
748
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
749
$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
750
$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
751
$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
753
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
754
$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
755
$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
756
$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
757
$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
758
$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
759
$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
761
$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
762
$this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
763
$this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
764
$this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
766
$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
767
$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
771
* testUserOffset method
775
public function testUserOffset() {
776
$timezoneServer = new DateTimeZone(date_default_timezone_get());
777
$timeServer = new DateTime('now', $timezoneServer);
778
$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
781
$result = $this->Time->fromString(time(), $yourTimezone);
782
$this->assertEquals($expected, $result);
784
$result = $this->Time->fromString(time(), $timezoneServer->getName());
785
$this->assertEquals($expected, $result);
787
$result = $this->Time->fromString(time(), $timezoneServer);
788
$this->assertEquals($expected, $result);
790
Configure::write('Config.timezone', $timezoneServer->getName());
791
$result = $this->Time->fromString(time());
792
$this->assertEquals($expected, $result);
793
Configure::delete('Config.timezone');
801
public function testFromString() {
802
$result = $this->Time->fromString('');
803
$this->assertFalse($result);
805
$result = $this->Time->fromString(0, 0);
806
$this->assertFalse($result);
808
$result = $this->Time->fromString('+1 hour');
809
$expected = strtotime('+1 hour');
810
$this->assertEquals($expected, $result);
812
$timezone = date('Z', time());
813
$result = $this->Time->fromString('+1 hour', $timezone);
814
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
815
$this->assertEquals($expected, $result);
817
$timezone = date_default_timezone_get();
818
$result = $this->Time->fromString('+1 hour', $timezone);
819
$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
820
$this->assertEquals($expected, $result);
822
date_default_timezone_set('UTC');
823
$date = new DateTime('now', new DateTimeZone('Europe/London'));
824
$this->Time->fromString($date);
825
$this->assertEquals('Europe/London', $date->getTimeZone()->getName());
827
$this->_restoreSystemTimezone();
831
* test fromString() with a DateTime object as the dateString
835
public function testFromStringWithDateTime() {
836
date_default_timezone_set('UTC');
838
$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
839
$result = $this->Time->fromString($date, 'UTC');
840
$date->setTimezone(new DateTimeZone('UTC'));
841
$expected = $date->format('U') + $date->getOffset();
843
$this->assertEquals($expected, $result);
845
date_default_timezone_set('Australia/Melbourne');
847
$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
848
$result = $this->Time->fromString($date, 'Asia/Kuwait');
849
$date->setTimezone(new DateTimeZone('Asia/Kuwait'));
850
$expected = $date->format('U') + $date->getOffset();
851
$this->assertEquals($expected, $result);
853
$this->_restoreSystemTimezone();
857
* test converting time specifiers using a time definition localfe file
861
public function testConvertSpecifiers() {
863
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
865
Configure::write('Config.language', 'time_test');
866
$time = strtotime('Thu Jan 14 11:43:39 2010');
868
$result = $this->Time->convertSpecifiers('%a', $time);
870
$this->assertEquals($expected, $result);
872
$result = $this->Time->convertSpecifiers('%A', $time);
873
$expected = 'jueves';
874
$this->assertEquals($expected, $result);
876
$result = $this->Time->convertSpecifiers('%c', $time);
877
$expected = 'jue %d ene %Y %H:%M:%S %Z';
878
$this->assertEquals($expected, $result);
880
$result = $this->Time->convertSpecifiers('%C', $time);
882
$this->assertEquals($expected, $result);
884
$result = $this->Time->convertSpecifiers('%D', $time);
885
$expected = '%m/%d/%y';
886
$this->assertEquals($expected, $result);
888
$result = $this->Time->convertSpecifiers('%b', $time);
890
$this->assertEquals($expected, $result);
892
$result = $this->Time->convertSpecifiers('%h', $time);
894
$this->assertEquals($expected, $result);
896
$result = $this->Time->convertSpecifiers('%B', $time);
898
$this->assertEquals($expected, $result);
900
$result = $this->Time->convertSpecifiers('%n', $time);
902
$this->assertEquals($expected, $result);
904
$result = $this->Time->convertSpecifiers('%n', $time);
906
$this->assertEquals($expected, $result);
908
$result = $this->Time->convertSpecifiers('%p', $time);
910
$this->assertEquals($expected, $result);
912
$result = $this->Time->convertSpecifiers('%P', $time);
914
$this->assertEquals($expected, $result);
916
$result = $this->Time->convertSpecifiers('%r', $time);
917
$expected = '%I:%M:%S AM';
918
$this->assertEquals($expected, $result);
920
$result = $this->Time->convertSpecifiers('%R', $time);
922
$this->assertEquals($expected, $result);
924
$result = $this->Time->convertSpecifiers('%t', $time);
926
$this->assertEquals($expected, $result);
928
$result = $this->Time->convertSpecifiers('%T', $time);
929
$expected = '%H:%M:%S';
930
$this->assertEquals($expected, $result);
932
$result = $this->Time->convertSpecifiers('%u', $time);
934
$this->assertEquals($expected, $result);
936
$result = $this->Time->convertSpecifiers('%x', $time);
937
$expected = '%d/%m/%y';
938
$this->assertEquals($expected, $result);
940
$result = $this->Time->convertSpecifiers('%X', $time);
941
$expected = '%H:%M:%S';
942
$this->assertEquals($expected, $result);
946
* test convert %e on windows.
950
public function testConvertPercentE() {
951
$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
953
$time = strtotime('Thu Jan 14 11:43:39 2010');
954
$result = $this->Time->convertSpecifiers('%e', $time);
956
$this->assertEquals($expected, $result);
958
$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
960
$this->assertEquals($expected, $result);
964
* test formatting dates taking in account preferred i18n locale file
968
public function testI18nFormat() {
970
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
972
Configure::write('Config.language', 'time_test');
974
$time = strtotime('Thu Jan 14 13:59:28 2010');
976
$result = $this->Time->i18nFormat($time);
977
$expected = '14/01/10';
978
$this->assertEquals($expected, $result);
980
$result = $this->Time->i18nFormat($time, '%c');
981
$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
982
$this->assertEquals($expected, $result);
984
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
985
$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
986
$this->assertEquals($expected, $result);
988
$time = strtotime('Wed Jan 13 13:59:28 2010');
990
$result = $this->Time->i18nFormat($time);
991
$expected = '13/01/10';
992
$this->assertEquals($expected, $result);
994
$result = $this->Time->i18nFormat($time, '%c');
995
$expected = 'miƩ 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
996
$this->assertEquals($expected, $result);
998
$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
999
$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
1000
$this->assertEquals($expected, $result);
1002
$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
1003
$expected = 'Date invalid';
1004
$this->assertEquals($expected, $result);
1008
* test new format() syntax which inverts first and second parameters
1012
public function testFormatNewSyntax() {
1014
$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
1015
$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
1023
public function testListTimezones() {
1024
$return = CakeTime::listTimezones();
1025
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1026
$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
1027
$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
1028
$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
1029
$this->assertTrue(isset($return['UTC']['UTC']));
1030
$this->assertFalse(isset($return['Cuba']));
1031
$this->assertFalse(isset($return['US']));
1033
$return = CakeTime::listTimezones('#^Asia/#');
1034
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1035
$this->assertFalse(isset($return['Pacific']));
1037
$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
1038
$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
1039
$this->assertTrue(isset($return['Pacific/Tahiti']));
1041
if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
1042
$return = CakeTime::listTimezones(DateTimeZone::ASIA);
1043
$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1044
$this->assertFalse(isset($return['Pacific']));
1046
$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
1047
$this->assertTrue(isset($return['Pacific/Honolulu']));
1048
$this->assertFalse(isset($return['Asia/Bangkok']));