PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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