TimeTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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->Time = new Time();
  41. $this->_systemTimezoneIdentifier = date_default_timezone_get();
  42. Configure::write('Config.language', 'eng');
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @return void
  48. */
  49. public function tearDown() {
  50. parent::tearDown();
  51. unset($this->Time);
  52. $this->_restoreSystemTimezone();
  53. }
  54. /**
  55. * Restored the original system timezone
  56. *
  57. * @return void
  58. */
  59. protected function _restoreSystemTimezone() {
  60. date_default_timezone_set($this->_systemTimezoneIdentifier);
  61. }
  62. /**
  63. * Provides values and expectations for the toQuarter method
  64. *
  65. * @return array
  66. */
  67. public function toQuarterProvider() {
  68. return [
  69. ['2007-12-25', 4],
  70. ['2007-9-25', 3],
  71. ['2007-3-25', 1],
  72. ['2007-3-25', ['2007-01-01', '2007-03-31'], true],
  73. ['2007-5-25', ['2007-04-01', '2007-06-30'], true],
  74. ['2007-8-25', ['2007-07-01', '2007-09-30'], true],
  75. ['2007-12-25', ['2007-10-01', '2007-12-31'], true],
  76. ];
  77. }
  78. /**
  79. * testToQuarter method
  80. *
  81. * @dataProvider toQuarterProvider
  82. * @return void
  83. */
  84. public function testToQuarter($date, $expected, $range = false) {
  85. $this->assertEquals($expected, (new Time($date))->toQuarter($range));
  86. }
  87. /**
  88. * provider for timeAgoInWords() tests
  89. *
  90. * @return array
  91. */
  92. public static function timeAgoProvider() {
  93. return array(
  94. array('-12 seconds', '12 seconds ago'),
  95. array('-12 minutes', '12 minutes ago'),
  96. array('-2 hours', '2 hours ago'),
  97. array('-1 day', '1 day ago'),
  98. array('-2 days', '2 days ago'),
  99. array('-2 days -3 hours', '2 days, 3 hours ago'),
  100. array('-1 week', '1 week ago'),
  101. array('-2 weeks -2 days', '2 weeks, 2 days ago'),
  102. array('+1 week', '1 week'),
  103. array('+1 week 1 day', '1 week, 1 day'),
  104. array('+2 weeks 2 day', '2 weeks, 2 days'),
  105. array('2007-9-24', 'on 24/9/07'),
  106. array('now', 'just now'),
  107. );
  108. }
  109. /**
  110. * testTimeAgoInWords method
  111. *
  112. * @dataProvider timeAgoProvider
  113. * @return void
  114. */
  115. public function testTimeAgoInWords($input, $expected) {
  116. $time = new Time($input);
  117. $result = $time->timeAgoInWords();
  118. $this->assertEquals($expected, $result);
  119. }
  120. /**
  121. * provider for timeAgo with an end date.
  122. *
  123. * @return void
  124. */
  125. public function timeAgoEndProvider() {
  126. return array(
  127. array(
  128. '+4 months +2 weeks +3 days',
  129. '4 months, 2 weeks, 3 days',
  130. '8 years'
  131. ),
  132. array(
  133. '+4 months +2 weeks +1 day',
  134. '4 months, 2 weeks, 1 day',
  135. '8 years'
  136. ),
  137. array(
  138. '+3 months +2 weeks',
  139. '3 months, 2 weeks',
  140. '8 years'
  141. ),
  142. array(
  143. '+3 months +2 weeks +1 day',
  144. '3 months, 2 weeks, 1 day',
  145. '8 years'
  146. ),
  147. array(
  148. '+1 months +1 week +1 day',
  149. '1 month, 1 week, 1 day',
  150. '8 years'
  151. ),
  152. array(
  153. '+2 months +2 days',
  154. '2 months, 2 days',
  155. '+2 months +2 days'
  156. ),
  157. array(
  158. '+2 months +12 days',
  159. '2 months, 1 week, 5 days',
  160. '3 months'
  161. ),
  162. );
  163. }
  164. /**
  165. * test the end option for timeAgoInWords
  166. *
  167. * @dataProvider timeAgoEndProvider
  168. * @return void
  169. */
  170. public function testTimeAgoInWordsEnd($input, $expected, $end) {
  171. $time = new Time($input);
  172. $result = $time->timeAgoInWords(array('end' => $end));
  173. $this->assertEquals($expected, $result);
  174. }
  175. /**
  176. * test the custom string options for timeAgoInWords
  177. *
  178. * @return void
  179. */
  180. public function testTimeAgoInWordsCustomStrings() {
  181. $time = new Time('-8 years -4 months -2 weeks -3 days');
  182. $result = $time->timeAgoInWords(array(
  183. 'relativeString' => 'at least %s ago',
  184. 'accuracy' => array('year' => 'year'),
  185. 'end' => '+10 years'
  186. ));
  187. $expected = 'at least 8 years ago';
  188. $this->assertEquals($expected, $result);
  189. $time = new Time('+4 months +2 weeks +3 days');
  190. $result = $time->timeAgoInWords(array(
  191. 'absoluteString' => 'exactly on %s',
  192. 'accuracy' => array('year' => 'year'),
  193. 'end' => '+2 months'
  194. ));
  195. $expected = 'exactly on ' . date('j/n/y', strtotime('+4 months +2 weeks +3 days'));
  196. $this->assertEquals($expected, $result);
  197. }
  198. /**
  199. * Test the accuracy option for timeAgoInWords()
  200. *
  201. * @return void
  202. */
  203. public function testTimeAgoInWordsAccuracy() {
  204. $time = new Time('+8 years +4 months +2 weeks +3 days');
  205. $result = $time->timeAgoInWords(array(
  206. 'accuracy' => array('year' => 'year'),
  207. 'end' => '+10 years'
  208. ));
  209. $expected = '8 years';
  210. $this->assertEquals($expected, $result);
  211. $time = new Time('+8 years +4 months +2 weeks +3 days');
  212. $result = $time->timeAgoInWords(array(
  213. 'accuracy' => array('year' => 'month'),
  214. 'end' => '+10 years'
  215. ));
  216. $expected = '8 years, 4 months';
  217. $this->assertEquals($expected, $result);
  218. $time = new Time('+8 years +4 months +2 weeks +3 days');
  219. $result = $time->timeAgoInWords(array(
  220. 'accuracy' => array('year' => 'week'),
  221. 'end' => '+10 years'
  222. ));
  223. $expected = '8 years, 4 months, 2 weeks';
  224. $this->assertEquals($expected, $result);
  225. $time = new Time('+8 years +4 months +2 weeks +3 days');
  226. $result = $time->timeAgoInWords(array(
  227. 'accuracy' => array('year' => 'day'),
  228. 'end' => '+10 years'
  229. ));
  230. $expected = '8 years, 4 months, 2 weeks, 3 days';
  231. $this->assertEquals($expected, $result);
  232. $time = new Time('+1 years +5 weeks');
  233. $result = $time->timeAgoInWords(array(
  234. 'accuracy' => array('year' => 'year'),
  235. 'end' => '+10 years'
  236. ));
  237. $expected = '1 year';
  238. $this->assertEquals($expected, $result);
  239. $time = new Time('+58 minutes');
  240. $result = $time->timeAgoInWords(array(
  241. 'accuracy' => 'hour'
  242. ));
  243. $expected = 'in about an hour';
  244. $this->assertEquals($expected, $result);
  245. $time = new Time('+23 hours');
  246. $result = $time->timeAgoInWords(array(
  247. 'accuracy' => 'day'
  248. ));
  249. $expected = 'in about a day';
  250. $this->assertEquals($expected, $result);
  251. }
  252. /**
  253. * Test the format option of timeAgoInWords()
  254. *
  255. * @return void
  256. */
  257. public function testTimeAgoInWordsWithFormat() {
  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('2007-9-25');
  262. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  263. $this->assertEquals('on 2007-09-25', $result);
  264. $time = new Time('+2 weeks +2 days');
  265. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  266. $this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
  267. $time = new Time('+2 months +2 days');
  268. $result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'Y-m-d'));
  269. $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
  270. }
  271. /**
  272. * test timeAgoInWords() with negative values.
  273. *
  274. * @return void
  275. */
  276. public function testTimeAgoInWordsNegativeValues() {
  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' => '3 month'));
  282. $this->assertEquals('2 months, 2 days ago', $result);
  283. $time = new Time('-2 months -2 days');
  284. $result = $time->timeAgoInWords(array('end' => '1 month', 'format' => 'Y-m-d'));
  285. $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
  286. $time = new Time('-2 years -5 months -2 days');
  287. $result = $time->timeAgoInWords(array('end' => '3 years'));
  288. $this->assertEquals('2 years, 5 months, 2 days ago', $result);
  289. $time = new Time('-2 weeks -2 days');
  290. $result = $time->timeAgoInWords(array('format' => 'Y-m-d'));
  291. $this->assertEquals('2 weeks, 2 days ago', $result);
  292. $time = new Time('-3 years -12 months');
  293. $result = $time->timeAgoInWords();
  294. $expected = 'on ' . $time->format('j/n/y');
  295. $this->assertEquals($expected, $result);
  296. $time = new Time('-1 month -1 week -6 days');
  297. $result = $time->timeAgoInWords(
  298. array('end' => '1 year', 'accuracy' => array('month' => 'month'))
  299. );
  300. $this->assertEquals('1 month ago', $result);
  301. $time = new Time('-1 years -2 weeks -3 days');
  302. $result = $time->timeAgoInWords(
  303. array('accuracy' => array('year' => 'year'))
  304. );
  305. $expected = 'on ' . $time->format('j/n/y');
  306. $this->assertEquals($expected, $result);
  307. $time = new Time('-13 months -5 days');
  308. $result = $time->timeAgoInWords(array('end' => '2 years'));
  309. $this->assertEquals('1 year, 1 month, 5 days ago', $result);
  310. $time = new Time('-58 minutes');
  311. $result = $time->timeAgoInWords(array('accuracy' => 'hour'));
  312. $this->assertEquals('about an hour ago', $result);
  313. $time = new Time('-23 hours');
  314. $result = $time->timeAgoInWords(array('accuracy' => 'day'));
  315. $this->assertEquals('about a day ago', $result);
  316. }
  317. /**
  318. * testNice method
  319. *
  320. * @return void
  321. */
  322. public function testNice() {
  323. $time = new Time('2014-04-20 20:00', 'UTC');
  324. $this->assertEquals('Apr 20, 2014, 8:00 PM', $time->nice());
  325. $result = $time->nice('America/New_York');
  326. $this->assertEquals('Apr 20, 2014, 4:00 PM', $result);
  327. $this->assertEquals('UTC', $time->getTimezone()->getName());
  328. }
  329. /**
  330. * testToUnix method
  331. *
  332. * @return void
  333. */
  334. public function testToUnix() {
  335. $this->assertEquals(time(), $this->Time->toUnix(time()));
  336. $this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  337. $this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  338. $this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  339. $this->assertEquals(false, $this->Time->toUnix(''));
  340. $this->assertEquals(false, $this->Time->toUnix(null));
  341. }
  342. /**
  343. * testToAtom method
  344. *
  345. * @return void
  346. */
  347. public function testToAtom() {
  348. $dateTime = new \DateTime;
  349. $this->assertEquals($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  350. }
  351. /**
  352. * testToRss method
  353. *
  354. * @return void
  355. */
  356. public function testToRss() {
  357. $date = '2012-08-12 12:12:45';
  358. $time = strtotime($date);
  359. $this->assertEquals(date('r', $time), $this->Time->toRss($time));
  360. $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
  361. foreach ($timezones as $timezone) {
  362. $yourTimezone = new \DateTimeZone($timezone);
  363. $yourTime = new \DateTime($date, $yourTimezone);
  364. $userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
  365. $time = $yourTime->format('U');
  366. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone");
  367. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  368. }
  369. }
  370. /**
  371. * testFormat method
  372. *
  373. * @return void
  374. */
  375. public function testFormat() {
  376. $format = 'D-M-Y';
  377. $tz = date_default_timezone_get();
  378. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  379. foreach ($arr as $val) {
  380. $this->assertEquals(date($format, $val), $this->Time->format($format, $val));
  381. $this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
  382. }
  383. $result = $this->Time->format('Y-m-d', null, 'never');
  384. $this->assertEquals('never', $result);
  385. $result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
  386. $this->assertEquals('13-01-2012', $result);
  387. $result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
  388. $this->assertEquals('invalid', $result);
  389. $result = $this->Time->format('0000-00-00', '%d-%m-%Y', 'invalid');
  390. $this->assertEquals('invalid', $result);
  391. }
  392. /**
  393. * testIsToday method
  394. *
  395. * @return void
  396. */
  397. public function testIsToday() {
  398. $result = $this->Time->isToday('+1 day');
  399. $this->assertFalse($result);
  400. $result = $this->Time->isToday('+1 days');
  401. $this->assertFalse($result);
  402. $result = $this->Time->isToday('+0 day');
  403. $this->assertTrue($result);
  404. $result = $this->Time->isToday('-1 day');
  405. $this->assertFalse($result);
  406. }
  407. /**
  408. * testIsFuture method
  409. *
  410. * @return void
  411. */
  412. public function testIsFuture() {
  413. $this->assertTrue($this->Time->isFuture('+1 month'));
  414. $this->assertTrue($this->Time->isFuture('+1 days'));
  415. $this->assertTrue($this->Time->isFuture('+1 minute'));
  416. $this->assertTrue($this->Time->isFuture('+1 second'));
  417. $this->assertFalse($this->Time->isFuture('-1 second'));
  418. $this->assertFalse($this->Time->isFuture('-1 day'));
  419. $this->assertFalse($this->Time->isFuture('-1 week'));
  420. $this->assertFalse($this->Time->isFuture('-1 month'));
  421. }
  422. /**
  423. * testIsPast method
  424. *
  425. * @return void
  426. */
  427. public function testIsPast() {
  428. $this->assertFalse($this->Time->isPast('+1 month'));
  429. $this->assertFalse($this->Time->isPast('+1 days'));
  430. $this->assertFalse($this->Time->isPast('+1 minute'));
  431. $this->assertFalse($this->Time->isPast('+1 second'));
  432. $this->assertTrue($this->Time->isPast('-1 second'));
  433. $this->assertTrue($this->Time->isPast('-1 day'));
  434. $this->assertTrue($this->Time->isPast('-1 week'));
  435. $this->assertTrue($this->Time->isPast('-1 month'));
  436. }
  437. /**
  438. * testIsThisWeek method
  439. *
  440. * @return void
  441. */
  442. public function testIsThisWeek() {
  443. // A map of days which goes from -1 day of week to +1 day of week
  444. $map = array(
  445. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  446. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  447. 'Sun' => array(-7, 1)
  448. );
  449. $days = $map[date('D')];
  450. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  451. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  452. }
  453. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  454. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  455. }
  456. /**
  457. * testIsThisMonth method
  458. *
  459. * @return void
  460. */
  461. public function testIsThisMonth() {
  462. $result = $this->Time->isThisMonth('+0 day');
  463. $this->assertTrue($result);
  464. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  465. $this->assertTrue($result);
  466. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  467. $this->assertFalse($result);
  468. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  469. $this->assertFalse($result);
  470. }
  471. /**
  472. * testIsThisYear method
  473. *
  474. * @return void
  475. */
  476. public function testIsThisYear() {
  477. $result = $this->Time->isThisYear('+0 day');
  478. $this->assertTrue($result);
  479. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  480. $this->assertTrue($result);
  481. }
  482. /**
  483. * testWasYesterday method
  484. *
  485. * @return void
  486. */
  487. public function testWasYesterday() {
  488. $result = $this->Time->wasYesterday('+1 day');
  489. $this->assertFalse($result);
  490. $result = $this->Time->wasYesterday('+1 days');
  491. $this->assertFalse($result);
  492. $result = $this->Time->wasYesterday('+0 day');
  493. $this->assertFalse($result);
  494. $result = $this->Time->wasYesterday('-1 day');
  495. $this->assertTrue($result);
  496. $result = $this->Time->wasYesterday('-1 days');
  497. $this->assertTrue($result);
  498. $result = $this->Time->wasYesterday('-2 days');
  499. $this->assertFalse($result);
  500. }
  501. /**
  502. * testIsTomorrow method
  503. *
  504. * @return void
  505. */
  506. public function testIsTomorrow() {
  507. $result = $this->Time->isTomorrow('+1 day');
  508. $this->assertTrue($result);
  509. $result = $this->Time->isTomorrow('+1 days');
  510. $this->assertTrue($result);
  511. $result = $this->Time->isTomorrow('+0 day');
  512. $this->assertFalse($result);
  513. $result = $this->Time->isTomorrow('-1 day');
  514. $this->assertFalse($result);
  515. }
  516. /**
  517. * testWasWithinLast method
  518. *
  519. * @return void
  520. */
  521. public function testWasWithinLast() {
  522. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  523. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  524. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  525. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  526. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  527. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  528. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  529. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  530. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  531. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  532. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  533. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  534. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  535. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  536. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  537. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  538. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  539. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  540. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  541. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  542. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  543. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  544. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  545. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  546. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  547. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  548. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  549. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  550. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  551. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  552. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  553. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  554. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  555. }
  556. /**
  557. * testWasWithinLast method
  558. *
  559. * @return void
  560. */
  561. public function testIsWithinNext() {
  562. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  563. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  564. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  565. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  566. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  567. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  568. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  569. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  570. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  571. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  572. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  573. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  574. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  575. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  576. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  577. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  578. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  579. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  580. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  581. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  582. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  583. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  584. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  585. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  586. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  587. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  588. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  589. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  590. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  591. $this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
  592. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
  593. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
  594. $this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
  595. $this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
  596. $this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
  597. }
  598. /**
  599. * test formatting dates taking in account preferred i18n locale file
  600. *
  601. * @return void
  602. */
  603. public function testI18nFormat() {
  604. Configure::write('Config.language', 'es');
  605. $time = strtotime('Thu Jan 14 13:59:28 2010');
  606. $result = $this->Time->i18nFormat($time);
  607. $expected = '14/01/10';
  608. $this->assertEquals($expected, $result);
  609. $result = $this->Time->i18nFormat($time, '%c');
  610. $expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
  611. $this->assertEquals($expected, $result);
  612. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  613. $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
  614. $this->assertEquals($expected, $result);
  615. $time = strtotime('Wed Jan 13 13:59:28 2010');
  616. $result = $this->Time->i18nFormat($time);
  617. $expected = '13/01/10';
  618. $this->assertEquals($expected, $result);
  619. $result = $this->Time->i18nFormat($time, '%c');
  620. $expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
  621. $this->assertEquals($expected, $result);
  622. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  623. $expected = 'Time is 01:59:28 PM, and date is 13/01/10';
  624. $this->assertEquals($expected, $result);
  625. $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
  626. $expected = 'Date invalid';
  627. $this->assertEquals($expected, $result);
  628. }
  629. /**
  630. * test new format() syntax which inverts first and second parameters
  631. *
  632. * @return void
  633. */
  634. public function testFormatNewSyntax() {
  635. $time = time();
  636. $this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
  637. $this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
  638. }
  639. /**
  640. * testListTimezones
  641. *
  642. * @return void
  643. */
  644. public function testListTimezones() {
  645. $return = Time::listTimezones();
  646. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  647. $this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
  648. $this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
  649. $this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
  650. $this->assertTrue(isset($return['UTC']['UTC']));
  651. $this->assertFalse(isset($return['Cuba']));
  652. $this->assertFalse(isset($return['US']));
  653. $return = Time::listTimezones('#^Asia/#');
  654. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  655. $this->assertFalse(isset($return['Pacific']));
  656. $return = Time::listTimezones('#^(America|Pacific)/#', null, false);
  657. $this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
  658. $this->assertTrue(isset($return['Pacific/Tahiti']));
  659. $return = Time::listTimezones(\DateTimeZone::ASIA);
  660. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  661. $this->assertFalse(isset($return['Pacific']));
  662. $return = Time::listTimezones(\DateTimeZone::PER_COUNTRY, 'US', false);
  663. $this->assertTrue(isset($return['Pacific/Honolulu']));
  664. $this->assertFalse(isset($return['Asia/Bangkok']));
  665. }
  666. /**
  667. * Tests that using Cake\Utility\Time::format() with the correct sytax actually converts
  668. * from one timezone to the other correctly
  669. *
  670. * @return void
  671. */
  672. public function testCorrectTimezoneConversion() {
  673. date_default_timezone_set('UTC');
  674. $date = '2012-01-01 10:00:00';
  675. $converted = Time::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
  676. $expected = new \DateTime($date);
  677. $expected->setTimezone(new \DateTimeZone('Europe/Copenhagen'));
  678. $this->assertEquals($expected->format('Y-m-d H:i'), $converted);
  679. }
  680. }