TimeLibTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. App::uses('TimeLib', 'Tools.Utility');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class TimeLibTest extends MyCakeTestCase {
  5. public $Time = null;
  6. /**
  7. * TimeLibTest::testObject()
  8. *
  9. * @return void
  10. */
  11. public function testObject() {
  12. $this->Time = new TimeLib();
  13. $this->assertTrue(is_object($this->Time));
  14. $this->assertInstanceOf('TimeLib', $this->Time);
  15. }
  16. /**
  17. * Currently only works with timezoned localized values, not with UTC!!!
  18. *
  19. * @return void
  20. */
  21. public function testIncrementDate() {
  22. $timezone = Configure::read('Config.timezone');
  23. //$timezone = Datetime::timezone();
  24. Configure::write('Config.timezone', 'Europe/Berlin');
  25. $phpTimezone = date_default_timezone_get();
  26. date_default_timezone_set('Europe/Berlin');
  27. $from = '2012-12-31';
  28. $Date = TimeLib::incrementDate($from, 0, 0);
  29. $this->assertSame($from, $Date->format(FORMAT_DB_DATE));
  30. $from = '2012-12-31';
  31. $Date = TimeLib::incrementDate($from, 0, 1);
  32. $this->assertSame('2013-01-31', $Date->format(FORMAT_DB_DATE));
  33. $from = '2012-12-31';
  34. $Date = TimeLib::incrementDate($from, 0, 2);
  35. $this->assertSame('2013-02-28', $Date->format(FORMAT_DB_DATE));
  36. $from = '2012-12-31';
  37. $Date = TimeLib::incrementDate($from, 0, 4);
  38. $this->assertSame('2013-04-30', $Date->format(FORMAT_DB_DATE));
  39. $from = '2012-12-31';
  40. $Date = TimeLib::incrementDate($from, 1, 0);
  41. $this->assertSame('2013-12-31', $Date->format(FORMAT_DB_DATE));
  42. // from leap year
  43. $from = '2008-02-29';
  44. $Date = TimeLib::incrementDate($from, 1, 0);
  45. $this->assertSame('2009-02-28', $Date->format(FORMAT_DB_DATE));
  46. // into leap year
  47. $from = '2007-02-28';
  48. $Date = TimeLib::incrementDate($from, 1, 0);
  49. $this->assertSame('2008-02-29', $Date->format(FORMAT_DB_DATE));
  50. // other direction
  51. $from = '2012-12-31';
  52. $Date = TimeLib::incrementDate($from, 0, -1);
  53. $this->assertSame('2012-11-30', $Date->format(FORMAT_DB_DATE));
  54. $from = '2012-12-31';
  55. $Date = TimeLib::incrementDate($from, -1, -1);
  56. $this->assertSame('2011-11-30', $Date->format(FORMAT_DB_DATE));
  57. // including days
  58. $from = '2012-12-31';
  59. $Date = TimeLib::incrementDate($from, 0, 1, 1);
  60. $this->assertSame('2013-02-01', $Date->format(FORMAT_DB_DATE));
  61. // including days
  62. $from = '2012-12-31';
  63. $Date = TimeLib::incrementDate($from, 0, 1, 5);
  64. $this->assertSame('2013-02-05', $Date->format(FORMAT_DB_DATE));
  65. Configure::write('Config.timezone', $timezone);
  66. date_default_timezone_set($phpTimezone);
  67. }
  68. /**
  69. * TimeLibTest::testNiceDate()
  70. *
  71. * @return void
  72. */
  73. public function testNiceDate() {
  74. $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
  75. //$this->assertTrue(!empty($res));
  76. $values = array(
  77. array('2009-12-01 00:00:00', FORMAT_NICE_YMD, '01.12.2009'),
  78. array('2009-12-01 00:00:00', FORMAT_NICE_M_FULL, 'December'),
  79. );
  80. foreach ($values as $v) {
  81. $ret = TimeLib::niceDate($v[0], $v[1]);
  82. //$this->debug($ret);
  83. $this->assertEquals($v[2], $ret);
  84. }
  85. }
  86. /**
  87. * Test that input as date only (YYYY-MM-DD) does not suddendly return a
  88. * different date on output due to timezone differences.
  89. * Here the timezone should not apply since we only input date and only output
  90. * date (time itself is irrelevant).
  91. *
  92. * @return void
  93. */
  94. public function testDateWithTimezone() {
  95. $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
  96. //$this->assertTrue(!empty($res));
  97. Configure::write('Config.timezone', 'America/Anchorage');
  98. $ret = TimeLib::niceDate('2009-12-01');
  99. //debug($ret);
  100. $this->assertEquals('01.12.2009', $ret);
  101. $ret = TimeLib::localDate('2009-12-01');
  102. //debug($ret);
  103. $this->assertEquals('01.12.2009', $ret);
  104. }
  105. /**
  106. * TimeLibTest::testLocalDate()
  107. *
  108. * @return void
  109. */
  110. public function testLocalDate() {
  111. $this->skipIf(php_sapi_name() === 'cli', 'for now');
  112. $res = setlocale(LC_TIME, array('de_DE.UTF-8', 'deu_deu'));
  113. $this->assertTrue(!empty($res));
  114. $values = array(
  115. array('2009-12-01 00:00:00', FORMAT_LOCAL_YMD, '01.12.2009'),
  116. array('2009-12-01 00:00:00', FORMAT_LOCAL_M_FULL, 'Dezember'),
  117. );
  118. foreach ($values as $v) {
  119. $ret = TimeLib::localDate($v[0], $v[1]);
  120. //$this->debug($ret);
  121. $this->assertEquals($v[2], $ret);
  122. }
  123. }
  124. /**
  125. * TimeLibTest::testParseLocalizedDate()
  126. *
  127. * @return void
  128. */
  129. public function testParseLocalizedDate() {
  130. $this->out($this->_header(__FUNCTION__), true);
  131. $ret = TimeLib::parseLocalizedDate('15-Feb-2009', 'j-M-Y', 'start');
  132. //$this->debug($ret);
  133. $this->assertEquals('2009-02-15 00:00:00', $ret);
  134. // problem when not passing months or days as well - no way of knowing how exact the date was
  135. $ret = TimeLib::parseLocalizedDate('2009', 'Y', 'start');
  136. //pr($ret);
  137. //$this->assertEquals($ret, '2009-01-01 00:00:00');
  138. $ret = TimeLib::parseLocalizedDate('Feb 2009', 'M Y', 'start');
  139. //pr($ret);
  140. //$this->assertEquals($ret, '2009-02-01 00:00:00');
  141. $values = array(
  142. array(__('Today'), array(date(FORMAT_DB_DATETIME, mktime(0, 0, 0, date('m'), date('d'), date('Y'))), date(FORMAT_DB_DATETIME, mktime(23, 59, 59, date('m'), date('d'), date('Y'))))),
  143. array('2010', array('2010-01-01 00:00:00', '2010-12-31 23:59:59')),
  144. array('23.02.2011', array('2011-02-23 00:00:00', '2011-02-23 23:59:59')),
  145. array('22/02/2011', array('2011-02-22 00:00:00', '2011-02-22 23:59:59')),
  146. array('3/2/11', array('2011-02-03 00:00:00', '2011-02-03 23:59:59')),
  147. array('2/12/9', array('2009-12-02 00:00:00', '2009-12-02 23:59:59')),
  148. array('12/2009', array('2009-12-01 00:00:00', '2009-12-31 23:59:59')),
  149. );
  150. foreach ($values as $v) {
  151. $ret = TimeLib::parseLocalizedDate($v[0], null, 'start');
  152. //pr($ret);
  153. $this->assertEquals($v[1][0], $ret);
  154. $ret = TimeLib::parseLocalizedDate($v[0], null, 'end');
  155. //pr($ret);
  156. $this->assertEquals($v[1][1], $ret);
  157. }
  158. }
  159. /**
  160. * TimeLibTest::testPeriod()
  161. *
  162. * @return void
  163. */
  164. public function testPeriod() {
  165. $this->out($this->_header(__FUNCTION__), true);
  166. $values = array(
  167. array(__('Today'), array(date(FORMAT_DB_DATETIME, mktime(0, 0, 0, date('m'), date('d'), date('Y'))), date(FORMAT_DB_DATETIME, mktime(23, 59, 59, date('m'), date('d'), date('Y'))))),
  168. array('2010', array('2010-01-01 00:00:00', '2010-12-31 23:59:59')),
  169. array('2011-02', array('2011-02-01 00:00:00', '2011-02-28 23:59:59')),
  170. array('2012-02', array('2012-02-01 00:00:00', '2012-02-29 23:59:59')),
  171. array('2010-02-23', array('2010-02-23 00:00:00', '2010-02-23 23:59:59')),
  172. array('2010-02-23 bis 2010-02-26', array('2010-02-23 00:00:00', '2010-02-26 23:59:59')),
  173. //array('2010-02-23 11:11:11 bis 2010-02-23 11:12:01', array('2010-02-23 11:11:11', '2010-02-23 11:12:01')),
  174. // localized
  175. array('23.02.2011', array('2011-02-23 00:00:00', '2011-02-23 23:59:59')),
  176. array('23.2.2010 bis 26.2.2011', array('2010-02-23 00:00:00', '2011-02-26 23:59:59')),
  177. );
  178. foreach ($values as $v) {
  179. $ret = TimeLib::period($v[0]);
  180. //pr($ret);
  181. $this->assertEquals($v[1], $ret);
  182. }
  183. }
  184. /**
  185. * TimeLibTest::testPeriodAsSql()
  186. *
  187. * @return void
  188. */
  189. public function testPeriodAsSql() {
  190. $this->out($this->_header(__FUNCTION__), true);
  191. $values = array(
  192. array(__('Today'), "(Model.field >= '" . date(FORMAT_DB_DATE) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
  193. array(__('Yesterday') . ' ' . __('until') . ' ' . __('Today'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
  194. array(__('Today') . ' ' . __('until') . ' ' . __('Tomorrow'), "(Model.field >= '" . date(FORMAT_DB_DATE, time()) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE, time() + DAY) . " 23:59:59')"),
  195. array(__('Yesterday') . ' ' . __('until') . ' ' . __('Tomorrow'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE, time() + DAY) . " 23:59:59')"),
  196. );
  197. foreach ($values as $v) {
  198. $ret = TimeLib::periodAsSql($v[0], 'Model.field');
  199. //pr($v[1]);
  200. //pr($ret);
  201. $this->assertSame($v[1], $ret);
  202. }
  203. }
  204. /**
  205. * TimeLibTest::testDifference()
  206. *
  207. * @return void
  208. */
  209. public function testDifference() {
  210. $this->out($this->_header(__FUNCTION__), true);
  211. $values = array(
  212. array('2010-02-23 11:11:11', '2010-02-23 11:12:01', 50),
  213. array('2010-02-23 11:11:11', '2010-02-24 11:12:01', DAY + 50)
  214. );
  215. foreach ($values as $v) {
  216. $ret = TimeLib::difference($v[0], $v[1]);
  217. $this->assertEquals($v[2], $ret);
  218. }
  219. }
  220. /**
  221. * TimeLibTest::testIsLeapYear()
  222. *
  223. * @return void
  224. */
  225. public function testIsLeapYear() {
  226. $is = TimeLib::isLeapYear('2000');
  227. $this->assertTrue($is);
  228. $is = TimeLib::isLeapYear('2001');
  229. $this->assertFalse($is);
  230. }
  231. /**
  232. * TimeLibTest::testIsInRange()
  233. *
  234. * @return void
  235. */
  236. public function testIsInRange() {
  237. $is = TimeLib::isInRange(date(FORMAT_DB_DATETIME, time() + 22 * HOUR), DAY);
  238. $this->assertTrue($is);
  239. $is = TimeLib::isInRange(date(FORMAT_DB_DATETIME, time() + 26 * HOUR), DAY);
  240. $this->assertFalse($is);
  241. }
  242. /**
  243. * TimeLibTest::testAgeBounds()
  244. *
  245. * @return void
  246. */
  247. public function testAgeBounds() {
  248. $this->out($this->_header(__FUNCTION__), true);
  249. $values = array(
  250. array(20, 20, array('min' => '1990-07-07', 'max' => '1991-07-06')),
  251. array(10, 30, array('min' => '1980-07-07', 'max' => '2001-07-06')),
  252. array(11, 12, array('min' => '1998-07-07', 'max' => '2000-07-06'))
  253. );
  254. foreach ($values as $v) {
  255. //echo $v[0].'/'.$v[1];
  256. $ret = TimeLib::ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
  257. //pr($ret);
  258. if (isset($v[2])) {
  259. $this->assertSame($v[2], $ret);
  260. $this->assertEquals($v[0], TimeLib::age($v[2]['max'], '2011-07-06'));
  261. $this->assertEquals($v[1], TimeLib::age($v[2]['min'], '2011-07-06'));
  262. }
  263. }
  264. }
  265. /**
  266. * TimeLibTest::testAgeByYear()
  267. *
  268. * @return void
  269. */
  270. public function testAgeByYear() {
  271. $this->out($this->_header(__FUNCTION__), true);
  272. // year only
  273. $is = TimeLib::ageByYear(2000);
  274. $this->out($is);
  275. $this->assertEquals((date('Y') - 2001) . '/' . (date('Y') - 2000), $is);
  276. $is = TimeLib::ageByYear(1985);
  277. $this->assertEquals((date('Y') - 1986) . '/' . (date('Y') - 1985), $is);
  278. // with month
  279. if (($month = date('n') + 1) <= 12) {
  280. $is = TimeLib::ageByYear(2000, $month);
  281. $this->out($is);
  282. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  283. $this->assertSame(date('Y') - 2001, $is); //null, '2000/'.$month
  284. }
  285. if (($month = date('n') - 1) >= 1) {
  286. $is = TimeLib::ageByYear(2000, $month);
  287. $this->out($is);
  288. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  289. $this->assertSame(date('Y') - 2000, $is); //null, '2000/'.$month)
  290. }
  291. }
  292. /**
  293. * TimeLibTest::testDaysInMonth()
  294. *
  295. * @return void
  296. */
  297. public function testDaysInMonth() {
  298. $this->out($this->_header(__FUNCTION__), true);
  299. $ret = TimeLib::daysInMonth('2004', '3');
  300. $this->assertEquals(31, $ret);
  301. $ret = TimeLib::daysInMonth('2006', '4');
  302. $this->assertEquals(30, $ret);
  303. $ret = TimeLib::daysInMonth('2007', '2');
  304. $this->assertEquals(28, $ret);
  305. $ret = TimeLib::daysInMonth('2008', '2');
  306. $this->assertEquals(29, $ret);
  307. }
  308. /**
  309. * TimeLibTest::testDay()
  310. *
  311. * @return void
  312. */
  313. public function testDay() {
  314. $this->out($this->_header(__FUNCTION__), true);
  315. $ret = TimeLib::day('0');
  316. $this->assertEquals(__('Sunday'), $ret);
  317. $ret = TimeLib::day(2, true);
  318. $this->assertEquals(__('Tue'), $ret);
  319. $ret = TimeLib::day(6);
  320. $this->assertEquals(__('Saturday'), $ret);
  321. $ret = TimeLib::day(6, false, 1);
  322. $this->assertEquals(__('Sunday'), $ret);
  323. $ret = TimeLib::day(0, false, 2);
  324. $this->assertEquals(__('Tuesday'), $ret);
  325. $ret = TimeLib::day(1, false, 6);
  326. $this->assertEquals(__('Sunday'), $ret);
  327. }
  328. /**
  329. * TimeLibTest::testMonth()
  330. *
  331. * @return void
  332. */
  333. public function testMonth() {
  334. $this->out($this->_header(__FUNCTION__), true);
  335. $ret = TimeLib::month('11');
  336. $this->assertEquals(__('November'), $ret);
  337. $ret = TimeLib::month(1);
  338. $this->assertEquals(__('January'), $ret);
  339. $ret = TimeLib::month(2, true, array('appendDot' => true));
  340. $this->assertEquals(__('Feb') . '.', $ret);
  341. $ret = TimeLib::month(5, true, array('appendDot' => true));
  342. $this->assertEquals(__('May'), $ret);
  343. }
  344. /**
  345. * TimeLibTest::testDays()
  346. *
  347. * @return void
  348. */
  349. public function testDays() {
  350. $this->out($this->_header(__FUNCTION__), true);
  351. $ret = TimeLib::days();
  352. $this->assertTrue(count($ret) === 7);
  353. }
  354. /**
  355. * TimeLibTest::testMonths()
  356. *
  357. * @return void
  358. */
  359. public function testMonths() {
  360. $this->out($this->_header(__FUNCTION__), true);
  361. $ret = TimeLib::months();
  362. $this->assertTrue(count($ret) === 12);
  363. }
  364. /**
  365. * TimeLibTest::testRelLengthOfTime()
  366. *
  367. * @return void
  368. */
  369. public function testRelLengthOfTime() {
  370. $this->out($this->_header(__FUNCTION__), true);
  371. $ret = TimeLib::relLengthOfTime('1990-11-20');
  372. //pr($ret);
  373. $ret = TimeLib::relLengthOfTime('2012-11-20');
  374. //pr($ret);
  375. }
  376. /**
  377. * TimeLibTest::testLengthOfTime()
  378. *
  379. * @return void
  380. */
  381. public function testLengthOfTime() {
  382. $this->out($this->_header(__FUNCTION__), true);
  383. $ret = TimeLib::lengthOfTime(60);
  384. //pr($ret);
  385. // FIX ME! Doesn't work!
  386. $ret = TimeLib::lengthOfTime(-60);
  387. //pr($ret);
  388. $ret = TimeLib::lengthOfTime(-121);
  389. //pr($ret);
  390. }
  391. /**
  392. * TimeLibTest::testFuzzyFromOffset()
  393. *
  394. * @return void
  395. */
  396. public function testFuzzyFromOffset() {
  397. $this->out($this->_header(__FUNCTION__), true);
  398. $ret = TimeLib::fuzzyFromOffset(MONTH);
  399. //pr($ret);
  400. $ret = TimeLib::fuzzyFromOffset(120);
  401. //pr($ret);
  402. $ret = TimeLib::fuzzyFromOffset(DAY);
  403. //pr($ret);
  404. $ret = TimeLib::fuzzyFromOffset(DAY + 2 * MINUTE);
  405. //pr($ret);
  406. // FIX ME! Doesn't work!
  407. $ret = TimeLib::fuzzyFromOffset(-DAY);
  408. //pr($ret);
  409. }
  410. /**
  411. * TimeLibTest::testCweekMod()
  412. *
  413. * @return void
  414. */
  415. public function testCweekMod() {
  416. $result = TimeLib::cWeekMod(0);
  417. $this->assertEquals(0, $result);
  418. $result = TimeLib::cWeekMod(1);
  419. $this->assertEquals(1, $result);
  420. $result = TimeLib::cWeekMod(6);
  421. $this->assertEquals(0, $result);
  422. }
  423. /**
  424. * TimeLibTest::testGetGmtOffset()
  425. *
  426. * @return void
  427. */
  428. public function testGetGmtOffset() {
  429. $result = TimeLib::getGmtOffset();
  430. $this->assertEquals(0, $result);
  431. $result = TimeLib::getGmtOffset('Europe/Berlin');
  432. $this->assertTrue($result > 0, $result);
  433. $result = TimeLib::getGmtOffset('America/Los_Angeles');
  434. $this->assertTrue($result < 0, $result);
  435. }
  436. /**
  437. * TimeLibTest::testTimezoneByCoordinates()
  438. *
  439. * @return void
  440. */
  441. public function testTimezoneByCoordinates() {
  442. $result = TimeLib::timezoneByCoordinates(48, 11);
  443. $this->assertEquals('Europe/Vaduz', $result);
  444. }
  445. /**
  446. * TimeLibTest::testCweekDay()
  447. *
  448. * @return void
  449. */
  450. public function testCweekDay() {
  451. $this->out($this->_header(__FUNCTION__), true);
  452. // wednesday
  453. $ret = TimeLib::cweekDay(51, 2011, 2);
  454. $this->out('51, 2011, 2');
  455. $this->out(date(FORMAT_DB_DATETIME, $ret));
  456. $this->assertTrue($ret >= 1324422000 && $ret <= 1324425600);
  457. //$this->assertEquals(1324422000, $ret);
  458. }
  459. public function testCweeks() {
  460. $this->out($this->_header(__FUNCTION__), true);
  461. $ret = TimeLib::cweeks('2004');
  462. $this->assertEquals(53, $ret);
  463. $ret = TimeLib::cweeks('2010');
  464. $this->assertEquals(52, $ret);
  465. $ret = TimeLib::cweeks('2006');
  466. $this->assertEquals(52, $ret);
  467. $ret = TimeLib::cweeks('2007');
  468. $this->assertEquals(52, $ret);
  469. /*
  470. for ($i = 1990; $i < 2020; $i++) {
  471. $this->out(TimeLib::cweeks($i).BR;
  472. }
  473. */
  474. }
  475. public function testCweekBeginning() {
  476. $this->out($this->_header(__FUNCTION__), true);
  477. $values = array(
  478. '2001' => 978303600, # Mon 01.01.2001, 00:00
  479. '2006' => 1136156400, # Mon 02.01.2006, 00:00
  480. '2010' => 1262559600, # Mon 04.01.2010, 00:00
  481. '2013' => 1356908400, # Mon 31.12.2012, 00:00
  482. );
  483. foreach ($values as $year => $expected) {
  484. $ret = TimeLib::cweekBeginning($year);
  485. $this->out($ret);
  486. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  487. //$this->assertEquals($ret, $expected, null, $year);
  488. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  489. }
  490. $values = array(
  491. array('2001', '1', 978303600), # Mon 01.01.2001, 00:00:00
  492. array('2001', '2', 978908400), # Mon 08.01.2001, 00:00:00
  493. array('2001', '5', 980722800), # Mon 29.01.2001, 00:00:00
  494. array('2001', '52', 1009148400), # Mon 24.12.2001, 00:00:00
  495. array('2013', '11', 1362956400), # Mon 11.03.2013, 00:00:00
  496. array('2006', '3', 1137366000), # Mon 16.01.2006, 00:00:00
  497. );
  498. foreach ($values as $v) {
  499. $ret = TimeLib::cweekBeginning($v[0], $v[1]);
  500. $this->out($ret);
  501. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  502. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  503. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  504. }
  505. }
  506. public function testCweekEnding() {
  507. $this->out($this->_header(__FUNCTION__), true);
  508. $values = array(
  509. '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
  510. '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
  511. '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
  512. '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
  513. );
  514. foreach ($values as $year => $expected) {
  515. $ret = TimeLib::cweekEnding($year);
  516. $this->out($ret);
  517. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  518. //$this->assertSame($expected, $ret);
  519. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  520. }
  521. $values = array(
  522. array('2001', '1', 978908399), # Sun 07.01.2001, 23:59:59
  523. array('2001', '2', 979513199), # Sun 14.01.2001, 23:59:59
  524. array('2001', '5', 981327599), # Sun 04.02.2001, 23:59:59
  525. array('2001', '52', 1009753199), # Sun 30.12.2001, 23:59:59
  526. array('2013', '11', 1363561199), # Sun 17.03.2013, 23:59:59
  527. array('2006', '3', 1137970799), # Sun 22.01.2006, 23:59:59
  528. );
  529. foreach ($values as $v) {
  530. $ret = TimeLib::cweekEnding($v[0], $v[1]);
  531. $this->out($ret);
  532. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  533. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  534. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  535. }
  536. }
  537. /**
  538. * TimeLibTest::testAgeByHoroscop()
  539. *
  540. * @return void
  541. */
  542. public function testAgeByHoroscop() {
  543. App::uses('ZodiacLib', 'Tools.Misc');
  544. $this->skipIf(php_sapi_name() === 'cli', 'Fix these tests');
  545. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
  546. // between xxxx-08-24 and xxxx-09-23 the latter, otherwise the first:
  547. $this->assertEquals(date('Y') - 2000 - 1, $is);
  548. $this->assertEquals(array(date('Y') - 2000 - 1, date('Y') - 2000), $is);
  549. $is = TimeLib::ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
  550. //pr($is);
  551. $this->assertEquals(date('Y') - 1991 - 1, $is);
  552. $is = TimeLib::ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
  553. //pr($is);
  554. $this->assertEquals(array(date('Y') - 1986 - 1, date('Y') - 1986), $is);
  555. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
  556. //debug($is);
  557. $this->assertEquals(date('Y') - 2000 - 1, $is); //array(10, 11)
  558. }
  559. /**
  560. * TimeLibTest::testAgeRange()
  561. *
  562. * @return void
  563. */
  564. public function testAgeRange() {
  565. $is = TimeLib::ageRange(2000);
  566. $this->assertEquals(date('Y') - 2000 - 1, $is);
  567. $is = TimeLib::ageRange(date('Y') - 11, null, null, 5);
  568. $this->assertEquals(array(6, 10), $is);
  569. $is = TimeLib::ageRange(date('Y') - 13, null, null, 5);
  570. $this->assertEquals(array(11, 15), $is);
  571. $is = TimeLib::ageRange(1985, 23, 11);
  572. $this->assertEquals(date('Y') - 1985 - 1, $is);
  573. $is = TimeLib::ageRange(date('Y') - 29, null, null, 6);
  574. $this->assertEquals(array(25, 30), $is);
  575. $is = TimeLib::ageRange(date('Y') - 29, 21, 11, 7);
  576. $this->assertEquals(array(22, 28), $is);
  577. }
  578. /**
  579. * TimeLibTest::testParseDate()
  580. *
  581. * @return void
  582. */
  583. public function testParseDate() {
  584. //echo $this->_header(__FUNCTION__);
  585. $tests = array(
  586. '2010-12-11' => 1292022000,
  587. '2010-01-02' => 1262386800,
  588. '10-01-02' => 1262386800,
  589. '2.1.2010' => 1262386800,
  590. '2.1.10' => 1262386800,
  591. '02.01.10' => 1262386800,
  592. '02.01.2010' => 1262386800,
  593. '02.01.2010 22:11' => 1262386800,
  594. '2010-01-02 22:11' => 1262386800,
  595. );
  596. foreach ($tests as $was => $expected) {
  597. $is = TimeLib::parseDate($was);
  598. $this->assertTrue($is <= $expected + HOUR && $is >= $expected);
  599. }
  600. }
  601. /**
  602. * TimeLibTest::testParseTime()
  603. *
  604. * @return void
  605. */
  606. public function testParseTime() {
  607. //echo $this->_header(__FUNCTION__);
  608. $tests = array(
  609. '2:4' => 7440,
  610. '2:04' => 7440,
  611. '2' => 7200,
  612. '1,5' => 3600 + 1800,
  613. '1.5' => 3600 + 1800,
  614. '1.50' => 3600 + 1800,
  615. '1.01' => 3660,
  616. ':4' => 240,
  617. ':04' => 240,
  618. ':40' => 40 * MINUTE,
  619. '1:2:4' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  620. '01:2:04' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  621. '0:2:04' => 2 * MINUTE + 4 * SECOND,
  622. '::4' => 4 * SECOND,
  623. '::04' => 4 * SECOND,
  624. '::40' => 40 * SECOND,
  625. '2011-11-12 10:10:10' => 10 * HOUR + 10 * MINUTE + 10 * SECOND,
  626. );
  627. // positive
  628. foreach ($tests as $was => $expected) {
  629. $is = TimeLib::parseTime($was);
  630. //pr($is);
  631. $this->assertEquals($expected, $is);
  632. }
  633. unset($tests['2011-11-12 10:10:10']);
  634. // negative
  635. foreach ($tests as $was => $expected) {
  636. $is = TimeLib::parseTime('-' . $was);
  637. //pr($is);
  638. $this->assertEquals(-$expected, $is);
  639. }
  640. }
  641. /**
  642. * TimeLibTest::testBuildTime()
  643. *
  644. * @return void
  645. */
  646. public function testBuildTime() {
  647. //echo $this->_header(__FUNCTION__);
  648. $tests = array(
  649. 7440 => '2:04',
  650. 7220 => '2:00', # 02:00:20 => rounded to 2:00:00
  651. 5400 => '1:30',
  652. 3660 => '1:01',
  653. );
  654. // positive
  655. foreach ($tests as $was => $expected) {
  656. $is = TimeLib::buildTime($was);
  657. //pr($is);
  658. $this->assertEquals($expected, $is);
  659. }
  660. // negative
  661. foreach ($tests as $was => $expected) {
  662. $is = TimeLib::buildTime(-$was);
  663. //pr($is);
  664. $this->assertEquals('-' . $expected, $is);
  665. }
  666. }
  667. /**
  668. * TimeLibTest::testBuildDefaultTime()
  669. *
  670. * @return void
  671. */
  672. public function testBuildDefaultTime() {
  673. //echo $this->_header(__FUNCTION__);
  674. $tests = array(
  675. 7440 => '02:04:00',
  676. 7220 => '02:00:20',
  677. 5400 => '01:30:00',
  678. 3660 => '01:01:00',
  679. 1 * HOUR + 2 * MINUTE + 4 * SECOND => '01:02:04',
  680. );
  681. foreach ($tests as $was => $expected) {
  682. $is = TimeLib::buildDefaultTime($was);
  683. //pr($is);
  684. $this->assertEquals($expected, $is);
  685. }
  686. }
  687. /**
  688. * 9.30 => 9.50
  689. *
  690. * @return void
  691. */
  692. public function testStandardDecimal() {
  693. //echo $this->_header(__FUNCTION__);
  694. $value = '9.30';
  695. $is = TimeLib::standardToDecimalTime($value);
  696. $this->assertEquals('9.50', round($is, 2));
  697. $value = '9.3';
  698. $is = TimeLib::standardToDecimalTime($value);
  699. $this->assertEquals('9.50', round($is, 2));
  700. }
  701. /**
  702. * 9.50 => 9.30
  703. *
  704. * @return void
  705. */
  706. public function testDecimalStandard() {
  707. //echo $this->_header(__FUNCTION__);
  708. $value = '9.50';
  709. $is = TimeLib::decimalToStandardTime($value);
  710. $this->assertEquals('9.3', round($is, 2));
  711. $value = '9.5';
  712. $is = TimeLib::decimalToStandardTime($value);
  713. //pr($is);
  714. $this->assertEquals('9.3', $is);
  715. $is = TimeLib::decimalToStandardTime($value, 2);
  716. //pr($is);
  717. $this->assertEquals('9.30', $is);
  718. $is = TimeLib::decimalToStandardTime($value, 2, ':');
  719. //pr($is);
  720. $this->assertEquals('9:30', $is);
  721. }
  722. /**
  723. * TimeLibTest::testHasDaylightSavingTime()
  724. *
  725. * @return void
  726. */
  727. public function testHasDaylightSavingTime() {
  728. $timezone = 'Europe/Berlin';
  729. $x = TimeLib::hasDaylightSavingTime($timezone);
  730. $this->assertTrue($x);
  731. $timezone = 'Asia/Baghdad';
  732. $x = TimeLib::hasDaylightSavingTime($timezone);
  733. $this->assertFalse($x);
  734. }
  735. /**
  736. * TimeLibTest::testTimezone()
  737. *
  738. * @return void
  739. */
  740. public function testTimezone() {
  741. $timezone = TimeLib::timezone();
  742. // usually UTC
  743. $name = $timezone->getName();
  744. $this->debug($name);
  745. $this->assertTrue(!empty($name));
  746. $location = $timezone->getLocation();
  747. $this->debug($location);
  748. $this->assertTrue(!empty($location['country_code']));
  749. $this->assertTrue(isset($location['latitude']));
  750. $this->assertTrue(isset($location['longitude']));
  751. $offset = $timezone->getOffset(new DateTime('@' . mktime(0, 0, 0, 1, 1, date('Y'))));
  752. $this->debug($offset);
  753. $phpTimezone = date_default_timezone_get();
  754. $this->assertEquals($name, $phpTimezone);
  755. }
  756. }