TimeTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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 CakePHP(tm) v 1.2.0.4206
  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\Core\Object;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\Utility\Time;
  23. /**
  24. * TimeTest class
  25. *
  26. */
  27. class TimeTest extends TestCase {
  28. /**
  29. * Default system timezone identifier
  30. *
  31. * @var string
  32. */
  33. protected $_systemTimezoneIdentifier = null;
  34. /**
  35. * setUp method
  36. *
  37. * @return void
  38. */
  39. public function setUp() {
  40. parent::setUp();
  41. $this->Time = new Time();
  42. $this->_systemTimezoneIdentifier = date_default_timezone_get();
  43. Configure::write('Config.language', 'eng');
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown() {
  51. parent::tearDown();
  52. unset($this->Time);
  53. $this->_restoreSystemTimezone();
  54. }
  55. /**
  56. * Restored the original system timezone
  57. *
  58. * @param string $timezoneIdentifier Timezone string
  59. * @return void
  60. */
  61. protected function _restoreSystemTimezone() {
  62. date_default_timezone_set($this->_systemTimezoneIdentifier);
  63. }
  64. /**
  65. * testToQuarter method
  66. *
  67. * @return void
  68. */
  69. public function testToQuarter() {
  70. $result = $this->Time->toQuarter('2007-12-25');
  71. $this->assertEquals(4, $result);
  72. $result = $this->Time->toQuarter('2007-9-25');
  73. $this->assertEquals(3, $result);
  74. $result = $this->Time->toQuarter('2007-3-25');
  75. $this->assertEquals(1, $result);
  76. $result = $this->Time->toQuarter('2007-3-25', true);
  77. $this->assertEquals(array('2007-01-01', '2007-03-31'), $result);
  78. $result = $this->Time->toQuarter('2007-5-25', true);
  79. $this->assertEquals(array('2007-04-01', '2007-06-30'), $result);
  80. $result = $this->Time->toQuarter('2007-8-25', true);
  81. $this->assertEquals(array('2007-07-01', '2007-09-30'), $result);
  82. $result = $this->Time->toQuarter('2007-12-25', true);
  83. $this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
  84. }
  85. /**
  86. * provider for timeAgoInWords() tests
  87. *
  88. * @return array
  89. */
  90. public static function timeAgoProvider() {
  91. return array(
  92. array('-12 seconds', '12 seconds ago'),
  93. array('-12 minutes', '12 minutes ago'),
  94. array('-2 hours', '2 hours ago'),
  95. array('-1 day', '1 day ago'),
  96. array('-2 days', '2 days ago'),
  97. array('-2 days -3 hours', '2 days, 3 hours ago'),
  98. array('-1 week', '1 week ago'),
  99. array('-2 weeks -2 days', '2 weeks, 2 days ago'),
  100. array('+1 week', '1 week'),
  101. array('+1 week 1 day', '1 week, 1 day'),
  102. array('+2 weeks 2 day', '2 weeks, 2 days'),
  103. array('2007-9-24', 'on 24/9/07'),
  104. array('now', 'just now'),
  105. );
  106. }
  107. /**
  108. * testTimeAgoInWords method
  109. *
  110. * @dataProvider timeAgoProvider
  111. * @return void
  112. */
  113. public function testTimeAgoInWords($input, $expected) {
  114. $result = $this->Time->timeAgoInWords($input);
  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. 'on ' . date('j/n/y', strtotime('+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. $result = $this->Time->timeAgoInWords(
  169. $input, array('end' => $end)
  170. );
  171. $this->assertEquals($expected, $result);
  172. }
  173. /**
  174. * test the custom string options for timeAgoInWords
  175. *
  176. * @return void
  177. */
  178. public function testTimeAgoInWordsCustomStrings() {
  179. $result = $this->Time->timeAgoInWords(
  180. strtotime('-8 years -4 months -2 weeks -3 days'),
  181. array('relativeString' => 'at least %s ago', 'accuracy' => array('year' => 'year'), 'end' => '+10 years')
  182. );
  183. $expected = 'at least 8 years ago';
  184. $this->assertEquals($expected, $result);
  185. $result = $this->Time->timeAgoInWords(
  186. strtotime('+4 months +2 weeks +3 days'),
  187. array('absoluteString' => 'exactly on %s', 'accuracy' => array('year' => 'year'), 'end' => '+2 months')
  188. );
  189. $expected = 'exactly on ' . date('j/n/y', strtotime('+4 months +2 weeks +3 days'));
  190. $this->assertEquals($expected, $result);
  191. }
  192. /**
  193. * Test the accuracy option for timeAgoInWords()
  194. *
  195. * @return void
  196. */
  197. public function testTimeAgoInWordsAccuracy() {
  198. $result = $this->Time->timeAgoInWords(
  199. strtotime('+8 years +4 months +2 weeks +3 days'),
  200. array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
  201. );
  202. $expected = '8 years';
  203. $this->assertEquals($expected, $result);
  204. $result = $this->Time->timeAgoInWords(
  205. strtotime('+8 years +4 months +2 weeks +3 days'),
  206. array('accuracy' => array('year' => 'month'), 'end' => '+10 years')
  207. );
  208. $expected = '8 years, 4 months';
  209. $this->assertEquals($expected, $result);
  210. $result = $this->Time->timeAgoInWords(
  211. strtotime('+8 years +4 months +2 weeks +3 days'),
  212. array('accuracy' => array('year' => 'week'), 'end' => '+10 years')
  213. );
  214. $expected = '8 years, 4 months, 2 weeks';
  215. $this->assertEquals($expected, $result);
  216. $result = $this->Time->timeAgoInWords(
  217. strtotime('+8 years +4 months +2 weeks +3 days'),
  218. array('accuracy' => array('year' => 'day'), 'end' => '+10 years')
  219. );
  220. $expected = '8 years, 4 months, 2 weeks, 3 days';
  221. $this->assertEquals($expected, $result);
  222. $result = $this->Time->timeAgoInWords(
  223. strtotime('+1 years +5 weeks'),
  224. array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
  225. );
  226. $expected = '1 year';
  227. $this->assertEquals($expected, $result);
  228. $result = $this->Time->timeAgoInWords(
  229. strtotime('+58 minutes'),
  230. array('accuracy' => 'hour')
  231. );
  232. $expected = 'in about an hour';
  233. $this->assertEquals($expected, $result);
  234. $result = $this->Time->timeAgoInWords(
  235. strtotime('+23 hours'),
  236. array('accuracy' => 'day')
  237. );
  238. $expected = 'in about a day';
  239. $this->assertEquals($expected, $result);
  240. }
  241. /**
  242. * Test the format option of timeAgoInWords()
  243. *
  244. * @return void
  245. */
  246. public function testTimeAgoInWordsWithFormat() {
  247. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  248. $this->assertEquals('on 2007-09-25', $result);
  249. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  250. $this->assertEquals('on 2007-09-25', $result);
  251. $result = $this->Time->timeAgoInWords(
  252. strtotime('+2 weeks +2 days'),
  253. 'Y-m-d'
  254. );
  255. $this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
  256. $result = $this->Time->timeAgoInWords(
  257. strtotime('+2 months +2 days'),
  258. array('end' => '1 month', 'format' => 'Y-m-d')
  259. );
  260. $this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
  261. }
  262. /**
  263. * test timeAgoInWords() with negative values.
  264. *
  265. * @return void
  266. */
  267. public function testTimeAgoInWordsNegativeValues() {
  268. $result = $this->Time->timeAgoInWords(
  269. strtotime('-2 months -2 days'),
  270. array('end' => '3 month')
  271. );
  272. $this->assertEquals('2 months, 2 days ago', $result);
  273. $result = $this->Time->timeAgoInWords(
  274. strtotime('-2 months -2 days'),
  275. array('end' => '3 month')
  276. );
  277. $this->assertEquals('2 months, 2 days ago', $result);
  278. $result = $this->Time->timeAgoInWords(
  279. strtotime('-2 months -2 days'),
  280. array('end' => '1 month', 'format' => 'Y-m-d')
  281. );
  282. $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
  283. $result = $this->Time->timeAgoInWords(
  284. strtotime('-2 years -5 months -2 days'),
  285. array('end' => '3 years')
  286. );
  287. $this->assertEquals('2 years, 5 months, 2 days ago', $result);
  288. $result = $this->Time->timeAgoInWords(
  289. strtotime('-2 weeks -2 days'),
  290. 'Y-m-d'
  291. );
  292. $this->assertEquals('2 weeks, 2 days ago', $result);
  293. $time = strtotime('-3 years -12 months');
  294. $result = $this->Time->timeAgoInWords($time);
  295. $expected = 'on ' . date('j/n/y', $time);
  296. $this->assertEquals($expected, $result);
  297. $result = $this->Time->timeAgoInWords(
  298. strtotime('-1 month -1 week -6 days'),
  299. array('end' => '1 year', 'accuracy' => array('month' => 'month'))
  300. );
  301. $this->assertEquals('1 month ago', $result);
  302. $timestamp = strtotime('-1 years -2 weeks -3 days');
  303. $result = $this->Time->timeAgoInWords(
  304. $timestamp,
  305. array('accuracy' => array('year' => 'year'))
  306. );
  307. $expected = 'on ' . date('j/n/y', $timestamp);
  308. $this->assertEquals($expected, $result);
  309. $result = $this->Time->timeAgoInWords(
  310. strtotime('-13 months -5 days'),
  311. array('end' => '2 years')
  312. );
  313. $this->assertEquals('1 year, 1 month, 5 days ago', $result);
  314. $result = $this->Time->timeAgoInWords(
  315. strtotime('-58 minutes'),
  316. array('accuracy' => 'hour')
  317. );
  318. $this->assertEquals('about an hour ago', $result);
  319. $result = $this->Time->timeAgoInWords(
  320. strtotime('-23 hours'),
  321. array('accuracy' => 'day')
  322. );
  323. $this->assertEquals('about a day ago', $result);
  324. }
  325. /**
  326. * testNice method
  327. *
  328. * @return void
  329. */
  330. public function testNice() {
  331. $time = time() + 2 * DAY;
  332. $this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  333. $time = time() - 2 * DAY;
  334. $this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  335. $time = time();
  336. $this->assertEquals(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  337. $time = 0;
  338. $this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  339. $time = null;
  340. $this->assertEquals(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  341. $time = time();
  342. $this->assertEquals(date('D', $time), $this->Time->nice($time, null, '%a'));
  343. $this->assertEquals(date('M d, Y', $time), $this->Time->nice($time, null, '%b %d, %Y'));
  344. Time::$niceFormat = '%Y-%d-%m';
  345. $this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
  346. $this->assertEquals('%Y-%d-%m', Time::$niceFormat);
  347. Time::$niceFormat = '%Y-%d-%m %H:%M';
  348. $this->assertEquals(date('Y-d-m H:i', $time), $this->Time->nice($time));
  349. $this->assertEquals('%Y-%d-%m %H:%M', Time::$niceFormat);
  350. date_default_timezone_set('UTC');
  351. $result = $this->Time->nice(null, 'America/New_York');
  352. $expected = $this->Time->nice(time(), 'America/New_York');
  353. $this->assertEquals(substr($expected, 0, -1), substr($result, 0, -1));
  354. $this->_restoreSystemTimezone();
  355. }
  356. /**
  357. * testDaysAsSql method
  358. *
  359. * @return void
  360. */
  361. public function testDaysAsSql() {
  362. $begin = time();
  363. $end = time() + DAY;
  364. $field = 'my_field';
  365. $expected = '(my_field >= \'' . date('Y-m-d', $begin) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $end) . ' 23:59:59\')';
  366. $this->assertEquals($expected, $this->Time->daysAsSql($begin, $end, $field));
  367. }
  368. /**
  369. * testDayAsSql method
  370. *
  371. * @return void
  372. */
  373. public function testDayAsSql() {
  374. $time = time();
  375. $field = 'my_field';
  376. $expected = '(my_field >= \'' . date('Y-m-d', $time) . ' 00:00:00\') AND (my_field <= \'' . date('Y-m-d', $time) . ' 23:59:59\')';
  377. $this->assertEquals($expected, $this->Time->dayAsSql($time, $field));
  378. }
  379. /**
  380. * testToUnix method
  381. *
  382. * @return void
  383. */
  384. public function testToUnix() {
  385. $this->assertEquals(time(), $this->Time->toUnix(time()));
  386. $this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  387. $this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  388. $this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  389. $this->assertEquals(false, $this->Time->toUnix(''));
  390. $this->assertEquals(false, $this->Time->toUnix(null));
  391. }
  392. /**
  393. * testToServer method
  394. *
  395. * @return void
  396. */
  397. public function testToServer() {
  398. date_default_timezone_set('Europe/Paris');
  399. $time = time();
  400. $this->assertEquals(date('Y-m-d H:i:s', $time), $this->Time->toServer($time));
  401. date_default_timezone_set('America/New_York');
  402. $time = time();
  403. date_default_timezone_set('Europe/Paris');
  404. $result = $this->Time->toServer($time, 'America/New_York');
  405. $this->assertEquals(date('Y-m-d H:i:s', $time), $result);
  406. date_default_timezone_set('Europe/Paris');
  407. $time = '2005-10-25 10:00:00';
  408. $result = $this->Time->toServer($time);
  409. $date = new \DateTime($time, new \DateTimeZone('UTC'));
  410. $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  411. $expected = $date->format('Y-m-d H:i:s');
  412. $this->assertEquals($expected, $result);
  413. $time = '2002-01-01 05:15:30';
  414. $result = $this->Time->toServer($time, 'America/New_York');
  415. $date = new \DateTime($time, new \DateTimeZone('America/New_York'));
  416. $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  417. $expected = $date->format('Y-m-d H:i:s');
  418. $this->assertEquals($expected, $result);
  419. $time = '2010-01-28T15:00:00+10:00';
  420. $result = $this->Time->toServer($time, 'America/New_York');
  421. $date = new \DateTime($time);
  422. $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  423. $expected = $date->format('Y-m-d H:i:s');
  424. $this->assertEquals($expected, $result);
  425. $date = new \DateTime(null, new \DateTimeZone('America/New_York'));
  426. $result = $this->Time->toServer($date, 'Pacific/Tahiti');
  427. $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  428. $expected = $date->format('Y-m-d H:i:s');
  429. $this->assertEquals($expected, $result);
  430. $this->_restoreSystemTimezone();
  431. $time = time();
  432. $result = $this->Time->toServer($time, null, 'l jS \of F Y h:i:s A');
  433. $expected = date('l jS \of F Y h:i:s A', $time);
  434. $this->assertEquals($expected, $result);
  435. $this->assertFalse($this->Time->toServer(time(), new Object()));
  436. date_default_timezone_set('UTC');
  437. $serverTime = new \DateTime('2012-12-11 14:15:20');
  438. $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
  439. foreach ($timezones as $timezone) {
  440. $result = $this->Time->toServer($serverTime->format('Y-m-d H:i:s'), $timezone, 'U');
  441. $tz = new \DateTimeZone($timezone);
  442. $this->assertEquals($serverTime->format('U'), $result + $tz->getOffset($serverTime));
  443. }
  444. date_default_timezone_set('UTC');
  445. $date = new \DateTime('now', new \DateTimeZone('America/New_York'));
  446. $result = $this->Time->toServer($date, null, 'Y-m-d H:i:s');
  447. $date->setTimezone($this->Time->timezone());
  448. $expected = $date->format('Y-m-d H:i:s');
  449. $this->assertEquals($expected, $result);
  450. $this->_restoreSystemTimezone();
  451. }
  452. /**
  453. * testToAtom method
  454. *
  455. * @return void
  456. */
  457. public function testToAtom() {
  458. $dateTime = new \DateTime;
  459. $this->assertEquals($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  460. }
  461. /**
  462. * testToRss method
  463. *
  464. * @return void
  465. */
  466. public function testToRss() {
  467. $date = '2012-08-12 12:12:45';
  468. $time = strtotime($date);
  469. $this->assertEquals(date('r', $time), $this->Time->toRss($time));
  470. $timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
  471. foreach ($timezones as $timezone) {
  472. $yourTimezone = new \DateTimeZone($timezone);
  473. $yourTime = new \DateTime($date, $yourTimezone);
  474. $userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
  475. $time = $yourTime->format('U');
  476. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $userOffset), "Failed on $timezone");
  477. $this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  478. }
  479. }
  480. /**
  481. * testFormat method
  482. *
  483. * @return void
  484. */
  485. public function testFormat() {
  486. $format = 'D-M-Y';
  487. $tz = date_default_timezone_get();
  488. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  489. foreach ($arr as $val) {
  490. $this->assertEquals(date($format, $val), $this->Time->format($format, $val));
  491. $this->assertEquals(date($format, $val), $this->Time->format($format, $val, false, $tz));
  492. }
  493. $result = $this->Time->format('Y-m-d', null, 'never');
  494. $this->assertEquals('never', $result);
  495. $result = $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
  496. $this->assertEquals('13-01-2012', $result);
  497. $result = $this->Time->format('nonsense', '%d-%m-%Y', 'invalid', 'UTC');
  498. $this->assertEquals('invalid', $result);
  499. $result = $this->Time->format('0000-00-00', '%d-%m-%Y', 'invalid');
  500. $this->assertEquals('invalid', $result);
  501. }
  502. /**
  503. * testOfGmt method
  504. *
  505. * @return void
  506. */
  507. public function testGmt() {
  508. $hour = 3;
  509. $min = 4;
  510. $sec = 2;
  511. $month = 5;
  512. $day = 14;
  513. $year = 2007;
  514. $time = mktime($hour, $min, $sec, $month, $day, $year);
  515. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  516. $this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  517. $hour = date('H');
  518. $min = date('i');
  519. $sec = date('s');
  520. $month = date('m');
  521. $day = date('d');
  522. $year = date('Y');
  523. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  524. $this->assertEquals($expected, $this->Time->gmt(null));
  525. }
  526. /**
  527. * testIsToday method
  528. *
  529. * @return void
  530. */
  531. public function testIsToday() {
  532. $result = $this->Time->isToday('+1 day');
  533. $this->assertFalse($result);
  534. $result = $this->Time->isToday('+1 days');
  535. $this->assertFalse($result);
  536. $result = $this->Time->isToday('+0 day');
  537. $this->assertTrue($result);
  538. $result = $this->Time->isToday('-1 day');
  539. $this->assertFalse($result);
  540. }
  541. /**
  542. * testIsFuture method
  543. *
  544. * @return void
  545. */
  546. public function testIsFuture() {
  547. $this->assertTrue($this->Time->isFuture('+1 month'));
  548. $this->assertTrue($this->Time->isFuture('+1 days'));
  549. $this->assertTrue($this->Time->isFuture('+1 minute'));
  550. $this->assertTrue($this->Time->isFuture('+1 second'));
  551. $this->assertFalse($this->Time->isFuture('-1 second'));
  552. $this->assertFalse($this->Time->isFuture('-1 day'));
  553. $this->assertFalse($this->Time->isFuture('-1 week'));
  554. $this->assertFalse($this->Time->isFuture('-1 month'));
  555. }
  556. /**
  557. * testIsPast method
  558. *
  559. * @return void
  560. */
  561. public function testIsPast() {
  562. $this->assertFalse($this->Time->isPast('+1 month'));
  563. $this->assertFalse($this->Time->isPast('+1 days'));
  564. $this->assertFalse($this->Time->isPast('+1 minute'));
  565. $this->assertFalse($this->Time->isPast('+1 second'));
  566. $this->assertTrue($this->Time->isPast('-1 second'));
  567. $this->assertTrue($this->Time->isPast('-1 day'));
  568. $this->assertTrue($this->Time->isPast('-1 week'));
  569. $this->assertTrue($this->Time->isPast('-1 month'));
  570. }
  571. /**
  572. * testIsThisWeek method
  573. *
  574. * @return void
  575. */
  576. public function testIsThisWeek() {
  577. // A map of days which goes from -1 day of week to +1 day of week
  578. $map = array(
  579. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  580. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  581. 'Sun' => array(-7, 1)
  582. );
  583. $days = $map[date('D')];
  584. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  585. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  586. }
  587. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  588. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  589. }
  590. /**
  591. * testIsThisMonth method
  592. *
  593. * @return void
  594. */
  595. public function testIsThisMonth() {
  596. $result = $this->Time->isThisMonth('+0 day');
  597. $this->assertTrue($result);
  598. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  599. $this->assertTrue($result);
  600. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  601. $this->assertFalse($result);
  602. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  603. $this->assertFalse($result);
  604. }
  605. /**
  606. * testIsThisYear method
  607. *
  608. * @return void
  609. */
  610. public function testIsThisYear() {
  611. $result = $this->Time->isThisYear('+0 day');
  612. $this->assertTrue($result);
  613. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  614. $this->assertTrue($result);
  615. }
  616. /**
  617. * testWasYesterday method
  618. *
  619. * @return void
  620. */
  621. public function testWasYesterday() {
  622. $result = $this->Time->wasYesterday('+1 day');
  623. $this->assertFalse($result);
  624. $result = $this->Time->wasYesterday('+1 days');
  625. $this->assertFalse($result);
  626. $result = $this->Time->wasYesterday('+0 day');
  627. $this->assertFalse($result);
  628. $result = $this->Time->wasYesterday('-1 day');
  629. $this->assertTrue($result);
  630. $result = $this->Time->wasYesterday('-1 days');
  631. $this->assertTrue($result);
  632. $result = $this->Time->wasYesterday('-2 days');
  633. $this->assertFalse($result);
  634. }
  635. /**
  636. * testIsTomorrow method
  637. *
  638. * @return void
  639. */
  640. public function testIsTomorrow() {
  641. $result = $this->Time->isTomorrow('+1 day');
  642. $this->assertTrue($result);
  643. $result = $this->Time->isTomorrow('+1 days');
  644. $this->assertTrue($result);
  645. $result = $this->Time->isTomorrow('+0 day');
  646. $this->assertFalse($result);
  647. $result = $this->Time->isTomorrow('-1 day');
  648. $this->assertFalse($result);
  649. }
  650. /**
  651. * testWasWithinLast method
  652. *
  653. * @return void
  654. */
  655. public function testWasWithinLast() {
  656. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  657. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  658. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  659. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  660. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  661. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  662. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  663. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  664. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  665. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  666. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  667. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  668. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  669. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  670. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  671. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  672. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  673. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  674. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  675. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  676. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  677. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  678. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  679. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  680. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  681. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  682. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  683. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  684. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  685. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  686. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  687. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  688. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  689. }
  690. /**
  691. * testWasWithinLast method
  692. *
  693. * @return void
  694. */
  695. public function testIsWithinNext() {
  696. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  697. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  698. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  699. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  700. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  701. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  702. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  703. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  704. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  705. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  706. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  707. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  708. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  709. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  710. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  711. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  712. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  713. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  714. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  715. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  716. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  717. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  718. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  719. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  720. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  721. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  722. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  723. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  724. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  725. $this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
  726. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 hour'));
  727. $this->assertFalse($this->Time->isWithinNext('1 ', '-1 minute'));
  728. $this->assertFalse($this->Time->isWithinNext('1 ', '-23 hours -59 minutes -59 seconds'));
  729. $this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
  730. $this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
  731. }
  732. /**
  733. * testUserOffset method
  734. *
  735. * @return void
  736. */
  737. public function testUserOffset() {
  738. $timezoneServer = new \DateTimeZone(date_default_timezone_get());
  739. $timeServer = new \DateTime('now', $timezoneServer);
  740. $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
  741. $expected = time();
  742. $result = $this->Time->fromString(time(), $yourTimezone);
  743. $this->assertWithinMargin($expected, $result, 1);
  744. $result = $this->Time->fromString(time(), $timezoneServer->getName());
  745. $this->assertWithinMargin($expected, $result, 1);
  746. $result = $this->Time->fromString(time(), $timezoneServer);
  747. $this->assertWithinMargin($expected, $result, 1);
  748. Configure::write('Config.timezone', $timezoneServer->getName());
  749. $result = $this->Time->fromString(time());
  750. $this->assertWithinMargin($expected, $result, 1);
  751. Configure::delete('Config.timezone');
  752. }
  753. /**
  754. * test fromString()
  755. *
  756. * @return void
  757. */
  758. public function testFromString() {
  759. $result = $this->Time->fromString('');
  760. $this->assertFalse($result);
  761. $result = $this->Time->fromString(0, 0);
  762. $this->assertFalse($result);
  763. $result = $this->Time->fromString('+1 hour');
  764. $expected = strtotime('+1 hour');
  765. $this->assertWithinMargin($expected, $result, 1);
  766. $timezone = date('Z', time());
  767. $result = $this->Time->fromString('+1 hour', $timezone);
  768. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  769. $this->assertWithinMargin($expected, $result, 1);
  770. $timezone = date_default_timezone_get();
  771. $result = $this->Time->fromString('+1 hour', $timezone);
  772. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  773. $this->assertWithinMargin($expected, $result, 1);
  774. date_default_timezone_set('UTC');
  775. $date = new \DateTime('now', new \DateTimeZone('Europe/London'));
  776. $this->Time->fromString($date);
  777. $this->assertEquals('Europe/London', $date->getTimeZone()->getName());
  778. $this->_restoreSystemTimezone();
  779. }
  780. /**
  781. * test fromString() with a DateTime object as the dateString
  782. *
  783. * @return void
  784. */
  785. public function testFromStringWithDateTime() {
  786. date_default_timezone_set('UTC');
  787. $date = new \DateTime('+1 hour', new \DateTimeZone('America/New_York'));
  788. $result = $this->Time->fromString($date, 'UTC');
  789. $date->setTimezone(new \DateTimeZone('UTC'));
  790. $expected = $date->format('U') + $date->getOffset();
  791. $this->assertWithinMargin($expected, $result, 1);
  792. date_default_timezone_set('Australia/Melbourne');
  793. $date = new \DateTime('+1 hour', new \DateTimeZone('America/New_York'));
  794. $result = $this->Time->fromString($date, 'Asia/Kuwait');
  795. $date->setTimezone(new \DateTimeZone('Asia/Kuwait'));
  796. $expected = $date->format('U') + $date->getOffset();
  797. $this->assertWithinMargin($expected, $result, 1);
  798. $this->_restoreSystemTimezone();
  799. }
  800. /**
  801. * Test that datetimes in the default timezone are not modified.
  802. *
  803. * @return void
  804. */
  805. public function testFromStringWithDateTimeNoConversion() {
  806. Configure::write('Config.timezone', date_default_timezone_get());
  807. $date = new \DateTime('2013-04-09');
  808. $result = $this->Time->fromString($date);
  809. $this->assertEquals($result, $date->format('U'));
  810. }
  811. /**
  812. * test converting time specifiers using a time definition localfe file
  813. *
  814. * @return void
  815. */
  816. public function testConvertSpecifiers() {
  817. Configure::write('Config.language', 'es');
  818. $time = strtotime('Thu Jan 14 11:43:39 2010');
  819. $result = $this->Time->convertSpecifiers('%a', $time);
  820. $expected = 'jue';
  821. $this->assertEquals($expected, $result);
  822. $result = $this->Time->convertSpecifiers('%A', $time);
  823. $expected = 'jueves';
  824. $this->assertEquals($expected, $result);
  825. $result = $this->Time->convertSpecifiers('%c', $time);
  826. $expected = 'jue %d ene %Y %H:%M:%S %Z';
  827. $this->assertEquals($expected, $result);
  828. $result = $this->Time->convertSpecifiers('%C', $time);
  829. $expected = '20';
  830. $this->assertEquals($expected, $result);
  831. $result = $this->Time->convertSpecifiers('%D', $time);
  832. $expected = '%m/%d/%y';
  833. $this->assertEquals($expected, $result);
  834. $result = $this->Time->convertSpecifiers('%b', $time);
  835. $expected = 'ene';
  836. $this->assertEquals($expected, $result);
  837. $result = $this->Time->convertSpecifiers('%h', $time);
  838. $expected = 'ene';
  839. $this->assertEquals($expected, $result);
  840. $result = $this->Time->convertSpecifiers('%B', $time);
  841. $expected = 'enero';
  842. $this->assertEquals($expected, $result);
  843. $result = $this->Time->convertSpecifiers('%n', $time);
  844. $expected = "\n";
  845. $this->assertEquals($expected, $result);
  846. $result = $this->Time->convertSpecifiers('%n', $time);
  847. $expected = "\n";
  848. $this->assertEquals($expected, $result);
  849. $result = $this->Time->convertSpecifiers('%p', $time);
  850. $expected = 'AM';
  851. $this->assertEquals($expected, $result);
  852. $result = $this->Time->convertSpecifiers('%P', $time);
  853. $expected = 'am';
  854. $this->assertEquals($expected, $result);
  855. $result = $this->Time->convertSpecifiers('%r', $time);
  856. $expected = '%I:%M:%S AM';
  857. $this->assertEquals($expected, $result);
  858. $result = $this->Time->convertSpecifiers('%R', $time);
  859. $expected = '11:43';
  860. $this->assertEquals($expected, $result);
  861. $result = $this->Time->convertSpecifiers('%t', $time);
  862. $expected = "\t";
  863. $this->assertEquals($expected, $result);
  864. $result = $this->Time->convertSpecifiers('%T', $time);
  865. $expected = '%H:%M:%S';
  866. $this->assertEquals($expected, $result);
  867. $result = $this->Time->convertSpecifiers('%u', $time);
  868. $expected = 4;
  869. $this->assertEquals($expected, $result);
  870. $result = $this->Time->convertSpecifiers('%x', $time);
  871. $expected = '%d/%m/%y';
  872. $this->assertEquals($expected, $result);
  873. $result = $this->Time->convertSpecifiers('%X', $time);
  874. $expected = '%H:%M:%S';
  875. $this->assertEquals($expected, $result);
  876. }
  877. /**
  878. * test convert %e on windows.
  879. *
  880. * @return void
  881. */
  882. public function testConvertPercentE() {
  883. $this->skipIf(DIRECTORY_SEPARATOR !== '\\', 'Cannot run windows tests on non-windows OS.');
  884. $time = strtotime('Thu Jan 14 11:43:39 2010');
  885. $result = $this->Time->convertSpecifiers('%e', $time);
  886. $expected = '14';
  887. $this->assertEquals($expected, $result);
  888. $result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01'));
  889. $expected = ' 1';
  890. $this->assertEquals($expected, $result);
  891. }
  892. /**
  893. * test formatting dates taking in account preferred i18n locale file
  894. *
  895. * @return void
  896. */
  897. public function testI18nFormat() {
  898. Configure::write('Config.language', 'es');
  899. $time = strtotime('Thu Jan 14 13:59:28 2010');
  900. $result = $this->Time->i18nFormat($time);
  901. $expected = '14/01/10';
  902. $this->assertEquals($expected, $result);
  903. $result = $this->Time->i18nFormat($time, '%c');
  904. $expected = 'jue 14 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
  905. $this->assertEquals($expected, $result);
  906. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  907. $expected = 'Time is 01:59:28 PM, and date is 14/01/10';
  908. $this->assertEquals($expected, $result);
  909. $time = strtotime('Wed Jan 13 13:59:28 2010');
  910. $result = $this->Time->i18nFormat($time);
  911. $expected = '13/01/10';
  912. $this->assertEquals($expected, $result);
  913. $result = $this->Time->i18nFormat($time, '%c');
  914. $expected = 'mié 13 ene 2010 13:59:28 ' . utf8_encode(strftime('%Z', $time));
  915. $this->assertEquals($expected, $result);
  916. $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
  917. $expected = 'Time is 01:59:28 PM, and date is 13/01/10';
  918. $this->assertEquals($expected, $result);
  919. $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
  920. $expected = 'Date invalid';
  921. $this->assertEquals($expected, $result);
  922. }
  923. /**
  924. * test new format() syntax which inverts first and second parameters
  925. *
  926. * @return void
  927. */
  928. public function testFormatNewSyntax() {
  929. $time = time();
  930. $this->assertEquals($this->Time->format($time), $this->Time->i18nFormat($time));
  931. $this->assertEquals($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c'));
  932. }
  933. /**
  934. * testListTimezones
  935. *
  936. * @return void
  937. */
  938. public function testListTimezones() {
  939. $return = Time::listTimezones();
  940. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  941. $this->assertEquals('Bangkok', $return['Asia']['Asia/Bangkok']);
  942. $this->assertTrue(isset($return['America']['America/Argentina/Buenos_Aires']));
  943. $this->assertEquals('Argentina/Buenos_Aires', $return['America']['America/Argentina/Buenos_Aires']);
  944. $this->assertTrue(isset($return['UTC']['UTC']));
  945. $this->assertFalse(isset($return['Cuba']));
  946. $this->assertFalse(isset($return['US']));
  947. $return = Time::listTimezones('#^Asia/#');
  948. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  949. $this->assertFalse(isset($return['Pacific']));
  950. $return = Time::listTimezones('#^(America|Pacific)/#', null, false);
  951. $this->assertTrue(isset($return['America/Argentina/Buenos_Aires']));
  952. $this->assertTrue(isset($return['Pacific/Tahiti']));
  953. $return = Time::listTimezones(\DateTimeZone::ASIA);
  954. $this->assertTrue(isset($return['Asia']['Asia/Bangkok']));
  955. $this->assertFalse(isset($return['Pacific']));
  956. $return = Time::listTimezones(\DateTimeZone::PER_COUNTRY, 'US', false);
  957. $this->assertTrue(isset($return['Pacific/Honolulu']));
  958. $this->assertFalse(isset($return['Asia/Bangkok']));
  959. }
  960. /**
  961. * Tests that using Cake\Utility\Time::format() with the correct sytax actually converts
  962. * from one timezone to the other correctly
  963. *
  964. * @return void
  965. */
  966. public function testCorrectTimezoneConversion() {
  967. date_default_timezone_set('UTC');
  968. $date = '2012-01-01 10:00:00';
  969. $converted = Time::format($date, '%Y-%m-%d %H:%M', '', 'Europe/Copenhagen');
  970. $expected = new \DateTime($date);
  971. $expected->setTimezone(new \DateTimeZone('Europe/Copenhagen'));
  972. $this->assertEquals($expected->format('Y-m-d H:i'), $converted);
  973. }
  974. }