PageRenderTime 130ms CodeModel.GetById 80ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/CakeTimeTest.php

https://bitbucket.org/linuxdream/agent-j
PHP | 1074 lines | 725 code | 142 blank | 207 comment | 2 complexity | 2364ac7b965cca70b57012ec03654973 MD5 | raw file
   1<?php
   2/**
   3 * CakeTimeTest file
   4 *
   5 * PHP 5
   6 *
   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)
   9 *
  10 * Licensed under The MIT License
  11 * Redistributions of files must retain the above copyright notice
  12 *
  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)
  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		$this->Time = new CakeTime();
  42		$this->_systemTimezoneIdentifier = date_default_timezone_get();
  43	}
  44
  45/**
  46 * tearDown method
  47 *
  48 * @return void
  49 */
  50	public function tearDown() {
  51		unset($this->Time);
  52		$this->_restoreSystemTimezone();
  53	}
  54
  55/**
  56 * Restored the original system timezone
  57 *
  58 * @param string $timezoneIdentifier Timezone string
  59 * @return void
  60 */
  61	protected function _restoreSystemTimezone() {
  62		date_default_timezone_set($this->_systemTimezoneIdentifier);
  63	}
  64
  65/**
  66 * testToQuarter method
  67 *
  68 * @return void
  69 */
  70	public function testToQuarter() {
  71		$result = $this->Time->toQuarter('2007-12-25');
  72		$this->assertEquals(4, $result);
  73
  74		$result = $this->Time->toQuarter('2007-9-25');
  75		$this->assertEquals(3, $result);
  76
  77		$result = $this->Time->toQuarter('2007-3-25');
  78		$this->assertEquals(1, $result);
  79
  80		$result = $this->Time->toQuarter('2007-3-25', true);
  81		$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
  82
  83		$result = $this->Time->toQuarter('2007-5-25', true);
  84		$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
  85
  86		$result = $this->Time->toQuarter('2007-8-25', true);
  87		$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
  88
  89		$result = $this->Time->toQuarter('2007-12-25', true);
  90		$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
  91	}
  92
  93/**
  94 * provider for timeAgoInWords() tests
  95 *
  96 * @return array
  97 */
  98	public static function timeAgoProvider() {
  99		return array(
 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'),
 113		);
 114	}
 115
 116/**
 117 * testTimeAgoInWords method
 118 *
 119 * @dataProvider timeAgoProvider
 120 * @return void
 121 */
 122	public function testTimeAgoInWords($input, $expected) {
 123		$result = $this->Time->timeAgoInWords($input);
 124		$this->assertEquals($expected, $result);
 125	}
 126
 127/**
 128 * provider for timeAgo with an end date.
 129 *
 130 * @return void
 131 */
 132	public function timeAgoEndProvider() {
 133		return array(
 134			array(
 135				'+4 months +2 weeks +3 days',
 136				'4 months, 2 weeks, 3 days',
 137				'8 years'
 138			),
 139			array(
 140				'+4 months +2 weeks +1 day',
 141				'4 months, 2 weeks, 1 day',
 142				'8 years'
 143			),
 144			array(
 145				'+3 months +2 weeks',
 146				'3 months, 2 weeks',
 147				'8 years'
 148			),
 149			array(
 150				'+3 months +2 weeks +1 day',
 151				'3 months, 2 weeks, 1 day',
 152				'8 years'
 153			),
 154			array(
 155				'+1 months +1 week +1 day',
 156				'1 month, 1 week, 1 day',
 157				'8 years'
 158			),
 159			array(
 160				'+2 months +2 days',
 161				'2 months, 2 days',
 162				'on ' . date('j/n/y', strtotime('+2 months +2 days'))
 163			),
 164			array(
 165				'+2 months +12 days',
 166				'2 months, 1 week, 5 days',
 167				'3 months'
 168			),
 169		);
 170	}
 171
 172/**
 173 * test the end option for timeAgoInWords
 174 *
 175 * @dataProvider timeAgoEndProvider
 176 * @return void
 177 */
 178	public function testTimeAgoInWordsEnd($input, $expected, $end) {
 179		$result = $this->Time->timeAgoInWords(
 180			$input, array('end' => $end)
 181		);
 182		$this->assertEquals($expected, $result);
 183	}
 184
 185/**
 186 * Test the accuracy option for timeAgoInWords()
 187 *
 188 * @return void
 189 */
 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')
 194		);
 195		$expected = '8 years';
 196		$this->assertEquals($expected, $result);
 197
 198		$result = $this->Time->timeAgoInWords(
 199			strtotime('+8 years +4 months +2 weeks +3 days'),
 200			array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
 201		);
 202		$expected = '8 years, 4 months';
 203		$this->assertEquals($expected, $result);
 204
 205		$result = $this->Time->timeAgoInWords(
 206			strtotime('+8 years +4 months +2 weeks +3 days'),
 207			array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
 208		);
 209		$expected = '8 years, 4 months, 2 weeks';
 210		$this->assertEquals($expected, $result);
 211
 212		$result = $this->Time->timeAgoInWords(
 213			strtotime('+8 years +4 months +2 weeks +3 days'),
 214			array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
 215		);
 216		$expected = '8 years, 4 months, 2 weeks, 3 days';
 217		$this->assertEquals($expected, $result);
 218
 219		$result = $this->Time->timeAgoInWords(
 220			strtotime('+1 years +5 weeks'),
 221			array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
 222		);
 223		$expected = '1 year';
 224		$this->assertEquals($expected, $result);
 225	}
 226
 227/**
 228 * Test the format option of timeAgoInWords()
 229 *
 230 * @return void
 231 */
 232	public function testTimeAgoInWordsWithFormat() {
 233		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
 234		$this->assertEquals('on 2007-09-25', $result);
 235
 236		$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
 237		$this->assertEquals('on 2007-09-25', $result);
 238
 239		$result = $this->Time->timeAgoInWords(
 240			strtotime('+2 weeks +2 days'),
 241			'Y-m-d'
 242		);
 243		$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
 244
 245		$result = $this->Time->timeAgoInWords(
 246			strtotime('+2 months +2 days'),
 247			array('end' => '1 month', 'format' => 'Y-m-d')
 248		);
 249		$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
 250	}
 251
 252/**
 253 * test timeAgoInWords() with negative values.
 254 *
 255 * @return void
 256 */
 257	public function testTimeAgoInWordsNegativeValues() {
 258		$result = $this->Time->timeAgoInWords(
 259			strtotime('-2 months -2 days'),
 260			array('end' => '3 month')
 261		);
 262		$this->assertEquals('2 months, 2 days ago', $result);
 263
 264		$result = $this->Time->timeAgoInWords(
 265			strtotime('-2 months -2 days'),
 266			array('end' => '3 month')
 267		);
 268		$this->assertEquals('2 months, 2 days ago', $result);
 269
 270		$result = $this->Time->timeAgoInWords(
 271			strtotime('-2 months -2 days'),
 272			array('end' => '1 month', 'format' => 'Y-m-d')
 273		);
 274		$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
 275
 276		$result = $this->Time->timeAgoInWords(
 277			strtotime('-2 years -5 months -2 days'),
 278			array('end' => '3 years')
 279		);
 280		$this->assertEquals('2 years, 5 months, 2 days ago', $result);
 281
 282		$result = $this->Time->timeAgoInWords(
 283			strtotime('-2 weeks -2 days'),
 284			'Y-m-d'
 285		);
 286		$this->assertEquals('2 weeks, 2 days ago', $result);
 287
 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);
 292
 293		$result = $this->Time->timeAgoInWords(
 294			strtotime('-1 month -1 week -6 days'),
 295			array('end' => '1 year', 'accuracy' => array('month' => 'month'))
 296		);
 297		$this->assertEquals('1 month ago', $result);
 298
 299		$timestamp = strtotime('-1 years -2 weeks -3 days');
 300		$result = $this->Time->timeAgoInWords(
 301			$timestamp,
 302			array('accuracy' => array('year' => 'year'))
 303		);
 304		$expected = 'on ' . date('j/n/y', $timestamp);
 305		$this->assertEquals($expected, $result);
 306
 307		$result = $this->Time->timeAgoInWords(
 308			strtotime('-13 months -5 days'),
 309			array('end' => '2 years')
 310		);
 311		$this->assertEquals('1 year, 1 month, 5 days ago', $result);
 312	}
 313
 314/**
 315 * testNice method
 316 *
 317 * @return void
 318 */
 319	public function testNice() {
 320		$time = time() + 2 * DAY;
 321		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
 322
 323		$time = time() - 2 * DAY;
 324		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
 325
 326		$time = time();
 327		$this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
 328
 329		$time = 0;
 330		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
 331
 332		$time = null;
 333		$this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
 334
 335		$time = 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'));
 338
 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);
 342
 343		CakeTime::$niceFormat = '%Y-%d-%m %H:%M';
 344		$this->assertEquals(date('Y-d-m H:i', $time), $this->Time->nice($time));
 345		$this->assertEquals('%Y-%d-%m %H:%M', $this->Time->niceFormat);
 346
 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(substr($expected, 0, -1), substr($result, 0, -1));
 351
 352		$this->_restoreSystemTimezone();
 353	}
 354
 355/**
 356 * testNiceShort method
 357 *
 358 * @return void
 359 */
 360	public function testNiceShort() {
 361		$time = time();
 362		$this->assertEquals('Today, ' . date('H:i', $time), $this->Time->niceShort($time));
 363
 364		$time = time() - DAY;
 365		$this->assertEquals('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time));
 366
 367		$time = time() + DAY;
 368		$this->assertEquals('Tomorrow, ' . date('H:i', $time), $this->Time->niceShort($time));
 369
 370		$time = strtotime('+6 days');
 371		$this->assertEquals('On ' . date('l F d, H:i', $time), $this->Time->niceShort($time));
 372
 373		$time = strtotime('-6 days');
 374		$this->assertEquals(date('l F d, H:i', $time), $this->Time->niceShort($time));
 375
 376		date_default_timezone_set('Europe/London');
 377		$result = $this->Time->niceShort('2005-01-15 10:00:00', new DateTimeZone('Europe/Brussels'));
 378		$this->assertEquals('Jan 15th 2005, 11:00', $result);
 379
 380		date_default_timezone_set('UTC');
 381		$result = $this->Time->niceShort(null, 'America/New_York');
 382		$expected = $this->Time->niceShort(time(), 'America/New_York');
 383		$this->assertEquals($expected, $result);
 384
 385		$this->_restoreSystemTimezone();
 386	}
 387
 388/**
 389 * testDaysAsSql method
 390 *
 391 * @return void
 392 */
 393	public function testDaysAsSql() {
 394		$begin = time();
 395		$end = time() + DAY;
 396		$field = 'my_field';
 397		$expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
 398		$this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
 399	}
 400
 401/**
 402 * testDayAsSql method
 403 *
 404 * @return void
 405 */
 406	public function testDayAsSql() {
 407		$time = time();
 408		$field = 'my_field';
 409		$expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
 410		$this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
 411	}
 412
 413/**
 414 * testToUnix method
 415 *
 416 * @return void
 417 */
 418	public function testToUnix() {
 419		$this->assertEquals(time(), $this->Time->toUnix(time()));
 420		$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
 421		$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
 422		$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
 423		$this->assertEquals(false, $this->Time->toUnix(''));
 424		$this->assertEquals(false, $this->Time->toUnix(null));
 425	}
 426
 427/**
 428 * testToServer method
 429 *
 430 * @return void
 431 */
 432	public function testToServer() {
 433		date_default_timezone_set('Europe/Paris');
 434
 435		$time = time();
 436		$this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
 437
 438		date_default_timezone_set('America/New_York');
 439		$time = time();
 440		date_default_timezone_set('Europe/Paris');
 441		$result = $this->Time->toServer($time, 'America/New_York');
 442		$this->assertEquals(date('Y-m-d H:i:s', $time), $result);
 443
 444		date_default_timezone_set('Europe/Paris');
 445		$time = '2005-10-25 10:00:00';
 446		$result = $this->Time->toServer($time);
 447		$date = new DateTime($time, new DateTimeZone('UTC'));
 448		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 449		$expected = $date->format('Y-m-d H:i:s');
 450		$this->assertEquals($expected, $result);
 451
 452		$time = '2002-01-01 05:15:30';
 453		$result = $this->Time->toServer($time, 'America/New_York');
 454		$date = new DateTime($time, new DateTimeZone('America/New_York'));
 455		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 456		$expected = $date->format('Y-m-d H:i:s');
 457		$this->assertEquals($expected, $result);
 458
 459		$time = '2010-01-28T15:00:00+10:00';
 460		$result = $this->Time->toServer($time, 'America/New_York');
 461		$date = new DateTime($time);
 462		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 463		$expected = $date->format('Y-m-d H:i:s');
 464		$this->assertEquals($expected, $result);
 465
 466		$date = new DateTime(null, new DateTimeZone('America/New_York'));
 467		$result = $this->Time->toServer($date, 'Pacific/Tahiti');
 468		$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
 469		$expected = $date->format('Y-m-d H:i:s');
 470		$this->assertEquals($expected, $result);
 471
 472		$this->_restoreSystemTimezone();
 473
 474		$time = time();
 475		$result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
 476		$expected = date('l jS \of F Y h:i:s A', $time);
 477		$this->assertEquals($expected, $result);
 478
 479		$this->assertFalse($this->Time->toServer(time(), new Object()));
 480
 481		date_default_timezone_set('UTC');
 482
 483		$serverTime = new DateTime('2012-12-11 14:15:20');
 484
 485		$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
 486		foreach ($timezones as $timezone) {
 487			$result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
 488			$tz = new DateTimeZone($timezone);
 489			$this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
 490		}
 491
 492		date_default_timezone_set('UTC');
 493		$date = new DateTime('now', new DateTimeZone('America/New_York'));
 494
 495		$result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
 496		$date->setTimezone($this->Time->timezone());
 497		$expected = $date->format('Y-m-d H:i:s');
 498		$this->assertEquals($expected, $result);
 499
 500		$this->_restoreSystemTimezone();
 501	}
 502
 503/**
 504 * testToAtom method
 505 *
 506 * @return void
 507 */
 508	public function testToAtom() {
 509		$this->assertEquals(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
 510	}
 511
 512/**
 513 * testToRss method
 514 *
 515 * @return void
 516 */
 517	public function testToRss() {
 518		$date = '2012-08-12 12:12:45';
 519		$time = strtotime($date);
 520		$this->assertEquals(date('r', $time), $this->Time->toRss($time));
 521
 522		$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
 523		foreach ($timezones as $timezone) {
 524			$yourTimezone = new DateTimeZone($timezone);
 525			$yourTime = new DateTime($date, $yourTimezone);
 526			$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
 527			$time = $yourTime->format('U');
 528			$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone");
 529			$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
 530		}
 531	}
 532
 533/**
 534 * testFormat method
 535 *
 536 * @return void
 537 */
 538	public function testFormat() {
 539		$format = 'D-M-Y';
 540		$tz = date_default_timezone_get();
 541		$arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
 542		foreach ($arr as $val) {
 543			$this->assertEquals(date($format, $val), $this->Time->format($format, $val));
 544			$this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
 545		}
 546
 547		$result = $this->Time->format('Y-m-d', null, 'never');
 548		$this->assertEquals('never', $result);
 549
 550		$result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
 551		$this->assertEquals('13-01-2012', $result);
 552
 553		$result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
 554		$this->assertEquals('invalid', $result);
 555	}
 556
 557/**
 558 * testOfGmt method
 559 *
 560 * @return void
 561 */
 562	public function testGmt() {
 563		$hour = 3;
 564		$min = 4;
 565		$sec = 2;
 566		$month = 5;
 567		$day = 14;
 568		$year = 2007;
 569		$time = mktime($hour, $min, $sec, $month, $day, $year);
 570		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
 571		$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
 572
 573		$hour = date('H');
 574		$min = date('i');
 575		$sec = date('s');
 576		$month = date('m');
 577		$day = date('d');
 578		$year = date('Y');
 579		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
 580		$this->assertEquals($expected, $this->Time->gmt(null));
 581	}
 582
 583/**
 584 * testIsToday method
 585 *
 586 * @return void
 587 */
 588	public function testIsToday() {
 589		$result = $this->Time->isToday('+1 day');
 590		$this->assertFalse($result);
 591		$result = $this->Time->isToday('+1 days');
 592		$this->assertFalse($result);
 593		$result = $this->Time->isToday('+0 day');
 594		$this->assertTrue($result);
 595		$result = $this->Time->isToday('-1 day');
 596		$this->assertFalse($result);
 597	}
 598
 599/**
 600 * testIsThisWeek method
 601 *
 602 * @return void
 603 */
 604	public function testIsThisWeek() {
 605		// A map of days which goes from -1 day of week to +1 day of week
 606		$map = array(
 607			'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
 608			'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
 609			'Sun' => array(-7, 1)
 610		);
 611		$days = $map[date('D')];
 612
 613		for ($day = $days[0] + 1; $day < $days[1]; $day++) {
 614			$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
 615		}
 616		$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
 617		$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
 618	}
 619
 620/**
 621 * testIsThisMonth method
 622 *
 623 * @return void
 624 */
 625	public function testIsThisMonth() {
 626		$result = $this->Time->isThisMonth('+0 day');
 627		$this->assertTrue($result);
 628		$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
 629		$this->assertTrue($result);
 630		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
 631		$this->assertFalse($result);
 632		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
 633		$this->assertFalse($result);
 634	}
 635
 636/**
 637 * testIsThisYear method
 638 *
 639 * @return void
 640 */
 641	public function testIsThisYear() {
 642		$result = $this->Time->isThisYear('+0 day');
 643		$this->assertTrue($result);
 644		$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
 645		$this->assertTrue($result);
 646	}
 647
 648/**
 649 * testWasYesterday method
 650 *
 651 * @return void
 652 */
 653	public function testWasYesterday() {
 654		$result = $this->Time->wasYesterday('+1 day');
 655		$this->assertFalse($result);
 656		$result = $this->Time->wasYesterday('+1 days');
 657		$this->assertFalse($result);
 658		$result = $this->Time->wasYesterday('+0 day');
 659		$this->assertFalse($result);
 660		$result = $this->Time->wasYesterday('-1 day');
 661		$this->assertTrue($result);
 662		$result = $this->Time->wasYesterday('-1 days');
 663		$this->assertTrue($result);
 664		$result = $this->Time->wasYesterday('-2 days');
 665		$this->assertFalse($result);
 666	}
 667
 668/**
 669 * testIsTomorrow method
 670 *
 671 * @return void
 672 */
 673	public function testIsTomorrow() {
 674		$result = $this->Time->isTomorrow('+1 day');
 675		$this->assertTrue($result);
 676		$result = $this->Time->isTomorrow('+1 days');
 677		$this->assertTrue($result);
 678		$result = $this->Time->isTomorrow('+0 day');
 679		$this->assertFalse($result);
 680		$result = $this->Time->isTomorrow('-1 day');
 681		$this->assertFalse($result);
 682	}
 683
 684/**
 685 * testWasWithinLast method
 686 *
 687 * @return void
 688 */
 689	public function testWasWithinLast() {
 690		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
 691		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
 692		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
 693		$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
 694		$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
 695		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
 696		$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
 697		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
 698
 699		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
 700		$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
 701		$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
 702		$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
 703		$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
 704		$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
 705		$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
 706		$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
 707
 708		$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
 709		$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
 710		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
 711		$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
 712		$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
 713		$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
 714
 715		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
 716		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
 717		$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
 718		$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
 719		$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
 720		$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
 721		$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
 722
 723		$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
 724		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
 725		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
 726		$this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
 727	}
 728
 729/**
 730 * testWasWithinLast method
 731 *
 732 * @return void
 733 */
 734	public function testIsWithinNext() {
 735		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
 736		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
 737		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
 738		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
 739		$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
 740		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
 741		$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
 742		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
 743
 744		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
 745		$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
 746		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
 747		$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
 748		$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
 749		$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
 750		$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
 751		$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
 752
 753		$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
 754		$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
 755		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
 756		$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
 757		$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
 758		$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
 759
 760		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
 761		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
 762		$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
 763		$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
 764		$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
 765		$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
 766		$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
 767
 768		$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
 769		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 hour'));
 770		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 minute'));
 771		$this->assertFalse($this->Time->isWithinNext('1   ', '-23 hours -59 minutes -59 seconds'));
 772
 773		$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
 774		$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
 775	}
 776
 777/**
 778 * testUserOffset method
 779 *
 780 * @return void
 781 */
 782	public function testUserOffset() {
 783		$timezoneServer = new DateTimeZone(date_default_timezone_get());
 784		$timeServer = new DateTime('now', $timezoneServer);
 785		$yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
 786
 787		$expected = time();
 788		$result = $this->Time->fromString(time(), $yourTimezone);
 789		$this->assertWithinMargin($expected, $result, 1);
 790
 791		$result = $this->Time->fromString(time(), $timezoneServer->getName());
 792		$this->assertWithinMargin($expected, $result, 1);
 793
 794		$result = $this->Time->fromString(time(), $timezoneServer);
 795		$this->assertWithinMargin($expected, $result, 1);
 796
 797		Configure::write('Config.timezone', $timezoneServer->getName());
 798		$result = $this->Time->fromString(time());
 799		$this->assertWithinMargin($expected, $result, 1);
 800		Configure::delete('Config.timezone');
 801	}
 802
 803/**
 804 * test fromString()
 805 *
 806 * @return void
 807 */
 808	public function testFromString() {
 809		$result = $this->Time->fromString('');
 810		$this->assertFalse($result);
 811
 812		$result = $this->Time->fromString(0, 0);
 813		$this->assertFalse($result);
 814
 815		$result = $this->Time->fromString('+1 hour');
 816		$expected = strtotime('+1 hour');
 817		$this->assertWithinMargin($expected, $result, 1);
 818
 819		$timezone = date('Z', time());
 820		$result = $this->Time->fromString('+1 hour', $timezone);
 821		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
 822		$this->assertWithinMargin($expected, $result, 1);
 823
 824		$timezone = date_default_timezone_get();
 825		$result = $this->Time->fromString('+1 hour', $timezone);
 826		$expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
 827		$this->assertWithinMargin($expected, $result, 1);
 828
 829		date_default_timezone_set('UTC');
 830		$date = new DateTime('now', new DateTimeZone('Europe/London'));
 831		$this->Time->fromString($date);
 832		$this->assertEquals('Europe/London', $date->getTimeZone()->getName());
 833
 834		$this->_restoreSystemTimezone();
 835	}
 836
 837/**
 838 * test fromString() with a DateTime object as the dateString
 839 *
 840 * @return void
 841 */
 842	public function testFromStringWithDateTime() {
 843		date_default_timezone_set('UTC');
 844
 845		$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
 846		$result = $this->Time->fromString($date, 'UTC');
 847		$date->setTimezone(new DateTimeZone('UTC'));
 848		$expected = $date->format('U') + $date->getOffset();
 849
 850		$this->assertWithinMargin($expected, $result, 1);
 851
 852		date_default_timezone_set('Australia/Melbourne');
 853
 854		$date = new DateTime('+1 hour', new DateTimeZone('America/New_York'));
 855		$result = $this->Time->fromString($date, 'Asia/Kuwait');
 856		$date->setTimezone(new DateTimeZone('Asia/Kuwait'));
 857		$expected = $date->format('U') + $date->getOffset();
 858		$this->assertWithinMargin($expected, $result, 1);
 859
 860		$this->_restoreSystemTimezone();
 861	}
 862
 863/**
 864 * test converting time specifiers using a time definition localfe file
 865 *
 866 * @return void
 867 */
 868	public function testConvertSpecifiers() {
 869		App::build(array(
 870			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
 871		), App::RESET);
 872		Configure::write('Config.language', 'time_test');
 873		$time = strtotime('Thu Jan 14 11:43:39 2010');
 874
 875		$result = $this->Time->convertSpecifiers('%a', $time);
 876		$expected = 'jue';
 877		$this->assertEquals($expected, $result);
 878
 879		$result = $this->Time->convertSpecifiers('%A', $time);
 880		$expected = 'jueves';
 881		$this->assertEquals($expected, $result);
 882
 883		$result = $this->Time->convertSpecifiers('%c', $time);
 884		$expected = 'jue %d ene %Y %H:%M:%S %Z';
 885		$this->assertEquals($expected, $result);
 886
 887		$result = $this->Time->convertSpecifiers('%C', $time);
 888		$expected = '20';
 889		$this->assertEquals($expected, $result);
 890
 891		$result = $this->Time->convertSpecifiers('%D', $time);
 892		$expected = '%m/%d/%y';
 893		$this->assertEquals($expected, $result);
 894
 895		$result = $this->Time->convertSpecifiers('%b', $time);
 896		$expected = 'ene';
 897		$this->assertEquals($expected, $result);
 898
 899		$result = $this->Time->convertSpecifiers('%h', $time);
 900		$expected = 'ene';
 901		$this->assertEquals($expected, $result);
 902
 903		$result = $this->Time->convertSpecifiers('%B', $time);
 904		$expected = 'enero';
 905		$this->assertEquals($expected, $result);
 906
 907		$result = $this->Time->convertSpecifiers('%n', $time);
 908		$expected = "\n";
 909		$this->assertEquals($expected, $result);
 910
 911		$result = $this->Time->convertSpecifiers('%n', $time);
 912		$expected = "\n";
 913		$this->assertEquals($expected, $result);
 914
 915		$result = $this->Time->convertSpecifiers('%p', $time);
 916		$expected = 'AM';
 917		$this->assertEquals($expected, $result);
 918
 919		$result = $this->Time->convertSpecifiers('%P', $time);
 920		$expected = 'am';
 921		$this->assertEquals($expected, $result);
 922
 923		$result = $this->Time->convertSpecifiers('%r', $time);
 924		$expected = '%I:%M:%S AM';
 925		$this->assertEquals($expected, $result);
 926
 927		$result = $this->Time->convertSpecifiers('%R', $time);
 928		$expected = '11:43';
 929		$this->assertEquals($expected, $result);
 930
 931		$result = $this->Time->convertSpecifiers('%t', $time);
 932		$expected = "\t";
 933		$this->assertEquals($expected, $result);
 934
 935		$result = $this->Time->convertSpecifiers('%T', $time);
 936		$expected = '%H:%M:%S';
 937		$this->assertEquals($expected, $result);
 938
 939		$result = $this->Time->convertSpecifiers('%u', $time);
 940		$expected = 4;
 941		$this->assertEquals($expected, $result);
 942
 943		$result = $this->Time->convertSpecifiers('%x', $time);
 944		$expected = '%d/%m/%y';
 945		$this->assertEquals($expected, $result);
 946
 947		$result = $this->Time->convertSpecifiers('%X', $time);
 948		$expected = '%H:%M:%S';
 949		$this->assertEquals($expected, $result);
 950	}
 951
 952/**
 953 * test convert %e on windows.
 954 *
 955 * @return void
 956 */
 957	public function testConvertPercentE() {
 958		$this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
 959
 960		$time = strtotime('Thu Jan 14 11:43:39 2010');
 961		$result = $this->Time->convertSpecifiers('%e', $time);
 962		$expected = '14';
 963		$this->assertEquals($expected, $result);
 964
 965		$result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
 966		$expected = ' 1';
 967		$this->assertEquals($expected, $result);
 968	}
 969
 970/**
 971 * test formatting dates taking in account preferred i18n locale file
 972 *
 973 * @return void
 974 */
 975	public function testI18nFormat() {
 976		App::build(array(
 977			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
 978		), App::RESET);
 979		Configure::write('Config.language', 'time_test');
 980
 981		$time = strtotime('Thu Jan 14 13:59:28 2010');
 982
 983		$result = $this->Time->i18nFormat($time);
 984		$expected = '14/01/10';
 985		$this->assertEquals($expected, $result);
 986
 987		$result = $this->Time->i18nFormat($time, '%c');
 988		$expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
 989		$this->assertEquals($expected, $result);
 990
 991		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
 992		$expected = 'Time is 01:59:28 PM, and date is 14/01/10';
 993		$this->assertEquals($expected, $result);
 994
 995		$time = strtotime('Wed Jan 13 13:59:28 2010');
 996
 997		$result = $this->Time->i18nFormat($time);
 998		$expected = '13/01/10';
 999		$this->assertEquals($expected, $result);
1000
1001		$result = $this->Time->i18nFormat($time, '%c');
1002		$expected = 'miĂŠ 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
1003		$this->assertEquals($expected, $result);
1004
1005		$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
1006		$expected = 'Time is 01:59:28 PM, and date is 13/01/10';
1007		$this->assertEquals($expected, $result);
1008
1009		$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
1010		$expected = 'Date invalid';
1011		$this->assertEquals($expected, $result);
1012	}
1013
1014/**
1015 * test new format() syntax which inverts first and second parameters
1016 *
1017 * @return void
1018 */
1019	public function testFormatNewSyntax() {
1020		$time = time();
1021		$this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
1022		$this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
1023	}
1024
1025/**
1026 * testListTimezones
1027 *
1028 * @return void
1029 */
1030	public function testListTimezones() {
1031		$return = CakeTime::listTimezones();
1032		$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1033		$this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
1034		$this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
1035		$this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
1036		$this->assertTrue(isset($return['UTC']['UTC']));
1037		$this->assertFalse(isset($return['Cuba']));
1038		$this->assertFalse(isset($return['US']));
1039
1040		$return = CakeTime::listTimezones('#^Asia/#');
1041		$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1042		$this->assertFalse(isset($return['Pacific']));
1043
1044		$return = CakeTime::listTimezones('#^(America|Pacific)/#', null, false);
1045		$this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
1046		$this->assertTrue(isset($return['Pacific/Tahiti']));
1047
1048		if (!$this->skipIf(version_compare(PHP_VERSION, '5.3.0', '<'))) {
1049			$return = CakeTime::listTimezones(DateTimeZone::ASIA);
1050			$this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
1051			$this->assertFalse(isset($return['Pacific']));
1052
1053			$return = CakeTime::listTimezones(DateTimeZone::PER_COUNTRY, 'US', false);
1054			$this->assertTrue(isset($return['Pacific/Honolulu']));
1055			$this->assertFalse(isset($return['Asia/Bangkok']));
1056		}
1057	}
1058
1059/**
1060 * Tests that using CakeTime::format() with the correct sytax actually converts
1061 * from one timezone to the other correctly
1062 *
1063 * @return void
1064 */
1065	public function testCorrectTimezoneConversion() {
1066		date_default_timezone_set('UTC');
1067		$date = '2012-01-01 10:00:00';
1068		$converted = CakeTime::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
1069		$expected = new DateTime($date);
1070		$expected->setTimezone(new DateTimeZone('Europe/Copenhagen'));
1071		$this->assertEquals($expected->format('Y-m-d H:i'), $converted);
1072	}
1073
1074}