TimeLibTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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::testAgeBounds()
  222. *
  223. * @return void
  224. */
  225. public function testAgeBounds() {
  226. $this->out($this->_header(__FUNCTION__), true);
  227. $values = array(
  228. array(20, 20, array('min' => '1990-07-07', 'max' => '1991-07-06')),
  229. array(10, 30, array('min' => '1980-07-07', 'max' => '2001-07-06')),
  230. array(11, 12, array('min' => '1998-07-07', 'max' => '2000-07-06'))
  231. );
  232. foreach ($values as $v) {
  233. //echo $v[0].'/'.$v[1];
  234. $ret = TimeLib::ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
  235. //pr($ret);
  236. if (isset($v[2])) {
  237. $this->assertSame($v[2], $ret);
  238. $this->assertEquals($v[0], TimeLib::age($v[2]['max'], '2011-07-06'));
  239. $this->assertEquals($v[1], TimeLib::age($v[2]['min'], '2011-07-06'));
  240. }
  241. }
  242. }
  243. /**
  244. * TimeLibTest::testAgeByYear()
  245. *
  246. * @return void
  247. */
  248. public function testAgeByYear() {
  249. $this->out($this->_header(__FUNCTION__), true);
  250. // year only
  251. $is = TimeLib::ageByYear(2000);
  252. $this->out($is);
  253. $this->assertEquals((date('Y') - 2001) . '/' . (date('Y') - 2000), $is);
  254. $is = TimeLib::ageByYear(1985);
  255. $this->assertEquals((date('Y') - 1986) . '/' . (date('Y') - 1985), $is);
  256. // with month
  257. if (($month = date('n') + 1) <= 12) {
  258. $is = TimeLib::ageByYear(2000, $month);
  259. $this->out($is);
  260. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  261. $this->assertSame(date('Y') - 2001, $is); //null, '2000/'.$month
  262. }
  263. if (($month = date('n') - 1) >= 1) {
  264. $is = TimeLib::ageByYear(2000, $month);
  265. $this->out($is);
  266. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  267. $this->assertSame(date('Y') - 2000, $is); //null, '2000/'.$month)
  268. }
  269. }
  270. /**
  271. * TimeLibTest::testDaysInMonth()
  272. *
  273. * @return void
  274. */
  275. public function testDaysInMonth() {
  276. $this->out($this->_header(__FUNCTION__), true);
  277. $ret = TimeLib::daysInMonth('2004', '3');
  278. $this->assertEquals(31, $ret);
  279. $ret = TimeLib::daysInMonth('2006', '4');
  280. $this->assertEquals(30, $ret);
  281. $ret = TimeLib::daysInMonth('2007', '2');
  282. $this->assertEquals(28, $ret);
  283. $ret = TimeLib::daysInMonth('2008', '2');
  284. $this->assertEquals(29, $ret);
  285. }
  286. /**
  287. * TimeLibTest::testDay()
  288. *
  289. * @return void
  290. */
  291. public function testDay() {
  292. $this->out($this->_header(__FUNCTION__), true);
  293. $ret = TimeLib::day('0');
  294. $this->assertEquals(__('Sunday'), $ret);
  295. $ret = TimeLib::day(2, true);
  296. $this->assertEquals(__('Tue'), $ret);
  297. $ret = TimeLib::day(6);
  298. $this->assertEquals(__('Saturday'), $ret);
  299. $ret = TimeLib::day(6, false, 1);
  300. $this->assertEquals(__('Sunday'), $ret);
  301. $ret = TimeLib::day(0, false, 2);
  302. $this->assertEquals(__('Tuesday'), $ret);
  303. $ret = TimeLib::day(1, false, 6);
  304. $this->assertEquals(__('Sunday'), $ret);
  305. }
  306. /**
  307. * TimeLibTest::testMonth()
  308. *
  309. * @return void
  310. */
  311. public function testMonth() {
  312. $this->out($this->_header(__FUNCTION__), true);
  313. $ret = TimeLib::month('11');
  314. $this->assertEquals(__('November'), $ret);
  315. $ret = TimeLib::month(1);
  316. $this->assertEquals(__('January'), $ret);
  317. $ret = TimeLib::month(2, true, array('appendDot' => true));
  318. $this->assertEquals(__('Feb') . '.', $ret);
  319. $ret = TimeLib::month(5, true, array('appendDot' => true));
  320. $this->assertEquals(__('May'), $ret);
  321. }
  322. /**
  323. * TimeLibTest::testDays()
  324. *
  325. * @return void
  326. */
  327. public function testDays() {
  328. $this->out($this->_header(__FUNCTION__), true);
  329. $ret = TimeLib::days();
  330. $this->assertTrue(count($ret) === 7);
  331. }
  332. /**
  333. * TimeLibTest::testMonths()
  334. *
  335. * @return void
  336. */
  337. public function testMonths() {
  338. $this->out($this->_header(__FUNCTION__), true);
  339. $ret = TimeLib::months();
  340. $this->assertTrue(count($ret) === 12);
  341. }
  342. /**
  343. * TimeLibTest::testRelLengthOfTime()
  344. *
  345. * @return void
  346. */
  347. public function testRelLengthOfTime() {
  348. $this->out($this->_header(__FUNCTION__), true);
  349. $ret = TimeLib::relLengthOfTime('1990-11-20');
  350. //pr($ret);
  351. $ret = TimeLib::relLengthOfTime('2012-11-20');
  352. //pr($ret);
  353. }
  354. /**
  355. * TimeLibTest::testLengthOfTime()
  356. *
  357. * @return void
  358. */
  359. public function testLengthOfTime() {
  360. $this->out($this->_header(__FUNCTION__), true);
  361. $ret = TimeLib::lengthOfTime(60);
  362. //pr($ret);
  363. // FIX ME! Doesn't work!
  364. $ret = TimeLib::lengthOfTime(-60);
  365. //pr($ret);
  366. $ret = TimeLib::lengthOfTime(-121);
  367. //pr($ret);
  368. }
  369. /**
  370. * TimeLibTest::testFuzzyFromOffset()
  371. *
  372. * @return void
  373. */
  374. public function testFuzzyFromOffset() {
  375. $this->out($this->_header(__FUNCTION__), true);
  376. $ret = TimeLib::fuzzyFromOffset(MONTH);
  377. //pr($ret);
  378. $ret = TimeLib::fuzzyFromOffset(120);
  379. //pr($ret);
  380. $ret = TimeLib::fuzzyFromOffset(DAY);
  381. //pr($ret);
  382. $ret = TimeLib::fuzzyFromOffset(DAY + 2 * MINUTE);
  383. //pr($ret);
  384. // FIX ME! Doesn't work!
  385. $ret = TimeLib::fuzzyFromOffset(-DAY);
  386. //pr($ret);
  387. }
  388. /**
  389. * TimeLibTest::testCweekMod()
  390. *
  391. * @return void
  392. */
  393. public function testCweekMod() {
  394. //$result = TimeLib::cWeekMod();
  395. }
  396. /**
  397. * TimeLibTest::testCweekDay()
  398. *
  399. * @return void
  400. */
  401. public function testCweekDay() {
  402. $this->out($this->_header(__FUNCTION__), true);
  403. // wednesday
  404. $ret = TimeLib::cweekDay(51, 2011, 2);
  405. $this->out('51, 2011, 2');
  406. $this->out(date(FORMAT_DB_DATETIME, $ret));
  407. $this->assertTrue($ret >= 1324422000 && $ret <= 1324425600);
  408. //$this->assertEquals(1324422000, $ret);
  409. }
  410. public function testCweeks() {
  411. $this->out($this->_header(__FUNCTION__), true);
  412. $ret = TimeLib::cweeks('2004');
  413. $this->assertEquals(53, $ret);
  414. $ret = TimeLib::cweeks('2010');
  415. $this->assertEquals(52, $ret);
  416. $ret = TimeLib::cweeks('2006');
  417. $this->assertEquals(52, $ret);
  418. $ret = TimeLib::cweeks('2007');
  419. $this->assertEquals(52, $ret);
  420. /*
  421. for ($i = 1990; $i < 2020; $i++) {
  422. $this->out(TimeLib::cweeks($i).BR;
  423. }
  424. */
  425. }
  426. public function testCweekBeginning() {
  427. $this->out($this->_header(__FUNCTION__), true);
  428. $values = array(
  429. '2001' => 978303600, # Mon 01.01.2001, 00:00
  430. '2006' => 1136156400, # Mon 02.01.2006, 00:00
  431. '2010' => 1262559600, # Mon 04.01.2010, 00:00
  432. '2013' => 1356908400, # Mon 31.12.2012, 00:00
  433. );
  434. foreach ($values as $year => $expected) {
  435. $ret = TimeLib::cweekBeginning($year);
  436. $this->out($ret);
  437. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  438. //$this->assertEquals($ret, $expected, null, $year);
  439. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  440. }
  441. $values = array(
  442. array('2001', '1', 978303600), # Mon 01.01.2001, 00:00:00
  443. array('2001', '2', 978908400), # Mon 08.01.2001, 00:00:00
  444. array('2001', '5', 980722800), # Mon 29.01.2001, 00:00:00
  445. array('2001', '52', 1009148400), # Mon 24.12.2001, 00:00:00
  446. array('2013', '11', 1362956400), # Mon 11.03.2013, 00:00:00
  447. array('2006', '3', 1137366000), # Mon 16.01.2006, 00:00:00
  448. );
  449. foreach ($values as $v) {
  450. $ret = TimeLib::cweekBeginning($v[0], $v[1]);
  451. $this->out($ret);
  452. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  453. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  454. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  455. }
  456. }
  457. public function testCweekEnding() {
  458. $this->out($this->_header(__FUNCTION__), true);
  459. $values = array(
  460. '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
  461. '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
  462. '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
  463. '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
  464. );
  465. foreach ($values as $year => $expected) {
  466. $ret = TimeLib::cweekEnding($year);
  467. $this->out($ret);
  468. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  469. //$this->assertSame($expected, $ret);
  470. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  471. }
  472. $values = array(
  473. array('2001', '1', 978908399), # Sun 07.01.2001, 23:59:59
  474. array('2001', '2', 979513199), # Sun 14.01.2001, 23:59:59
  475. array('2001', '5', 981327599), # Sun 04.02.2001, 23:59:59
  476. array('2001', '52', 1009753199), # Sun 30.12.2001, 23:59:59
  477. array('2013', '11', 1363561199), # Sun 17.03.2013, 23:59:59
  478. array('2006', '3', 1137970799), # Sun 22.01.2006, 23:59:59
  479. );
  480. foreach ($values as $v) {
  481. $ret = TimeLib::cweekEnding($v[0], $v[1]);
  482. $this->out($ret);
  483. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  484. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  485. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  486. }
  487. }
  488. /**
  489. * TimeLibTest::testAgeByHoroscop()
  490. *
  491. * @return void
  492. */
  493. public function testAgeByHoroscop() {
  494. App::uses('ZodiacLib', 'Tools.Misc');
  495. $this->skipIf(php_sapi_name() === 'cli', 'Fix these tests');
  496. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
  497. // between xxxx-08-24 and xxxx-09-23 the latter, otherwise the first:
  498. $this->assertEquals(date('Y') - 2000 - 1, $is);
  499. $this->assertEquals(array(date('Y') - 2000 - 1, date('Y') - 2000), $is);
  500. $is = TimeLib::ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
  501. //pr($is);
  502. $this->assertEquals(date('Y') - 1991 - 1, $is);
  503. $is = TimeLib::ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
  504. //pr($is);
  505. $this->assertEquals(array(date('Y') - 1986 - 1, date('Y') - 1986), $is);
  506. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
  507. //debug($is);
  508. $this->assertEquals(date('Y') - 2000 - 1, $is); //array(10, 11)
  509. }
  510. /**
  511. * TimeLibTest::testAgeRange()
  512. *
  513. * @return void
  514. */
  515. public function testAgeRange() {
  516. $is = TimeLib::ageRange(2000);
  517. $this->assertEquals(date('Y') - 2000 - 1, $is);
  518. $is = TimeLib::ageRange(date('Y') - 11, null, null, 5);
  519. $this->assertEquals(array(6, 10), $is);
  520. $is = TimeLib::ageRange(date('Y') - 13, null, null, 5);
  521. $this->assertEquals(array(11, 15), $is);
  522. $is = TimeLib::ageRange(1985, 23, 11);
  523. $this->assertEquals(date('Y') - 1985 - 1, $is);
  524. $is = TimeLib::ageRange(date('Y') - 29, null, null, 6);
  525. $this->assertEquals(array(25, 30), $is);
  526. $is = TimeLib::ageRange(date('Y') - 29, 21, 11, 7);
  527. $this->assertEquals(array(22, 28), $is);
  528. }
  529. /**
  530. * TimeLibTest::testParseDate()
  531. *
  532. * @return void
  533. */
  534. public function testParseDate() {
  535. //echo $this->_header(__FUNCTION__);
  536. $tests = array(
  537. '2010-12-11' => 1292022000,
  538. '2010-01-02' => 1262386800,
  539. '10-01-02' => 1262386800,
  540. '2.1.2010' => 1262386800,
  541. '2.1.10' => 1262386800,
  542. '02.01.10' => 1262386800,
  543. '02.01.2010' => 1262386800,
  544. '02.01.2010 22:11' => 1262386800,
  545. '2010-01-02 22:11' => 1262386800,
  546. );
  547. foreach ($tests as $was => $expected) {
  548. $is = TimeLib::parseDate($was);
  549. $this->assertTrue($is <= $expected + HOUR && $is >= $expected);
  550. }
  551. }
  552. /**
  553. * TimeLibTest::testParseTime()
  554. *
  555. * @return void
  556. */
  557. public function testParseTime() {
  558. //echo $this->_header(__FUNCTION__);
  559. $tests = array(
  560. '2:4' => 7440,
  561. '2:04' => 7440,
  562. '2' => 7200,
  563. '1,5' => 3600 + 1800,
  564. '1.5' => 3600 + 1800,
  565. '1.50' => 3600 + 1800,
  566. '1.01' => 3660,
  567. ':4' => 240,
  568. ':04' => 240,
  569. ':40' => 40 * MINUTE,
  570. '1:2:4' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  571. '01:2:04' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  572. '0:2:04' => 2 * MINUTE + 4 * SECOND,
  573. '::4' => 4 * SECOND,
  574. '::04' => 4 * SECOND,
  575. '::40' => 40 * SECOND,
  576. '2011-11-12 10:10:10' => 10 * HOUR + 10 * MINUTE + 10 * SECOND,
  577. );
  578. // positive
  579. foreach ($tests as $was => $expected) {
  580. $is = TimeLib::parseTime($was);
  581. //pr($is);
  582. $this->assertEquals($expected, $is);
  583. }
  584. unset($tests['2011-11-12 10:10:10']);
  585. // negative
  586. foreach ($tests as $was => $expected) {
  587. $is = TimeLib::parseTime('-' . $was);
  588. //pr($is);
  589. $this->assertEquals(-$expected, $is);
  590. }
  591. }
  592. /**
  593. * TimeLibTest::testBuildTime()
  594. *
  595. * @return void
  596. */
  597. public function testBuildTime() {
  598. //echo $this->_header(__FUNCTION__);
  599. $tests = array(
  600. 7440 => '2:04',
  601. 7220 => '2:00', # 02:00:20 => rounded to 2:00:00
  602. 5400 => '1:30',
  603. 3660 => '1:01',
  604. );
  605. // positive
  606. foreach ($tests as $was => $expected) {
  607. $is = TimeLib::buildTime($was);
  608. //pr($is);
  609. $this->assertEquals($expected, $is);
  610. }
  611. // negative
  612. foreach ($tests as $was => $expected) {
  613. $is = TimeLib::buildTime(-$was);
  614. //pr($is);
  615. $this->assertEquals('-' . $expected, $is);
  616. }
  617. }
  618. /**
  619. * TimeLibTest::testBuildDefaultTime()
  620. *
  621. * @return void
  622. */
  623. public function testBuildDefaultTime() {
  624. //echo $this->_header(__FUNCTION__);
  625. $tests = array(
  626. 7440 => '02:04:00',
  627. 7220 => '02:00:20',
  628. 5400 => '01:30:00',
  629. 3660 => '01:01:00',
  630. 1 * HOUR + 2 * MINUTE + 4 * SECOND => '01:02:04',
  631. );
  632. foreach ($tests as $was => $expected) {
  633. $is = TimeLib::buildDefaultTime($was);
  634. //pr($is);
  635. $this->assertEquals($expected, $is);
  636. }
  637. }
  638. /**
  639. * 9.30 => 9.50
  640. *
  641. * @return void
  642. */
  643. public function testStandardDecimal() {
  644. //echo $this->_header(__FUNCTION__);
  645. $value = '9.30';
  646. $is = TimeLib::standardToDecimalTime($value);
  647. $this->assertEquals('9.50', round($is, 2));
  648. $value = '9.3';
  649. $is = TimeLib::standardToDecimalTime($value);
  650. $this->assertEquals('9.50', round($is, 2));
  651. }
  652. /**
  653. * 9.50 => 9.30
  654. *
  655. * @return void
  656. */
  657. public function testDecimalStandard() {
  658. //echo $this->_header(__FUNCTION__);
  659. $value = '9.50';
  660. $is = TimeLib::decimalToStandardTime($value);
  661. $this->assertEquals('9.3', round($is, 2));
  662. $value = '9.5';
  663. $is = TimeLib::decimalToStandardTime($value);
  664. //pr($is);
  665. $this->assertEquals('9.3', $is);
  666. $is = TimeLib::decimalToStandardTime($value, 2);
  667. //pr($is);
  668. $this->assertEquals('9.30', $is);
  669. $is = TimeLib::decimalToStandardTime($value, 2, ':');
  670. //pr($is);
  671. $this->assertEquals('9:30', $is);
  672. }
  673. /**
  674. * TimeLibTest::testHasDaylightSavingTime()
  675. *
  676. * @return void
  677. */
  678. public function testHasDaylightSavingTime() {
  679. $timezone = 'Europe/Berlin';
  680. $x = TimeLib::hasDaylightSavingTime($timezone);
  681. $this->assertTrue($x);
  682. $timezone = 'Asia/Baghdad';
  683. $x = TimeLib::hasDaylightSavingTime($timezone);
  684. $this->assertFalse($x);
  685. }
  686. /**
  687. * TimeLibTest::testTimezone()
  688. *
  689. * @return void
  690. */
  691. public function testTimezone() {
  692. $timezone = TimeLib::timezone();
  693. // usually UTC
  694. $name = $timezone->getName();
  695. $this->debug($name);
  696. $this->assertTrue(!empty($name));
  697. $location = $timezone->getLocation();
  698. $this->debug($location);
  699. $this->assertTrue(!empty($location['country_code']));
  700. $this->assertTrue(isset($location['latitude']));
  701. $this->assertTrue(isset($location['longitude']));
  702. $offset = $timezone->getOffset(new DateTime('@' . mktime(0, 0, 0, 1, 1, date('Y'))));
  703. $this->debug($offset);
  704. $phpTimezone = date_default_timezone_get();
  705. $this->assertEquals($name, $phpTimezone);
  706. }
  707. }