TimeLibTest.php 25 KB

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