TimeLibTest.php 20 KB

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