TimeTest.php 36 KB

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