PageRenderTime 133ms CodeModel.GetById 70ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 0ms

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

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