TimeLibTest.php 18 KB

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