TimeLibTest.php 21 KB

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