PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

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