TimeLibTest.php 18 KB

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