TimeHelperTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\Configure;
  17. use Cake\I18n\I18n;
  18. use Cake\I18n\Time;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\View\Helper\TimeHelper;
  21. use Cake\View\View;
  22. /**
  23. * TimeHelperTest class
  24. *
  25. */
  26. class TimeHelperTest extends TestCase
  27. {
  28. public $Time = null;
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. $this->View = new View();
  38. $this->Time = new TimeHelper($this->View);
  39. Time::$defaultLocale = 'en_US';
  40. $this->locale = I18n::locale();
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. parent::tearDown();
  50. Time::$defaultLocale = 'en_US';
  51. I18n::locale($this->locale);
  52. }
  53. /**
  54. * Test element wrapping in timeAgoInWords
  55. *
  56. * @return void
  57. */
  58. public function testTimeAgoInWords()
  59. {
  60. $Time = new TimeHelper($this->View);
  61. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  62. $result = $Time->timeAgoInWords($timestamp, [
  63. 'end' => '1 years',
  64. 'element' => 'span'
  65. ]);
  66. $expected = [
  67. 'span' => [
  68. 'title' => $timestamp,
  69. 'class' => 'time-ago-in-words'
  70. ],
  71. 'on ' . date('n/j/y', $timestamp),
  72. '/span'
  73. ];
  74. $this->assertHtml($expected, $result);
  75. $result = $Time->timeAgoInWords($timestamp, [
  76. 'end' => '1 years',
  77. 'element' => [
  78. 'title' => 'testing',
  79. 'rel' => 'test'
  80. ]
  81. ]);
  82. $expected = [
  83. 'span' => [
  84. 'title' => 'testing',
  85. 'class' => 'time-ago-in-words',
  86. 'rel' => 'test'
  87. ],
  88. 'on ' . date('n/j/y', $timestamp),
  89. '/span'
  90. ];
  91. $this->assertHtml($expected, $result);
  92. $timestamp = strtotime('+2 weeks');
  93. $result = $Time->timeAgoInWords(
  94. $timestamp,
  95. ['end' => '1 years', 'element' => 'div']
  96. );
  97. $expected = [
  98. 'div' => [
  99. 'title' => $timestamp,
  100. 'class' => 'time-ago-in-words'
  101. ],
  102. '2 weeks',
  103. '/div'
  104. ];
  105. $this->assertHtml($expected, $result);
  106. }
  107. /**
  108. * Test output timezone with timeAgoInWords
  109. *
  110. * @return void
  111. */
  112. public function testTimeAgoInWordsOutputTimezone()
  113. {
  114. $Time = new TimeHelper($this->View, ['outputTimezone' => 'America/Vancouver']);
  115. $timestamp = new Time('+8 years, +4 months +2 weeks +3 days');
  116. $result = $Time->timeAgoInWords($timestamp, [
  117. 'end' => '1 years',
  118. 'element' => 'span'
  119. ]);
  120. $vancouver = clone $timestamp;
  121. $vancouver->timezone('America/Vancouver');
  122. $expected = [
  123. 'span' => [
  124. 'title' => $vancouver->__toString(),
  125. 'class' => 'time-ago-in-words'
  126. ],
  127. 'on ' . $vancouver->format('n/j/y'),
  128. '/span'
  129. ];
  130. $this->assertHtml($expected, $result);
  131. }
  132. /**
  133. * testToQuarter method
  134. *
  135. * @return void
  136. */
  137. public function testToQuarter()
  138. {
  139. $this->assertEquals(4, $this->Time->toQuarter('2007-12-25'));
  140. $this->assertEquals(['2007-10-01', '2007-12-31'], $this->Time->toQuarter('2007-12-25', true));
  141. }
  142. /**
  143. * testNice method
  144. *
  145. * @return void
  146. */
  147. public function testNice()
  148. {
  149. $time = '2014-04-20 20:00';
  150. $this->assertTimeFormat('Apr 20, 2014, 8:00 PM', $this->Time->nice($time));
  151. $result = $this->Time->nice($time, 'America/New_York');
  152. $this->assertTimeFormat('Apr 20, 2014, 4:00 PM', $result);
  153. }
  154. /**
  155. * testToUnix method
  156. *
  157. * @return void
  158. */
  159. public function testToUnix()
  160. {
  161. $this->assertEquals(1397980800, $this->Time->toUnix('2014-04-20 08:00:00'));
  162. }
  163. /**
  164. * testToAtom method
  165. *
  166. * @return void
  167. */
  168. public function testToAtom()
  169. {
  170. $dateTime = new \DateTime;
  171. $this->assertEquals($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  172. }
  173. /**
  174. * testToAtom method
  175. *
  176. * @return void
  177. */
  178. public function testToAtomOutputTimezone()
  179. {
  180. $this->Time->config('outputTimezone', 'America/Vancouver');
  181. $dateTime = new Time;
  182. $vancouver = clone $dateTime;
  183. $vancouver->timezone('America/Vancouver');
  184. $this->assertEquals($vancouver->format(Time::ATOM), $this->Time->toAtom($vancouver));
  185. }
  186. /**
  187. * testToRss method
  188. *
  189. * @return void
  190. */
  191. public function testToRss()
  192. {
  193. $date = '2012-08-12 12:12:45';
  194. $time = strtotime($date);
  195. $this->assertEquals(date('r', $time), $this->Time->toRss($time));
  196. $timezones = ['Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'];
  197. foreach ($timezones as $timezone) {
  198. $yourTimezone = new \DateTimeZone($timezone);
  199. $yourTime = new \DateTime($date, $yourTimezone);
  200. $time = $yourTime->format('U');
  201. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  202. }
  203. }
  204. /**
  205. * test toRss with outputTimezone
  206. *
  207. * @return void
  208. */
  209. public function testToRssOutputTimezone()
  210. {
  211. $this->markTestIncomplete();
  212. }
  213. /**
  214. * testOfGmt method
  215. *
  216. * @return void
  217. */
  218. public function testGmt()
  219. {
  220. $this->assertEquals(1397980800, $this->Time->gmt('2014-04-20 08:00:00'));
  221. }
  222. /**
  223. * testIsToday method
  224. *
  225. * @return void
  226. */
  227. public function testIsToday()
  228. {
  229. $result = $this->Time->isToday('+1 day');
  230. $this->assertFalse($result);
  231. $result = $this->Time->isToday('+1 days');
  232. $this->assertFalse($result);
  233. $result = $this->Time->isToday('+0 day');
  234. $this->assertTrue($result);
  235. $result = $this->Time->isToday('-1 day');
  236. $this->assertFalse($result);
  237. }
  238. /**
  239. * test isToday with outputTimezone
  240. *
  241. * @return void
  242. */
  243. public function testIsTodayOutputTimezone()
  244. {
  245. $this->markTestIncomplete();
  246. }
  247. /**
  248. * testIsFuture method
  249. *
  250. * @return void
  251. */
  252. public function testIsFuture()
  253. {
  254. $this->assertTrue($this->Time->isFuture('+1 month'));
  255. $this->assertTrue($this->Time->isFuture('+1 days'));
  256. $this->assertTrue($this->Time->isFuture('+1 minute'));
  257. $this->assertTrue($this->Time->isFuture('+1 second'));
  258. $this->assertFalse($this->Time->isFuture('-1 second'));
  259. $this->assertFalse($this->Time->isFuture('-1 day'));
  260. $this->assertFalse($this->Time->isFuture('-1 week'));
  261. $this->assertFalse($this->Time->isFuture('-1 month'));
  262. }
  263. /**
  264. * test isFuture with outputTimezone
  265. *
  266. * @return void
  267. */
  268. public function testIsFutureOutputTimezone()
  269. {
  270. $this->markTestIncomplete();
  271. }
  272. /**
  273. * testIsPast method
  274. *
  275. * @return void
  276. */
  277. public function testIsPast()
  278. {
  279. $this->assertFalse($this->Time->isPast('+1 month'));
  280. $this->assertFalse($this->Time->isPast('+1 days'));
  281. $this->assertFalse($this->Time->isPast('+1 minute'));
  282. $this->assertFalse($this->Time->isPast('+1 second'));
  283. $this->assertTrue($this->Time->isPast('-1 second'));
  284. $this->assertTrue($this->Time->isPast('-1 day'));
  285. $this->assertTrue($this->Time->isPast('-1 week'));
  286. $this->assertTrue($this->Time->isPast('-1 month'));
  287. }
  288. /**
  289. * test isPast with outputTimezone
  290. *
  291. * @return void
  292. */
  293. public function testIsPastOutputTimezone()
  294. {
  295. $this->markTestIncomplete();
  296. }
  297. /**
  298. * testIsThisWeek method
  299. *
  300. * @return void
  301. */
  302. public function testIsThisWeek()
  303. {
  304. // A map of days which goes from -1 day of week to +1 day of week
  305. $map = [
  306. 'Mon' => [-1, 7], 'Tue' => [-2, 6], 'Wed' => [-3, 5],
  307. 'Thu' => [-4, 4], 'Fri' => [-5, 3], 'Sat' => [-6, 2],
  308. 'Sun' => [-7, 1]
  309. ];
  310. $days = $map[date('D')];
  311. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  312. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  313. }
  314. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  315. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  316. }
  317. /**
  318. * test isThisWeek with outputTimezone
  319. *
  320. * @return void
  321. */
  322. public function testIsThisWeekOutputTimezone()
  323. {
  324. $this->markTestIncomplete();
  325. }
  326. /**
  327. * testIsThisMonth method
  328. *
  329. * @return void
  330. */
  331. public function testIsThisMonth()
  332. {
  333. $result = $this->Time->isThisMonth('+0 day');
  334. $this->assertTrue($result);
  335. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  336. $this->assertTrue($result);
  337. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  338. $this->assertFalse($result);
  339. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  340. $this->assertFalse($result);
  341. }
  342. /**
  343. * test isThisMonth with outputTimezone
  344. *
  345. * @return void
  346. */
  347. public function testIsThisMonthOutputTimezone()
  348. {
  349. $this->markTestIncomplete();
  350. }
  351. /**
  352. * testIsThisYear method
  353. *
  354. * @return void
  355. */
  356. public function testIsThisYear()
  357. {
  358. $result = $this->Time->isThisYear('+0 day');
  359. $this->assertTrue($result);
  360. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  361. $this->assertTrue($result);
  362. }
  363. /**
  364. * test isThisYear with outputTimezone
  365. *
  366. * @return void
  367. */
  368. public function testIsThisYearOutputTimezone()
  369. {
  370. $this->markTestIncomplete();
  371. }
  372. /**
  373. * testWasYesterday method
  374. *
  375. * @return void
  376. */
  377. public function testWasYesterday()
  378. {
  379. $result = $this->Time->wasYesterday('+1 day');
  380. $this->assertFalse($result);
  381. $result = $this->Time->wasYesterday('+1 days');
  382. $this->assertFalse($result);
  383. $result = $this->Time->wasYesterday('+0 day');
  384. $this->assertFalse($result);
  385. $result = $this->Time->wasYesterday('-1 day');
  386. $this->assertTrue($result);
  387. $result = $this->Time->wasYesterday('-1 days');
  388. $this->assertTrue($result);
  389. $result = $this->Time->wasYesterday('-2 days');
  390. $this->assertFalse($result);
  391. }
  392. /**
  393. * test wasYesterday with outputTimezone
  394. *
  395. * @return void
  396. */
  397. public function testWasYesterdayOutputTimezone()
  398. {
  399. $this->markTestIncomplete();
  400. }
  401. /**
  402. * testIsTomorrow method
  403. *
  404. * @return void
  405. */
  406. public function testIsTomorrow()
  407. {
  408. $result = $this->Time->isTomorrow('+1 day');
  409. $this->assertTrue($result);
  410. $result = $this->Time->isTomorrow('+1 days');
  411. $this->assertTrue($result);
  412. $result = $this->Time->isTomorrow('+0 day');
  413. $this->assertFalse($result);
  414. $result = $this->Time->isTomorrow('-1 day');
  415. $this->assertFalse($result);
  416. }
  417. /**
  418. * test isTomorrow with outputTimezone
  419. *
  420. * @return void
  421. */
  422. public function testIsTomorrowOutputTimezone()
  423. {
  424. $this->markTestIncomplete();
  425. }
  426. /**
  427. * testWasWithinLast method
  428. *
  429. * @return void
  430. */
  431. public function testWasWithinLast()
  432. {
  433. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  434. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  435. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  436. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  437. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  438. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  439. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  440. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  441. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  442. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  443. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  444. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  445. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  446. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  447. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  448. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  449. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  450. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  451. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  452. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  453. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  454. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  455. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  456. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  457. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  458. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  459. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  460. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  461. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  462. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  463. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  464. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  465. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  466. }
  467. /**
  468. * test wasWithinLast with outputTimezone
  469. *
  470. * @return void
  471. */
  472. public function testWasWithinLastOutputTimezone()
  473. {
  474. $this->markTestIncomplete();
  475. }
  476. /**
  477. * testWasWithinLast method
  478. *
  479. * @return void
  480. */
  481. public function testIsWithinNext()
  482. {
  483. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  484. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  485. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  486. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  487. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  488. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  489. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  490. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  491. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  492. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  493. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  494. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  495. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  496. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  497. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  498. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  499. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  500. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  501. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  502. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  503. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  504. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  505. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  506. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  507. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  508. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  509. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  510. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  511. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  512. $this->assertTrue($this->Time->isWithinNext('1 day', '+1 day'));
  513. $this->assertTrue($this->Time->isWithinNext('7 day', '+1 week'));
  514. $this->assertTrue($this->Time->isWithinNext('1 minute', '+1 second'));
  515. $this->assertTrue($this->Time->isWithinNext('1 month', '+1 month'));
  516. }
  517. /**
  518. * test isWithinNext with outputTimezone
  519. *
  520. * @return void
  521. */
  522. public function testIsWithinNextOutputTimezone()
  523. {
  524. $this->markTestIncomplete();
  525. }
  526. /**
  527. * test formatting dates taking in account preferred i18n locale file
  528. *
  529. * @return void
  530. */
  531. public function testFormat()
  532. {
  533. $time = strtotime('Thu Jan 14 13:59:28 2010');
  534. $result = $this->Time->format($time);
  535. $expected = '1/14/10, 1:59 PM';
  536. $this->assertTimeFormat($expected, $result);
  537. $result = $this->Time->format($time, \IntlDateFormatter::FULL);
  538. $expected = 'Thursday, January 14, 2010 at 1:59:28 PM GMT';
  539. $this->assertTimeFormat($expected, $result);
  540. $result = $this->Time->format('invalid date', null, 'Date invalid');
  541. $expected = 'Date invalid';
  542. $this->assertEquals($expected, $result);
  543. I18n::locale('fr_FR');
  544. Time::$defaultLocale = 'fr_FR';
  545. $time = new \Cake\I18n\FrozenTime('Thu Jan 14 13:59:28 2010');
  546. $result = $this->Time->format($time, \IntlDateFormatter::FULL);
  547. $expected = 'jeudi 14 janvier 2010 13:59:28 UTC';
  548. $this->assertTimeFormat($expected, $result);
  549. }
  550. /**
  551. * test format with outputTimezone
  552. *
  553. * @return void
  554. */
  555. public function testFormatOutputTimezone()
  556. {
  557. $this->markTestIncomplete();
  558. }
  559. /**
  560. * Test format() with a string.
  561. *
  562. * @return void
  563. */
  564. public function testFormatString()
  565. {
  566. $time = '2010-01-14 13:59:28';
  567. $result = $this->Time->format($time);
  568. $this->assertTimeFormat('1/14/10 1:59 PM', $result);
  569. $result = $this->Time->format($time, 'HH:mm', null, 'America/New_York');
  570. $this->assertTimeFormat('08:59', $result);
  571. }
  572. /**
  573. * Test format() with a Time instance.
  574. *
  575. * @return void
  576. */
  577. public function testFormatTimeInstance()
  578. {
  579. $time = new Time('2010-01-14 13:59:28', 'America/New_York');
  580. $result = $this->Time->format($time, 'HH:mm', null, 'America/New_York');
  581. $this->assertTimeFormat('13:59', $result);
  582. $time = new Time('2010-01-14 13:59:28', 'UTC');
  583. $result = $this->Time->format($time, 'HH:mm', null, 'America/New_York');
  584. $this->assertTimeFormat('08:59', $result);
  585. }
  586. /**
  587. * Custom assert to allow for variation in the version of the intl library, where
  588. * some translations contain a few extra commas.
  589. *
  590. * @param string $expected
  591. * @param string $result
  592. * @return void
  593. */
  594. public function assertTimeFormat($expected, $result)
  595. {
  596. return $this->assertEquals(
  597. str_replace([',', '(', ')', ' at', ' à'], '', $expected),
  598. str_replace([',', '(', ')', ' at', ' à'], '', $result)
  599. );
  600. }
  601. /**
  602. * Test formatting in case the $time parameter is not set
  603. *
  604. * @return void
  605. */
  606. public function testNullDateFormat()
  607. {
  608. $result = $this->Time->format(null);
  609. $this->assertSame(false, $result);
  610. $fallback = 'Date invalid or not set';
  611. $result = $this->Time->format(null, \IntlDateFormatter::FULL, $fallback);
  612. $this->assertEquals($fallback, $result);
  613. }
  614. }