TimeLibTest.php 20 KB

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