TimeHelperTest.php 21 KB

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