PageRenderTime 179ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/CakeTimeTest.php

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