TimeTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. * testIsThisWeek method
  341. *
  342. * @return void
  343. */
  344. public function testIsThisWeek() {
  345. $time = new Time('this sunday');
  346. $this->assertTrue($time->isThisWeek());
  347. $this->assertTrue($time->modify('-1 day')->isThisWeek());
  348. $this->assertFalse($time->modify('-6 days')->isThisWeek());
  349. $time = new Time();
  350. $time->year = $time->year - 1;
  351. $this->assertFalse($time->isThisWeek());
  352. }
  353. /**
  354. * testIsThisMonth method
  355. *
  356. * @return void
  357. */
  358. public function testIsThisMonth() {
  359. $time = new Time();
  360. $this->assertTrue($time->isThisMonth());
  361. $time->year = $time->year + 1;
  362. $this->assertFalse($time->isThisMonth());
  363. $time = new Time();
  364. $this->assertFalse($time->modify('next month')->isThisMonth());
  365. }
  366. /**
  367. * testIsThisYear method
  368. *
  369. * @return void
  370. */
  371. public function testIsThisYear() {
  372. $time = new Time();
  373. $this->assertTrue($time->isThisYear());
  374. $time->year = $time->year + 1;
  375. $this->assertFalse($time->isThisYear());
  376. $thisYear = date('Y');
  377. $time = new Time("$thisYear-01-01 00:00", 'Australia/Sydney');
  378. $now = clone $time;
  379. $now->timezone('UTC');
  380. Time::setTestNow($now);
  381. $this->assertFalse($time->isThisYear());
  382. }
  383. /**
  384. * testWasWithinLast method
  385. *
  386. * @return void
  387. */
  388. public function testWasWithinLast() {
  389. $this->assertTrue((new Time('-1 day'))->wasWithinLast('1 day'));
  390. $this->assertTrue((new Time('-1 week'))->wasWithinLast('1 week'));
  391. $this->assertTrue((new Time('-1 year'))->wasWithinLast('1 year'));
  392. $this->assertTrue((new Time('-1 second'))->wasWithinLast('1 second'));
  393. $this->assertTrue((new Time('-1 day'))->wasWithinLast('1 week'));
  394. $this->assertTrue((new Time('-1 week'))->wasWithinLast('2 week'));
  395. $this->assertTrue((new Time('-1 second'))->wasWithinLast('10 minutes'));
  396. $this->assertTrue((new Time('-1 month'))->wasWithinLast('13 month'));
  397. $this->assertTrue((new Time('-1 seconds'))->wasWithinLast('1 hour'));
  398. $this->assertFalse((new Time('-1 year'))->wasWithinLast('1 second'));
  399. $this->assertFalse((new Time('-1 year'))->wasWithinLast('0 year'));
  400. $this->assertFalse((new Time('-1 weeks'))->wasWithinLast('1 day'));
  401. $this->assertTrue((new Time('-3 days'))->wasWithinLast('5'));
  402. }
  403. /**
  404. * testWasWithinLast method
  405. *
  406. * @return void
  407. */
  408. public function testIsWithinNext() {
  409. $this->assertFalse((new Time('-1 day'))->isWithinNext('1 day'));
  410. $this->assertFalse((new Time('-1 week'))->isWithinNext('1 week'));
  411. $this->assertFalse((new Time('-1 year'))->isWithinNext('1 year'));
  412. $this->assertFalse((new Time('-1 second'))->isWithinNext('1 second'));
  413. $this->assertFalse((new Time('-1 day'))->isWithinNext('1 week'));
  414. $this->assertFalse((new Time('-1 week'))->isWithinNext('2 week'));
  415. $this->assertFalse((new Time('-1 second'))->isWithinNext('10 minutes'));
  416. $this->assertFalse((new Time('-1 month'))->isWithinNext('13 month'));
  417. $this->assertFalse((new Time('-1 seconds'))->isWithinNext('1 hour'));
  418. $this->assertTrue((new Time('+1 day'))->isWithinNext('1 day'));
  419. $this->assertTrue((new Time('+1 week'))->isWithinNext('7 day'));
  420. $this->assertTrue((new Time('+1 second'))->isWithinNext('1 minute'));
  421. $this->assertTrue((new Time('+1 month'))->isWithinNext('1 month'));
  422. }
  423. /**
  424. * test formatting dates taking in account preferred i18n locale file
  425. *
  426. * @return void
  427. */
  428. public function testI18nFormat() {
  429. $time = new Time('Thu Jan 14 13:59:28 2010');
  430. $result = $time->i18nFormat();
  431. $expected = '1/14/10, 1:59 PM';
  432. $this->assertEquals($expected, $result);
  433. $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
  434. $expected = 'jueves, 14 de enero de 2010, 13:59:28 (GMT)';
  435. $this->assertEquals($expected, $result);
  436. $format = [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT];
  437. $result = $time->i18nFormat($format);
  438. $expected = '1:59 PM';
  439. $this->assertEquals($expected, $result);
  440. $result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
  441. $expected = '00:59:28';
  442. $this->assertEquals($expected, $result);
  443. }
  444. /**
  445. * testListTimezones
  446. *
  447. * @return void
  448. */
  449. public function testListTimezones() {
  450. $return = Time::listTimezones();
  451. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  452. $this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
  453. $this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
  454. $this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
  455. $this->assertTrue(isset($return['UTC']['UTC']));
  456. $this->assertFalse(isset($return['Cuba']));
  457. $this->assertFalse(isset($return['US']));
  458. $return = Time::listTimezones('#^Asia/#');
  459. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  460. $this->assertFalse(isset($return['Pacific']));
  461. $return = Time::listTimezones('#^(America|Pacific)/#', null, false);
  462. $this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
  463. $this->assertTrue(isset($return['Pacific/Tahiti']));
  464. $return = Time::listTimezones(\DateTimeZone::ASIA);
  465. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  466. $this->assertFalse(isset($return['Pacific']));
  467. $return = Time::listTimezones(\DateTimeZone::PER_COUNTRY, 'US', false);
  468. $this->assertTrue(isset($return['Pacific/Honolulu']));
  469. $this->assertFalse(isset($return['Asia/Bangkok']));
  470. }
  471. }