TimeTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. /**
  3. * TimeTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Utility;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Time;
  22. /**
  23. * TimeTest class
  24. *
  25. */
  26. class TimeTest extends TestCase {
  27. /**
  28. * Default system timezone identifier
  29. *
  30. * @var string
  31. */
  32. protected $_systemTimezoneIdentifier = null;
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $this->now = Time::getTestNow();
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown() {
  48. parent::tearDown();
  49. Time::setTestNow($this->now);
  50. }
  51. /**
  52. * Restored the original system timezone
  53. *
  54. * @return void
  55. */
  56. protected function _restoreSystemTimezone() {
  57. date_default_timezone_set($this->_systemTimezoneIdentifier);
  58. }
  59. /**
  60. * Provides values and expectations for the toQuarter method
  61. *
  62. * @return array
  63. */
  64. public function toQuarterProvider() {
  65. return [
  66. ['2007-12-25', 4],
  67. ['2007-9-25', 3],
  68. ['2007-3-25', 1],
  69. ['2007-3-25', ['2007-01-01', '2007-03-31'], true],
  70. ['2007-5-25', ['2007-04-01', '2007-06-30'], true],
  71. ['2007-8-25', ['2007-07-01', '2007-09-30'], true],
  72. ['2007-12-25', ['2007-10-01', '2007-12-31'], true],
  73. ];
  74. }
  75. /**
  76. * testToQuarter method
  77. *
  78. * @dataProvider toQuarterProvider
  79. * @return void
  80. */
  81. public function testToQuarter($date, $expected, $range = false) {
  82. $this->assertEquals($expected, (new Time($date))->toQuarter($range));
  83. }
  84. /**
  85. * provider for timeAgoInWords() tests
  86. *
  87. * @return array
  88. */
  89. public static function timeAgoProvider() {
  90. return array(
  91. array('-12 seconds', '12 seconds ago'),
  92. array('-12 minutes', '12 minutes ago'),
  93. array('-2 hours', '2 hours ago'),
  94. array('-1 day', '1 day ago'),
  95. array('-2 days', '2 days ago'),
  96. array('-2 days -3 hours', '2 days, 3 hours ago'),
  97. array('-1 week', '1 week ago'),
  98. array('-2 weeks -2 days', '2 weeks, 2 days ago'),
  99. array('+1 week', '1 week'),
  100. array('+1 week 1 day', '1 week, 1 day'),
  101. array('+2 weeks 2 day', '2 weeks, 2 days'),
  102. array('2007-9-24', 'on 24/9/07'),
  103. array('now', 'just now'),
  104. );
  105. }
  106. /**
  107. * testTimeAgoInWords method
  108. *
  109. * @dataProvider timeAgoProvider
  110. * @return void
  111. */
  112. public function testTimeAgoInWords($input, $expected) {
  113. $time = new Time($input);
  114. $result = $time->timeAgoInWords();
  115. $this->assertEquals($expected, $result);
  116. }
  117. /**
  118. * provider for timeAgo with an end date.
  119. *
  120. * @return void
  121. */
  122. public function timeAgoEndProvider() {
  123. return array(
  124. array(
  125. '+4 months +2 weeks +3 days',
  126. '4 months, 2 weeks, 3 days',
  127. '8 years'
  128. ),
  129. array(
  130. '+4 months +2 weeks +1 day',
  131. '4 months, 2 weeks, 1 day',
  132. '8 years'
  133. ),
  134. array(
  135. '+3 months +2 weeks',
  136. '3 months, 2 weeks',
  137. '8 years'
  138. ),
  139. array(
  140. '+3 months +2 weeks +1 day',
  141. '3 months, 2 weeks, 1 day',
  142. '8 years'
  143. ),
  144. array(
  145. '+1 months +1 week +1 day',
  146. '1 month, 1 week, 1 day',
  147. '8 years'
  148. ),
  149. array(
  150. '+2 months +2 days',
  151. '2 months, 2 days',
  152. '+2 months +2 days'
  153. ),
  154. array(
  155. '+2 months +12 days',
  156. '2 months, 1 week, 5 days',
  157. '3 months'
  158. ),
  159. );
  160. }
  161. /**
  162. * test the end option for timeAgoInWords
  163. *
  164. * @dataProvider timeAgoEndProvider
  165. * @return void
  166. */
  167. public function testTimeAgoInWordsEnd($input, $expected, $end) {
  168. $time = new Time($input);
  169. $result = $time->timeAgoInWords(array('end' => $end));
  170. $this->assertEquals($expected, $result);
  171. }
  172. /**
  173. * test the custom string options for timeAgoInWords
  174. *
  175. * @return void
  176. */
  177. public function testTimeAgoInWordsCustomStrings() {
  178. $time = new Time('-8 years -4 months -2 weeks -3 days');
  179. $result = $time->timeAgoInWords(array(
  180. 'relativeString' => 'at least %s ago',
  181. 'accuracy' => array('year' => 'year'),
  182. 'end' => '+10 years'
  183. ));
  184. $expected = 'at least 8 years ago';
  185. $this->assertEquals($expected, $result);
  186. $time = new Time('+4 months +2 weeks +3 days');
  187. $result = $time->timeAgoInWords(array(
  188. 'absoluteString' => 'exactly on %s',
  189. 'accuracy' => array('year' => 'year'),
  190. 'end' => '+2 months'
  191. ));
  192. $expected = 'exactly on ' . date('j/n/y', strtotime('+4 months +2 weeks +3 days'));
  193. $this->assertEquals($expected, $result);
  194. }
  195. /**
  196. * Test the accuracy option for timeAgoInWords()
  197. *
  198. * @return void
  199. */
  200. public function testTimeAgoInWordsAccuracy() {
  201. $time = new Time('+8 years +4 months +2 weeks +3 days');
  202. $result = $time->timeAgoInWords(array(
  203. 'accuracy' => array('year' => 'year'),
  204. 'end' => '+10 years'
  205. ));
  206. $expected = '8 years';
  207. $this->assertEquals($expected, $result);
  208. $time = new Time('+8 years +4 months +2 weeks +3 days');
  209. $result = $time->timeAgoInWords(array(
  210. 'accuracy' => array('year' => 'month'),
  211. 'end' => '+10 years'
  212. ));
  213. $expected = '8 years, 4 months';
  214. $this->assertEquals($expected, $result);
  215. $time = new Time('+8 years +4 months +2 weeks +3 days');
  216. $result = $time->timeAgoInWords(array(
  217. 'accuracy' => array('year' => 'week'),
  218. 'end' => '+10 years'
  219. ));
  220. $expected = '8 years, 4 months, 2 weeks';
  221. $this->assertEquals($expected, $result);
  222. $time = new Time('+8 years +4 months +2 weeks +3 days');
  223. $result = $time->timeAgoInWords(array(
  224. 'accuracy' => array('year' => 'day'),
  225. 'end' => '+10 years'
  226. ));
  227. $expected = '8 years, 4 months, 2 weeks, 3 days';
  228. $this->assertEquals($expected, $result);
  229. $time = new Time('+1 years +5 weeks');
  230. $result = $time->timeAgoInWords(array(
  231. 'accuracy' => array('year' => 'year'),
  232. 'end' => '+10 years'
  233. ));
  234. $expected = '1 year';
  235. $this->assertEquals($expected, $result);
  236. $time = new Time('+58 minutes');
  237. $result = $time->timeAgoInWords(array(
  238. 'accuracy' => 'hour'
  239. ));
  240. $expected = 'in about an hour';
  241. $this->assertEquals($expected, $result);
  242. $time = new Time('+23 hours');
  243. $result = $time->timeAgoInWords(array(
  244. 'accuracy' => 'day'
  245. ));
  246. $expected = 'in about a day';
  247. $this->assertEquals($expected, $result);
  248. }
  249. /**
  250. * Test the format option of timeAgoInWords()
  251. *
  252. * @return void
  253. */
  254. public function testTimeAgoInWordsWithFormat() {
  255. $time = new Time('2007-9-25');
  256. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  257. $this->assertEquals('on 2007-09-25', $result);
  258. $time = new Time('2007-9-25');
  259. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  260. $this->assertEquals('on 2007-09-25', $result);
  261. $time = new Time('+2 weeks +2 days');
  262. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  263. $this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
  264. $time = new Time('+2 months +2 days');
  265. $result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'Y-m-d'));
  266. $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
  267. }
  268. /**
  269. * test timeAgoInWords() with negative values.
  270. *
  271. * @return void
  272. */
  273. public function testTimeAgoInWordsNegativeValues() {
  274. $time = new Time('-2 months -2 days');
  275. $result = $time->timeAgoInWords(array('end' => '3 month'));
  276. $this->assertEquals('2 months, 2 days ago', $result);
  277. $time = new Time('-2 months -2 days');
  278. $result = $time->timeAgoInWords(array('end' => '3 month'));
  279. $this->assertEquals('2 months, 2 days ago', $result);
  280. $time = new Time('-2 months -2 days');
  281. $result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'Y-m-d'));
  282. $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
  283. $time = new Time('-2 years -5 months -2 days');
  284. $result = $time->timeAgoInWords(array('end' => '3 years'));
  285. $this->assertEquals('2 years, 5 months, 2 days ago', $result);
  286. $time = new Time('-2 weeks -2 days');
  287. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  288. $this->assertEquals('2 weeks, 2 days ago', $result);
  289. $time = new Time('-3 years -12 months');
  290. $result = $time->timeAgoInWords();
  291. $expected = 'on ' . $time->format('j/n/y');
  292. $this->assertEquals($expected, $result);
  293. $time = new Time('-1 month -1 week -6 days');
  294. $result = $time->timeAgoInWords(
  295. array('end' => '1 year', 'accuracy' => array('month' => 'month'))
  296. );
  297. $this->assertEquals('1 month ago', $result);
  298. $time = new Time('-1 years -2 weeks -3 days');
  299. $result = $time->timeAgoInWords(
  300. array('accuracy' => array('year' => 'year'))
  301. );
  302. $expected = 'on ' . $time->format('j/n/y');
  303. $this->assertEquals($expected, $result);
  304. $time = new Time('-13 months -5 days');
  305. $result = $time->timeAgoInWords(array('end' => '2 years'));
  306. $this->assertEquals('1 year, 1 month, 5 days ago', $result);
  307. $time = new Time('-58 minutes');
  308. $result = $time->timeAgoInWords(array('accuracy' => 'hour'));
  309. $this->assertEquals('about an hour ago', $result);
  310. $time = new Time('-23 hours');
  311. $result = $time->timeAgoInWords(array('accuracy' => 'day'));
  312. $this->assertEquals('about a day ago', $result);
  313. }
  314. /**
  315. * testNice method
  316. *
  317. * @return void
  318. */
  319. public function testNice() {
  320. $time = new Time('2014-04-20 20:00', 'UTC');
  321. $this->assertEquals('Apr 20, 2014, 8:00 PM', $time->nice());
  322. $result = $time->nice('America/New_York');
  323. $this->assertEquals('Apr 20, 2014, 4:00 PM', $result);
  324. $this->assertEquals('UTC', $time->getTimezone()->getName());
  325. $this->assertEquals('20 avr. 2014 20:00', $time->nice(null, 'fr-FR'));
  326. $this->assertEquals('20 avr. 2014 16:00', $time->nice('America/New_York', 'fr-FR'));
  327. }
  328. /**
  329. * testToUnix method
  330. *
  331. * @return void
  332. */
  333. public function testToUnix() {
  334. $time = new Time('2014-04-20 08:00:00');
  335. $this->assertEquals('1397980800', $time->toUnixString());
  336. $time = new Time('2021-12-11 07:00:01');
  337. $this->assertEquals('1639206001', $time->toUnixString());
  338. }
  339. /**
  340. * testFormat method
  341. *
  342. * @return void
  343. */
  344. public function testFormat() {
  345. $format = 'D-M-Y';
  346. $tz = date_default_timezone_get();
  347. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  348. foreach ($arr as $val) {
  349. $this->assertEquals(date($format, $val), $this->Time->format($format, $val));
  350. $this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
  351. }
  352. $result = $this->Time->format('Y-m-d', null, 'never');
  353. $this->assertEquals('never', $result);
  354. $result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
  355. $this->assertEquals('13-01-2012', $result);
  356. $result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
  357. $this->assertEquals('invalid', $result);
  358. $result = $this->Time->format('0000-00-00', '%d-%m-%Y', 'invalid');
  359. $this->assertEquals('invalid', $result);
  360. }
  361. /**
  362. * testIsThisWeek method
  363. *
  364. * @return void
  365. */
  366. public function testIsThisWeek() {
  367. $time = new Time('this sunday');
  368. $this->assertTrue($time->isThisWeek());
  369. $this->assertTrue($time->modify('-1 day')->isThisWeek());
  370. $this->assertFalse($time->modify('-6 days')->isThisWeek());
  371. $time = new Time();
  372. $time->year = $time->year - 1;
  373. $this->assertFalse($time->isThisWeek());
  374. }
  375. /**
  376. * testIsThisMonth method
  377. *
  378. * @return void
  379. */
  380. public function testIsThisMonth() {
  381. $time = new Time();
  382. $this->assertTrue($time->isThisMonth());
  383. $time->year = $time->year + 1;
  384. $this->assertFalse($time->isThisMonth());
  385. $time = new Time();
  386. $this->assertFalse($time->modify('next month')->isThisMonth());
  387. }
  388. /**
  389. * testIsThisYear method
  390. *
  391. * @return void
  392. */
  393. public function testIsThisYear() {
  394. $time = new Time();
  395. $this->assertTrue($time->isThisYear());
  396. $time->year = $time->year + 1;
  397. $this->assertFalse($time->isThisYear());
  398. $thisYear = date('Y');
  399. $time = new Time("$thisYear-01-01 00:00", 'Australia/Sydney');
  400. $now = clone $time;
  401. $now->timezone('UTC');
  402. Time::setTestNow($now);
  403. $this->assertFalse($time->isThisYear());
  404. }
  405. /**
  406. * testIsTomorrow method
  407. *
  408. * @return void
  409. */
  410. public function testIsTomorrow() {
  411. $result = $this->Time->isTomorrow('+1 day');
  412. $this->assertTrue($result);
  413. $result = $this->Time->isTomorrow('+1 days');
  414. $this->assertTrue($result);
  415. $result = $this->Time->isTomorrow('+0 day');
  416. $this->assertFalse($result);
  417. $result = $this->Time->isTomorrow('-1 day');
  418. $this->assertFalse($result);
  419. }
  420. /**
  421. * testWasWithinLast method
  422. *
  423. * @return void
  424. */
  425. public function testWasWithinLast() {
  426. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  427. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  428. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  429. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  430. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  431. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  432. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  433. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  434. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  435. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  436. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  437. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  438. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  439. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  440. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  441. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  442. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  443. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  444. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  445. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  446. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  447. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  448. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  449. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  450. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  451. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  452. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  453. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  454. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  455. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  456. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  457. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  458. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  459. }
  460. /**
  461. * testWasWithinLast method
  462. *
  463. * @return void
  464. */
  465. public function testIsWithinNext() {
  466. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  467. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  468. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  469. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  470. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  471. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  472. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  473. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  474. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  475. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  476. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  477. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  478. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  479. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  480. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  481. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  482. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  483. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  484. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  485. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  486. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  487. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  488. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  489. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  490. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  491. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  492. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  493. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  494. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  495. $this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
  496. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
  497. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
  498. $this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
  499. $this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
  500. $this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
  501. }
  502. /**
  503. * test formatting dates taking in account preferred i18n locale file
  504. *
  505. * @return void
  506. */
  507. public function testI18nFormat() {
  508. Configure::write('Config.language', 'es');
  509. $time = strtotime('Thu Jan 14 13:59:28 2010');
  510. $result = $this->Time->i18nFormat($time);
  511. $expected = '14/01/10';
  512. $this->assertEquals($expected, $result);
  513. $result = $this->Time->i18nFormat($time, '%c');
  514. $expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
  515. $this->assertEquals($expected, $result);
  516. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  517. $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
  518. $this->assertEquals($expected, $result);
  519. $time = strtotime('Wed Jan 13 13:59:28 2010');
  520. $result = $this->Time->i18nFormat($time);
  521. $expected = '13/01/10';
  522. $this->assertEquals($expected, $result);
  523. $result = $this->Time->i18nFormat($time, '%c');
  524. $expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
  525. $this->assertEquals($expected, $result);
  526. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  527. $expected = 'Time is 01:59:28 PM, and date is 13/01/10';
  528. $this->assertEquals($expected, $result);
  529. $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
  530. $expected = 'Date invalid';
  531. $this->assertEquals($expected, $result);
  532. }
  533. /**
  534. * test new format() syntax which inverts first and second parameters
  535. *
  536. * @return void
  537. */
  538. public function testFormatNewSyntax() {
  539. $time = time();
  540. $this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
  541. $this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
  542. }
  543. /**
  544. * testListTimezones
  545. *
  546. * @return void
  547. */
  548. public function testListTimezones() {
  549. $return = Time::listTimezones();
  550. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  551. $this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
  552. $this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
  553. $this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
  554. $this->assertTrue(isset($return['UTC']['UTC']));
  555. $this->assertFalse(isset($return['Cuba']));
  556. $this->assertFalse(isset($return['US']));
  557. $return = Time::listTimezones('#^Asia/#');
  558. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  559. $this->assertFalse(isset($return['Pacific']));
  560. $return = Time::listTimezones('#^(America|Pacific)/#', null, false);
  561. $this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
  562. $this->assertTrue(isset($return['Pacific/Tahiti']));
  563. $return = Time::listTimezones(\DateTimeZone::ASIA);
  564. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  565. $this->assertFalse(isset($return['Pacific']));
  566. $return = Time::listTimezones(\DateTimeZone::PER_COUNTRY, 'US', false);
  567. $this->assertTrue(isset($return['Pacific/Honolulu']));
  568. $this->assertFalse(isset($return['Asia/Bangkok']));
  569. }
  570. /**
  571. * Tests that using Cake\Utility\Time::format() with the correct sytax actually converts
  572. * from one timezone to the other correctly
  573. *
  574. * @return void
  575. */
  576. public function testCorrectTimezoneConversion() {
  577. date_default_timezone_set('UTC');
  578. $date = '2012-01-01 10:00:00';
  579. $converted = Time::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
  580. $expected = new \DateTime($date);
  581. $expected->setTimezone(new \DateTimeZone('Europe/Copenhagen'));
  582. $this->assertEquals($expected->format('Y-m-d H:i'), $converted);
  583. }
  584. }