TimeLibTest.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. App::uses('TimeLib', 'Tools.Utility');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class TimeLibTest extends MyCakeTestCase {
  5. public $Time = null;
  6. /**
  7. * TimeLibTest::testObject()
  8. *
  9. * @return void
  10. */
  11. public function testObject() {
  12. $this->Time = new TimeLib();
  13. $this->assertTrue(is_object($this->Time));
  14. $this->assertInstanceOf('TimeLib', $this->Time);
  15. Configure::write('Config.language', 'deu');
  16. }
  17. /**
  18. * Currently only works with timezoned localized values, not with UTC!!!
  19. *
  20. * @return void
  21. */
  22. public function testIncrementDate() {
  23. $timezone = Configure::read('Config.timezone');
  24. //$timezone = Datetime::timezone();
  25. Configure::write('Config.timezone', 'Europe/Berlin');
  26. $phpTimezone = date_default_timezone_get();
  27. date_default_timezone_set('Europe/Berlin');
  28. $from = '2012-12-31';
  29. $Date = TimeLib::incrementDate($from, 0, 0);
  30. $this->assertSame($from, $Date->format(FORMAT_DB_DATE));
  31. $from = '2012-12-31';
  32. $Date = TimeLib::incrementDate($from, 0, 1);
  33. $this->assertSame('2013-01-31', $Date->format(FORMAT_DB_DATE));
  34. $from = '2012-12-31';
  35. $Date = TimeLib::incrementDate($from, 0, 2);
  36. $this->assertSame('2013-02-28', $Date->format(FORMAT_DB_DATE));
  37. $from = '2012-12-31';
  38. $Date = TimeLib::incrementDate($from, 0, 4);
  39. $this->assertSame('2013-04-30', $Date->format(FORMAT_DB_DATE));
  40. $from = '2012-12-31';
  41. $Date = TimeLib::incrementDate($from, 1, 0);
  42. $this->assertSame('2013-12-31', $Date->format(FORMAT_DB_DATE));
  43. // from leap year
  44. $from = '2008-02-29';
  45. $Date = TimeLib::incrementDate($from, 1, 0);
  46. $this->assertSame('2009-02-28', $Date->format(FORMAT_DB_DATE));
  47. // into leap year
  48. $from = '2007-02-28';
  49. $Date = TimeLib::incrementDate($from, 1, 0);
  50. $this->assertSame('2008-02-29', $Date->format(FORMAT_DB_DATE));
  51. // other direction
  52. $from = '2012-12-31';
  53. $Date = TimeLib::incrementDate($from, 0, -1);
  54. $this->assertSame('2012-11-30', $Date->format(FORMAT_DB_DATE));
  55. $from = '2012-12-31';
  56. $Date = TimeLib::incrementDate($from, -1, -1);
  57. $this->assertSame('2011-11-30', $Date->format(FORMAT_DB_DATE));
  58. // including days
  59. $from = '2012-12-31';
  60. $Date = TimeLib::incrementDate($from, 0, 1, 1);
  61. $this->assertSame('2013-02-01', $Date->format(FORMAT_DB_DATE));
  62. // including days
  63. $from = '2012-12-31';
  64. $Date = TimeLib::incrementDate($from, 0, 1, 5);
  65. $this->assertSame('2013-02-05', $Date->format(FORMAT_DB_DATE));
  66. Configure::write('Config.timezone', $timezone);
  67. date_default_timezone_set($phpTimezone);
  68. }
  69. /**
  70. * TimeLibTest::testNiceDate()
  71. *
  72. * @return void
  73. */
  74. public function testNiceDate() {
  75. $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
  76. //$this->assertTrue(!empty($res));
  77. $values = [
  78. ['2009-12-01 00:00:00', FORMAT_NICE_YMD, '01.12.2009'],
  79. ['2009-12-01 00:00:00', FORMAT_NICE_M_FULL, 'December'],
  80. ];
  81. foreach ($values as $v) {
  82. $result = TimeLib::niceDate($v[0], $v[1]);
  83. $this->assertEquals($v[2], $result);
  84. }
  85. $date = '2009-12-01 00:00:00';
  86. $format = FORMAT_NICE_YMD;
  87. $result = TimeLib::niceDate($date, $format, ['oclock' => true]);
  88. $expected = '01.12.2009';
  89. $this->assertEquals($expected, $result);
  90. $date = '2009-12-01 00:00:00';
  91. $format = FORMAT_NICE_YMDHM;
  92. $result = TimeLib::niceDate($date, $format, ['oclock' => true]);
  93. $expected = '01.12.2009, 00:00 ' . __d('tools', 'o\'clock');
  94. $this->assertEquals($expected, $result);
  95. }
  96. /**
  97. * TimeLibTest::testNiceTime()
  98. *
  99. * @return void
  100. */
  101. public function testNiceTime() {
  102. $result = TimeLib::niceTime('22:11:18');
  103. $expected = '22:11';
  104. $this->assertEquals($expected, $result);
  105. $result = TimeLib::niceTime('2014-11-12 22:11:18');
  106. $this->assertEquals($expected, $result);
  107. }
  108. /**
  109. * Test that input as date only (YYYY-MM-DD) does not suddendly return a
  110. * different date on output due to timezone differences.
  111. * Here the timezone should not apply since we only input date and only output
  112. * date (time itself is irrelevant).
  113. *
  114. * @return void
  115. */
  116. public function testDateWithTimezone() {
  117. $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
  118. //$this->assertTrue(!empty($res));
  119. Configure::write('Config.timezone', 'America/Anchorage');
  120. $ret = TimeLib::niceDate('2009-12-01');
  121. //debug($ret);
  122. $this->assertEquals('01.12.2009', $ret);
  123. $ret = TimeLib::localDate('2009-12-01');
  124. //debug($ret);
  125. $this->assertEquals('01.12.2009', $ret);
  126. }
  127. /**
  128. * TimeLibTest::testParseLocalizedDate()
  129. *
  130. * @return void
  131. */
  132. public function testParseLocalizedDate() {
  133. $ret = TimeLib::parseLocalizedDate('15-Feb-2009', 'j-M-Y', 'start');
  134. //$this->debug($ret);
  135. $this->assertEquals('2009-02-15 00:00:00', $ret);
  136. // problem when not passing months or days as well - no way of knowing how exact the date was
  137. $ret = TimeLib::parseLocalizedDate('2009', 'Y', 'start');
  138. //pr($ret);
  139. //$this->assertEquals($ret, '2009-01-01 00:00:00');
  140. $ret = TimeLib::parseLocalizedDate('Feb 2009', 'M Y', 'start');
  141. //pr($ret);
  142. //$this->assertEquals($ret, '2009-02-01 00:00:00');
  143. $values = [
  144. [__d('tools', 'Today'), [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')))]],
  145. ['2010', ['2010-01-01 00:00:00', '2010-12-31 23:59:59']],
  146. ['23.02.2011', ['2011-02-23 00:00:00', '2011-02-23 23:59:59']],
  147. ['22/02/2011', ['2011-02-22 00:00:00', '2011-02-22 23:59:59']],
  148. ['3/2/11', ['2011-02-03 00:00:00', '2011-02-03 23:59:59']],
  149. //array('2/12/9', array('2009-12-02 00:00:00', '2009-12-02 23:59:59')),
  150. //array('12/2009', array('2009-12-01 00:00:00', '2009-12-31 23:59:59')),
  151. ];
  152. foreach ($values as $v) {
  153. $ret = TimeLib::parseLocalizedDate($v[0], null, 'start');
  154. //pr($ret);
  155. $this->assertEquals($v[1][0], $ret);
  156. $ret = TimeLib::parseLocalizedDate($v[0], null, 'end');
  157. //pr($ret);
  158. $this->assertEquals($v[1][1], $ret);
  159. }
  160. }
  161. /**
  162. * TimeLibTest::testLocalDate()
  163. *
  164. * @return void
  165. */
  166. public function testLocalDate() {
  167. $this->skipIf(php_sapi_name() === 'cli', 'for now');
  168. $res = setlocale(LC_TIME, ['de_DE.UTF-8', 'deu_deu']);
  169. $this->assertTrue(!empty($res));
  170. $values = [
  171. ['2009-12-01 00:00:00', FORMAT_LOCAL_YMD, '01.12.2009'],
  172. ['2009-12-01 00:00:00', FORMAT_LOCAL_M_FULL, 'Dezember'],
  173. ];
  174. foreach ($values as $v) {
  175. $ret = TimeLib::localDate($v[0], $v[1]);
  176. //$this->debug($ret);
  177. $this->assertEquals($v[2], $ret);
  178. }
  179. $date = '2009-12-01 00:00:00';
  180. $format = FORMAT_LOCAL_YMD;
  181. $result = TimeLib::localDate($date, $format, ['oclock' => true]);
  182. $expected = '01.12.2009';
  183. $this->assertEquals($expected, $result);
  184. $date = '2009-12-01 00:00:00';
  185. $format = FORMAT_LOCAL_YMDHM;
  186. $result = TimeLib::localDate($date, $format, ['oclock' => true]);
  187. $expected = '01.12.2009, 00:00 ' . __d('tools', 'o\'clock');
  188. $this->assertEquals($expected, $result);
  189. }
  190. /**
  191. * TimeLibTest::testPeriod()
  192. *
  193. * @return void
  194. */
  195. public function testPeriod() {
  196. //$this->out($this->_header(__FUNCTION__), true);
  197. $values = [
  198. [__d('tools', 'Today'), [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')))]],
  199. ['2010', ['2010-01-01 00:00:00', '2010-12-31 23:59:59']],
  200. ['2011-02', ['2011-02-01 00:00:00', '2011-02-28 23:59:59']],
  201. ['2012-02', ['2012-02-01 00:00:00', '2012-02-29 23:59:59']],
  202. ['2010-02-23', ['2010-02-23 00:00:00', '2010-02-23 23:59:59']],
  203. ['2010-02-23 bis 2010-02-26', ['2010-02-23 00:00:00', '2010-02-26 23:59:59']],
  204. //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')),
  205. // localized
  206. ['23.02.2011', ['2011-02-23 00:00:00', '2011-02-23 23:59:59']],
  207. ['23.2.2010 bis 26.2.2011', ['2010-02-23 00:00:00', '2011-02-26 23:59:59']],
  208. ];
  209. foreach ($values as $v) {
  210. $ret = TimeLib::period($v[0]);
  211. //pr($ret);
  212. $this->assertEquals($v[1], $ret);
  213. }
  214. }
  215. /**
  216. * TimeLibTest::testPeriodAsSql()
  217. *
  218. * @return void
  219. */
  220. public function testPeriodAsSql() {
  221. //$this->out($this->_header(__FUNCTION__), true);
  222. $values = [
  223. [__d('tools', 'Today'), "(Model.field >= '" . date(FORMAT_DB_DATE) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"],
  224. [__d('tools', 'Yesterday') . ' ' . __d('tools', 'until') . ' ' . __d('tools', 'Today'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"],
  225. [__d('tools', 'Today') . ' ' . __d('tools', 'until') . ' ' . __d('tools', 'Tomorrow'), "(Model.field >= '" . date(FORMAT_DB_DATE, time()) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE, time() + DAY) . " 23:59:59')"],
  226. [__d('tools', 'Yesterday') . ' ' . __d('tools', 'until') . ' ' . __d('tools', 'Tomorrow'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE, time() + DAY) . " 23:59:59')"],
  227. ];
  228. foreach ($values as $v) {
  229. $ret = TimeLib::periodAsSql($v[0], 'Model.field');
  230. //pr($v[1]);
  231. //pr($ret);
  232. $this->assertSame($v[1], $ret);
  233. }
  234. }
  235. /**
  236. * TimeLibTest::testDifference()
  237. *
  238. * @return void
  239. */
  240. public function testDifference() {
  241. //$this->out($this->_header(__FUNCTION__), true);
  242. $values = [
  243. ['2010-02-23 11:11:11', '2010-02-23 11:12:01', 50],
  244. ['2010-02-23 11:11:11', '2010-02-24 11:12:01', DAY + 50]
  245. ];
  246. foreach ($values as $v) {
  247. $ret = TimeLib::difference($v[0], $v[1]);
  248. $this->assertEquals($v[2], $ret);
  249. }
  250. }
  251. /**
  252. * TimeLibTest::testIsLeapYear()
  253. *
  254. * @return void
  255. */
  256. public function testIsLeapYear() {
  257. $is = TimeLib::isLeapYear('2000');
  258. $this->assertTrue($is);
  259. $is = TimeLib::isLeapYear('2001');
  260. $this->assertFalse($is);
  261. }
  262. /**
  263. * TimeLibTest::testIsInRange()
  264. *
  265. * @return void
  266. */
  267. public function testIsInRange() {
  268. $is = TimeLib::isInRange(date(FORMAT_DB_DATETIME, time() + 22 * HOUR), DAY);
  269. $this->assertTrue($is);
  270. $is = TimeLib::isInRange(date(FORMAT_DB_DATETIME, time() + 26 * HOUR), DAY);
  271. $this->assertFalse($is);
  272. $day = date(FORMAT_DB_DATETIME, time() + 10 * DAY);
  273. $this->assertTrue(TimeLib::isInRange($day, 11 * DAY));
  274. $this->assertTrue(TimeLib::isInRange($day, 10 * DAY));
  275. $this->assertFalse(TimeLib::isInRange($day, 9 * DAY));
  276. $day = date(FORMAT_DB_DATETIME, time() - 78 * DAY);
  277. $this->assertTrue(TimeLib::isInRange($day, 79 * DAY));
  278. $this->assertTrue(TimeLib::isInRange($day, 78 * DAY));
  279. $this->assertFalse(TimeLib::isInRange($day, 77 * DAY));
  280. }
  281. /**
  282. * Test age
  283. *
  284. * @return void
  285. */
  286. public function testAge() {
  287. $this->assertEquals('0', TimeLib::age(null));
  288. list($year, $month, $day) = explode('-', date('Y-m-d'));
  289. $this->assertEquals('0', TimeLib::age($year . '-' . $month . '-' . $day, null));
  290. list($year, $month, $day) = explode('-', date('Y-m-d', strtotime('-10 years')));
  291. $this->assertEquals('10', TimeLib::age($year . '-' . $month . '-' . $day, null));
  292. list($year, $month, $day) = explode('-', date('Y-m-d', strtotime('-10 years +1 day')));
  293. $this->assertEquals('9', TimeLib::age($year . '-' . $month . '-' . $day, null));
  294. list($year, $month, $day) = explode('-', date('Y-m-d', strtotime('-10 years -1 day')));
  295. $this->assertEquals('10', TimeLib::age($year . '-' . $month . '-' . $day, null));
  296. // Crossing years
  297. $this->assertEquals('2', TimeLib::age('2005-12-01', '2008-02-29'));
  298. $this->assertEquals('6', TimeLib::age('2002-01-29', '2008-12-02'));
  299. // Leap year
  300. // These tests might fail on windows / some strange setup?
  301. $this->assertEquals('2', TimeLib::age('2005-03-01', '2008-02-28'));
  302. $this->assertEquals('2', TimeLib::age('2005-03-01', '2008-02-29'));
  303. $this->assertEquals('3', TimeLib::age('2005-03-01', '2008-03-01'));
  304. $this->assertEquals('3', TimeLib::age('2005-02-29', '2008-03-01'));
  305. // Future
  306. list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years -1 day')));
  307. $this->assertEquals('9', TimeLib::age(null, $yearE . '-' . $monthE . '-' . $dayE));
  308. list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years +1 day')));
  309. $this->assertEquals('10', TimeLib::age(null, $yearE . '-' . $monthE . '-' . $dayE));
  310. $birthday = '2033-04-09';
  311. $this->assertEquals(-1, TimeLib::age($birthday));
  312. $birthday = '1985-04-08';
  313. $relativeDate = '2010-04-07';
  314. $this->assertEquals('24', TimeLib::age($birthday, $relativeDate));
  315. $relativeDate = '2010-04-08';
  316. $this->assertEquals('25', TimeLib::age($birthday, $relativeDate));
  317. $relativeDate = '2010-04-09';
  318. $this->assertEquals('25', TimeLib::age($birthday, $relativeDate));
  319. }
  320. /**
  321. * TimeLibTest::testAgeBounds()
  322. *
  323. * @return void
  324. */
  325. public function testAgeBounds() {
  326. //$this->out($this->_header(__FUNCTION__), true);
  327. $values = [
  328. [20, 20, ['min' => '1990-07-07', 'max' => '1991-07-06']],
  329. [10, 30, ['min' => '1980-07-07', 'max' => '2001-07-06']],
  330. [11, 12, ['min' => '1998-07-07', 'max' => '2000-07-06']]
  331. ];
  332. foreach ($values as $v) {
  333. //echo $v[0].'/'.$v[1];
  334. $ret = TimeLib::ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
  335. //pr($ret);
  336. if (isset($v[2])) {
  337. $this->assertSame($v[2], $ret);
  338. $this->assertEquals($v[0], TimeLib::age($v[2]['max'], '2011-07-06'));
  339. $this->assertEquals($v[1], TimeLib::age($v[2]['min'], '2011-07-06'));
  340. }
  341. }
  342. }
  343. /**
  344. * TimeLibTest::testAgeByYear()
  345. *
  346. * @return void
  347. */
  348. public function testAgeByYear() {
  349. //$this->out($this->_header(__FUNCTION__), true);
  350. // year only
  351. $is = TimeLib::ageByYear(2000);
  352. //$this->out($is);
  353. if (date('n') == 1 && date('j') == 1) {
  354. $this->assertEquals(date('Y') - 2000, $is);
  355. } else {
  356. $this->assertEquals((date('Y') - 2001) . '/' . (date('Y') - 2000), $is);
  357. }
  358. $is = TimeLib::ageByYear(1985);
  359. $this->assertEquals((date('Y') - 1986) . '/' . (date('Y') - 1985), $is);
  360. // with month
  361. if (($month = date('n') + 1) <= 12) {
  362. $is = TimeLib::ageByYear(2000, $month);
  363. $this->out($is);
  364. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  365. $this->assertSame(date('Y') - 2001, $is); //null, '2000/'.$month
  366. }
  367. if (($month = date('n') - 1) >= 1) {
  368. $is = TimeLib::ageByYear(2000, $month);
  369. $this->out($is);
  370. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  371. $this->assertSame(date('Y') - 2000, $is); //null, '2000/'.$month)
  372. }
  373. }
  374. /**
  375. * TimeLibTest::testDaysInMonth()
  376. *
  377. * @return void
  378. */
  379. public function testDaysInMonth() {
  380. //$this->out($this->_header(__FUNCTION__), true);
  381. $ret = TimeLib::daysInMonth('2004', '3');
  382. $this->assertEquals(31, $ret);
  383. $ret = TimeLib::daysInMonth('2006', '4');
  384. $this->assertEquals(30, $ret);
  385. $ret = TimeLib::daysInMonth('2007', '2');
  386. $this->assertEquals(28, $ret);
  387. $ret = TimeLib::daysInMonth('2008', '2');
  388. $this->assertEquals(29, $ret);
  389. }
  390. /**
  391. * TimeLibTest::testDay()
  392. *
  393. * @return void
  394. */
  395. public function testDay() {
  396. //$this->out($this->_header(__FUNCTION__), true);
  397. $ret = TimeLib::day('0');
  398. $this->assertEquals(__d('tools', 'Sunday'), $ret);
  399. $ret = TimeLib::day(2, true);
  400. $this->assertEquals(__d('tools', 'Tue'), $ret);
  401. $ret = TimeLib::day(6);
  402. $this->assertEquals(__d('tools', 'Saturday'), $ret);
  403. $ret = TimeLib::day(6, false, 1);
  404. $this->assertEquals(__d('tools', 'Sunday'), $ret);
  405. $ret = TimeLib::day(0, false, 2);
  406. $this->assertEquals(__d('tools', 'Tuesday'), $ret);
  407. $ret = TimeLib::day(1, false, 6);
  408. $this->assertEquals(__d('tools', 'Sunday'), $ret);
  409. }
  410. /**
  411. * TimeLibTest::testMonth()
  412. *
  413. * @return void
  414. */
  415. public function testMonth() {
  416. //$this->out($this->_header(__FUNCTION__), true);
  417. $ret = TimeLib::month('11');
  418. $this->assertEquals(__d('tools', 'November'), $ret);
  419. $ret = TimeLib::month(1);
  420. $this->assertEquals(__d('tools', 'January'), $ret);
  421. $ret = TimeLib::month(2, true, ['appendDot' => true]);
  422. $this->assertEquals(__d('tools', 'Feb') . '.', $ret);
  423. $ret = TimeLib::month(5, true, ['appendDot' => true]);
  424. $this->assertEquals(__d('tools', 'May'), $ret);
  425. }
  426. /**
  427. * TimeLibTest::testDays()
  428. *
  429. * @return void
  430. */
  431. public function testDays() {
  432. //$this->out($this->_header(__FUNCTION__), true);
  433. $ret = TimeLib::days();
  434. $this->assertTrue(count($ret) === 7);
  435. }
  436. /**
  437. * TimeLibTest::testMonths()
  438. *
  439. * @return void
  440. */
  441. public function testMonths() {
  442. //$this->out($this->_header(__FUNCTION__), true);
  443. $ret = TimeLib::months();
  444. $this->assertTrue(count($ret) === 12);
  445. }
  446. /**
  447. * TimeLibTest::testRelLengthOfTime()
  448. *
  449. * @return void
  450. */
  451. public function testRelLengthOfTime() {
  452. $ret = TimeLib::relLengthOfTime('1990-11-20');
  453. //pr($ret);
  454. $ret = TimeLib::relLengthOfTime('2012-11-20');
  455. //pr($ret);
  456. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
  457. //pr($res);
  458. $this->assertTrue(!empty($res));
  459. $this->skipIf(__d('tools', 'Days') !== 'Tage', 'Needs German po file.');
  460. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR), null, ['plural' => 'n']);
  461. //pr($res);
  462. //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__d('tools', 'Hours'));
  463. $this->assertEquals(__d('tools', '%s ago', '4 ' . __d('tools', 'Days') . 'n' . ', ' . '5 ' . __d('tools', 'Hours')), $res);
  464. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() + 4 * DAY + 5 * HOUR), null, ['plural' => 'n']);
  465. //pr($res);
  466. $this->assertEquals(__d('tools', 'In %s', '4 ' . __d('tools', 'Days') . 'n' . ', ' . '5 ' . __d('tools', 'Hours')), $res);
  467. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, ['plural' => 'n']);
  468. //pr($res);
  469. $this->assertEquals($res, __d('tools', 'justNow'));
  470. }
  471. /**
  472. * Test cweek
  473. *
  474. * @return void
  475. */
  476. public function testCweek() {
  477. $year = 2008;
  478. $month = 12;
  479. $day = 29;
  480. $date = mktime(0, 0, 0, $month, $day, $year);
  481. $this->assertEquals('01/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  482. $year = 2009;
  483. $month = 1;
  484. $day = 1;
  485. $date = mktime(0, 0, 0, $month, $day, $year);
  486. $this->assertEquals('01/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  487. $year = 2009;
  488. $month = 1;
  489. $day = 9;
  490. $date = mktime(0, 0, 0, $month, $day, $year);
  491. $this->assertEquals('02/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day . ' 00:00:00'));
  492. $year = 2009;
  493. $month = 12;
  494. $day = 26;
  495. $date = mktime(0, 0, 0, $month, $day, $year);
  496. $this->assertEquals('52/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  497. }
  498. /**
  499. * Test IsInTheFuture
  500. *
  501. * @return void
  502. */
  503. public function testIsInTheFuture() {
  504. $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
  505. $is = TimeLib::isInTheFuture($testDate);
  506. $this->assertTrue($is);
  507. $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
  508. $is = TimeLib::isInTheFuture($testDate);
  509. $this->assertFalse($is);
  510. }
  511. /**
  512. * Test IsNotTodayAndInTheFuture
  513. *
  514. * @return void
  515. */
  516. public function testIsNotTodayAndInTheFuture() {
  517. $testDate = date(FORMAT_DB_DATE, time());
  518. $is = TimeLib::isNotTodayAndInTheFuture($testDate);
  519. $this->assertFalse($is);
  520. $testDate = date(FORMAT_DB_DATETIME, time() + 1 * DAY);
  521. $is = TimeLib::isNotTodayAndInTheFuture($testDate);
  522. $this->assertTrue($is);
  523. }
  524. /**
  525. * Test IsDayAfterTomorrow
  526. *
  527. * @return void
  528. */
  529. public function testIsDayAfterTomorrow() {
  530. $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
  531. $is = TimeLib::isDayAfterTomorrow($testDate);
  532. $this->assertTrue($is);
  533. $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
  534. $is = TimeLib::isDayAfterTomorrow($testDate);
  535. $this->assertFalse($is);
  536. }
  537. /**
  538. * TimeLibTest::testLengthOfTime()
  539. *
  540. * @return void
  541. */
  542. public function testLengthOfTime() {
  543. //$this->out($this->_header(__FUNCTION__), true);
  544. $ret = TimeLib::lengthOfTime(60);
  545. //pr($ret);
  546. // FIX ME! Doesn't work!
  547. $ret = TimeLib::lengthOfTime(-60);
  548. //pr($ret);
  549. $ret = TimeLib::lengthOfTime(-121);
  550. //pr($ret);
  551. $this->assertEquals('6 ' . __d('tools', 'Minutes') . ', 40 ' . __d('tools', 'Seconds'), TimeLib::lengthOfTime(400));
  552. $res = TimeLib::lengthOfTime(400, 'i');
  553. //pr($res);
  554. $this->assertEquals('6 ' . __d('tools', 'Minutes'), $res);
  555. $res = TimeLib::lengthOfTime(6 * DAY);
  556. //pr($res);
  557. $this->assertEquals('6 ' . __d('tools', 'Days') . ', 0 ' . __d('tools', 'Hours'), $res);
  558. }
  559. /**
  560. * TimeLibTest::testFuzzyFromOffset()
  561. *
  562. * @return void
  563. */
  564. public function testFuzzyFromOffset() {
  565. //$this->out($this->_header(__FUNCTION__), true);
  566. $ret = TimeLib::fuzzyFromOffset(MONTH);
  567. //pr($ret);
  568. $ret = TimeLib::fuzzyFromOffset(120);
  569. //pr($ret);
  570. $ret = TimeLib::fuzzyFromOffset(DAY);
  571. //pr($ret);
  572. $ret = TimeLib::fuzzyFromOffset(DAY + 2 * MINUTE);
  573. //pr($ret);
  574. // FIX ME! Doesn't work!
  575. $ret = TimeLib::fuzzyFromOffset(-DAY);
  576. //pr($ret);
  577. }
  578. /**
  579. * TimeLibTest::testCweekMod()
  580. *
  581. * @return void
  582. */
  583. public function testCweekMod() {
  584. $result = TimeLib::cWeekMod(0);
  585. $this->assertEquals(0, $result);
  586. $result = TimeLib::cWeekMod(1);
  587. $this->assertEquals(1, $result);
  588. $result = TimeLib::cWeekMod(6);
  589. $this->assertEquals(0, $result);
  590. }
  591. /**
  592. * TimeLibTest::testTimezoneByCoordinates()
  593. *
  594. * @return void
  595. */
  596. public function testTimezoneByCoordinates() {
  597. $result = TimeLib::timezoneByCoordinates(48, 11);
  598. $this->assertEquals('Europe/Vaduz', $result);
  599. }
  600. /**
  601. * TimeLibTest::testCweekDay()
  602. *
  603. * @return void
  604. */
  605. public function testCweekDay() {
  606. //$this->out($this->_header(__FUNCTION__), true);
  607. // wednesday
  608. $ret = TimeLib::cweekDay(51, 2011, 2);
  609. $this->out('51, 2011, 2');
  610. $this->out(date(FORMAT_DB_DATETIME, $ret));
  611. $this->assertTrue($ret >= 1324422000 && $ret <= 1324425600);
  612. //$this->assertEquals(1324422000, $ret);
  613. }
  614. public function testCweeks() {
  615. //$this->out($this->_header(__FUNCTION__), true);
  616. $ret = TimeLib::cweeks('2004');
  617. $this->assertEquals(53, $ret);
  618. $ret = TimeLib::cweeks('2010');
  619. $this->assertEquals(52, $ret);
  620. $ret = TimeLib::cweeks('2006');
  621. $this->assertEquals(52, $ret);
  622. $ret = TimeLib::cweeks('2007');
  623. $this->assertEquals(52, $ret);
  624. /*
  625. for ($i = 1990; $i < 2020; $i++) {
  626. $this->out(TimeLib::cweeks($i).BR;
  627. }
  628. */
  629. }
  630. public function testCweekBeginning() {
  631. //$this->out($this->_header(__FUNCTION__), true);
  632. $values = [
  633. '2001' => 978303600, # Mon 01.01.2001, 00:00
  634. '2006' => 1136156400, # Mon 02.01.2006, 00:00
  635. '2010' => 1262559600, # Mon 04.01.2010, 00:00
  636. '2013' => 1356908400, # Mon 31.12.2012, 00:00
  637. ];
  638. foreach ($values as $year => $expected) {
  639. $ret = TimeLib::cweekBeginning($year);
  640. $this->out($ret);
  641. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  642. //$this->assertEquals($ret, $expected, null, $year);
  643. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  644. }
  645. $values = [
  646. ['2001', '1', 978303600], # Mon 01.01.2001, 00:00:00
  647. ['2001', '2', 978908400], # Mon 08.01.2001, 00:00:00
  648. ['2001', '5', 980722800], # Mon 29.01.2001, 00:00:00
  649. ['2001', '52', 1009148400], # Mon 24.12.2001, 00:00:00
  650. ['2013', '11', 1362956400], # Mon 11.03.2013, 00:00:00
  651. ['2006', '3', 1137366000], # Mon 16.01.2006, 00:00:00
  652. ];
  653. foreach ($values as $v) {
  654. $ret = TimeLib::cweekBeginning($v[0], $v[1]);
  655. $this->out($ret);
  656. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  657. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  658. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  659. }
  660. }
  661. public function testCweekEnding() {
  662. //$this->out($this->_header(__FUNCTION__), true);
  663. $values = [
  664. '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
  665. '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
  666. '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
  667. '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
  668. ];
  669. foreach ($values as $year => $expected) {
  670. $ret = TimeLib::cweekEnding($year);
  671. $this->out($ret);
  672. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  673. //$this->assertSame($expected, $ret);
  674. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  675. }
  676. $values = [
  677. ['2001', '1', 978908399], # Sun 07.01.2001, 23:59:59
  678. ['2001', '2', 979513199], # Sun 14.01.2001, 23:59:59
  679. ['2001', '5', 981327599], # Sun 04.02.2001, 23:59:59
  680. ['2001', '52', 1009753199], # Sun 30.12.2001, 23:59:59
  681. ['2013', '11', 1363561199], # Sun 17.03.2013, 23:59:59
  682. ['2006', '3', 1137970799], # Sun 22.01.2006, 23:59:59
  683. ];
  684. foreach ($values as $v) {
  685. $ret = TimeLib::cweekEnding($v[0], $v[1]);
  686. $this->out($ret);
  687. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  688. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  689. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  690. }
  691. }
  692. /**
  693. * TimeLibTest::testAgeByHoroscop()
  694. *
  695. * @return void
  696. */
  697. public function testAgeByHoroscop() {
  698. App::uses('ZodiacLib', 'Tools.Misc');
  699. $this->skipIf(true, 'Fix these tests');
  700. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
  701. // between xxxx-08-24 and xxxx-09-23 the latter, otherwise the first:
  702. $this->assertEquals(date('Y') - 2000 - 1, $is);
  703. $this->assertEquals([date('Y') - 2000 - 1, date('Y') - 2000], $is);
  704. $is = TimeLib::ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
  705. //pr($is);
  706. $this->assertEquals(date('Y') - 1991 - 1, $is);
  707. $is = TimeLib::ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
  708. //pr($is);
  709. $this->assertEquals([date('Y') - 1986 - 1, date('Y') - 1986], $is);
  710. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
  711. //debug($is);
  712. $this->assertEquals(date('Y') - 2000 - 1, $is); //array(10, 11)
  713. }
  714. /**
  715. * TimeLibTest::testAgeRange()
  716. *
  717. * @return void
  718. */
  719. public function testAgeRange() {
  720. $is = TimeLib::ageRange(2000);
  721. $this->assertEquals(date('Y') - 2000 - 1, $is);
  722. $is = TimeLib::ageRange(date('Y') - 11, null, null, 5);
  723. $this->assertEquals([6, 10], $is);
  724. $is = TimeLib::ageRange(date('Y') - 13, null, null, 5);
  725. $this->assertEquals([11, 15], $is);
  726. $is = TimeLib::ageRange(1985, 23, 11);
  727. $this->assertEquals(date('Y') - 1985 - 1, $is);
  728. $is = TimeLib::ageRange(date('Y') - 29, null, null, 6);
  729. $this->assertEquals([25, 30], $is);
  730. $is = TimeLib::ageRange(date('Y') - 29, 21, 11, 7);
  731. $this->assertEquals([22, 28], $is);
  732. }
  733. /**
  734. * TimeLibTest::testParseDate()
  735. *
  736. * @return void
  737. */
  738. public function testParseDate() {
  739. //echo $this->_header(__FUNCTION__);
  740. $tests = [
  741. '2010-12-11' => 1292022000,
  742. '2010-01-02' => 1262386800,
  743. '10-01-02' => 1262386800,
  744. '2.1.2010' => 1262386800,
  745. '2.1.10' => 1262386800,
  746. '02.01.10' => 1262386800,
  747. '02.01.2010' => 1262386800,
  748. '02.01.2010 22:11' => 1262386800,
  749. '2010-01-02 22:11' => 1262386800,
  750. ];
  751. foreach ($tests as $was => $expected) {
  752. $is = TimeLib::parseDate($was);
  753. $this->assertTrue($is <= $expected + HOUR && $is >= $expected);
  754. }
  755. }
  756. /**
  757. * TimeLibTest::testParseTime()
  758. *
  759. * @return void
  760. */
  761. public function testParseTime() {
  762. //echo $this->_header(__FUNCTION__);
  763. $tests = [
  764. '2:4' => 7440,
  765. '2:04' => 7440,
  766. '2' => 7200,
  767. '1,5' => 3600 + 1800,
  768. '1.5' => 3600 + 1800,
  769. '1.50' => 3600 + 1800,
  770. '1.01' => 3660,
  771. ':4' => 240,
  772. ':04' => 240,
  773. ':40' => 40 * MINUTE,
  774. '1:2:4' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  775. '01:2:04' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  776. '0:2:04' => 2 * MINUTE + 4 * SECOND,
  777. '::4' => 4 * SECOND,
  778. '::04' => 4 * SECOND,
  779. '::40' => 40 * SECOND,
  780. '2011-11-12 10:10:10' => 10 * HOUR + 10 * MINUTE + 10 * SECOND,
  781. ];
  782. // positive
  783. foreach ($tests as $was => $expected) {
  784. $is = TimeLib::parseTime($was);
  785. //pr($is);
  786. $this->assertEquals($expected, $is);
  787. }
  788. unset($tests['2011-11-12 10:10:10']);
  789. // negative
  790. foreach ($tests as $was => $expected) {
  791. $is = TimeLib::parseTime('-' . $was);
  792. //pr($is);
  793. $this->assertEquals(-$expected, $is);
  794. }
  795. }
  796. /**
  797. * TimeLibTest::testBuildTime()
  798. *
  799. * @return void
  800. */
  801. public function testBuildTime() {
  802. //echo $this->_header(__FUNCTION__);
  803. $tests = [
  804. 7440 => '2:04',
  805. 7220 => '2:00', # 02:00:20 => rounded to 2:00:00
  806. 5400 => '1:30',
  807. 3660 => '1:01',
  808. ];
  809. // positive
  810. foreach ($tests as $was => $expected) {
  811. $is = TimeLib::buildTime($was);
  812. //pr($is);
  813. $this->assertEquals($expected, $is);
  814. }
  815. // negative
  816. foreach ($tests as $was => $expected) {
  817. $is = TimeLib::buildTime(-$was);
  818. //pr($is);
  819. $this->assertEquals('-' . $expected, $is);
  820. }
  821. }
  822. /**
  823. * TimeLibTest::testBuildDefaultTime()
  824. *
  825. * @return void
  826. */
  827. public function testBuildDefaultTime() {
  828. //echo $this->_header(__FUNCTION__);
  829. $tests = [
  830. 7440 => '02:04:00',
  831. 7220 => '02:00:20',
  832. 5400 => '01:30:00',
  833. 3660 => '01:01:00',
  834. 1 * HOUR + 2 * MINUTE + 4 * SECOND => '01:02:04',
  835. ];
  836. foreach ($tests as $was => $expected) {
  837. $is = TimeLib::buildDefaultTime($was);
  838. //pr($is);
  839. $this->assertEquals($expected, $is);
  840. }
  841. }
  842. /**
  843. * 9.30 => 9.50
  844. *
  845. * @return void
  846. */
  847. public function testStandardDecimal() {
  848. //echo $this->_header(__FUNCTION__);
  849. $value = '9.30';
  850. $is = TimeLib::standardToDecimalTime($value);
  851. $this->assertEquals('9.50', round($is, 2));
  852. $value = '9.3';
  853. $is = TimeLib::standardToDecimalTime($value);
  854. $this->assertEquals('9.50', round($is, 2));
  855. }
  856. /**
  857. * 9.50 => 9.30
  858. *
  859. * @return void
  860. */
  861. public function testDecimalStandard() {
  862. //echo $this->_header(__FUNCTION__);
  863. $value = '9.50';
  864. $is = TimeLib::decimalToStandardTime($value);
  865. $this->assertEquals('9.3', round($is, 2));
  866. $value = '9.5';
  867. $is = TimeLib::decimalToStandardTime($value);
  868. //pr($is);
  869. $this->assertEquals('9.3', $is);
  870. $is = TimeLib::decimalToStandardTime($value, 2);
  871. //pr($is);
  872. $this->assertEquals('9.30', $is);
  873. $is = TimeLib::decimalToStandardTime($value, 2, ':');
  874. //pr($is);
  875. $this->assertEquals('9:30', $is);
  876. }
  877. /**
  878. * TimeLibTest::testHasDaylightSavingTime()
  879. *
  880. * @return void
  881. */
  882. public function testHasDaylightSavingTime() {
  883. $timezone = 'Europe/Berlin';
  884. $x = TimeLib::hasDaylightSavingTime($timezone);
  885. $this->assertTrue($x);
  886. $timezone = 'Asia/Baghdad';
  887. $x = TimeLib::hasDaylightSavingTime($timezone);
  888. $this->assertFalse($x);
  889. }
  890. /**
  891. * TimeLibTest::testTimezone()
  892. *
  893. * @return void
  894. */
  895. public function testTimezone() {
  896. $timezone = TimeLib::timezone();
  897. // usually UTC
  898. $name = $timezone->getName();
  899. $this->debug($name);
  900. $this->assertTrue(!empty($name));
  901. $location = $timezone->getLocation();
  902. $this->debug($location);
  903. $this->assertTrue(!empty($location['country_code']));
  904. $this->assertTrue(isset($location['latitude']));
  905. $this->assertTrue(isset($location['longitude']));
  906. $offset = $timezone->getOffset(new DateTime('@' . mktime(0, 0, 0, 1, 1, date('Y'))));
  907. $this->debug($offset);
  908. $phpTimezone = date_default_timezone_get();
  909. $this->assertEquals($name, $phpTimezone);
  910. }
  911. /**
  912. * TimeLibTest::testGetGmtOffset()
  913. *
  914. * @return void
  915. */
  916. public function testGetGmtOffset() {
  917. $timezone = Configure::read('Config.timezone');
  918. $phpTimezone = date_default_timezone_get();
  919. Configure::write('Config.timezone', 'UTC');
  920. date_default_timezone_set('UTC');
  921. $result = TimeLib::getGmtOffset();
  922. $this->assertEquals(0, $result);
  923. $result = TimeLib::getGmtOffset('Europe/Berlin');
  924. $this->assertTrue($result > 0, $result);
  925. $result = TimeLib::getGmtOffset('America/Los_Angeles');
  926. $this->assertTrue($result < 0, $result);
  927. Configure::write('Config.timezone', $timezone);
  928. date_default_timezone_set($phpTimezone);
  929. }
  930. /**
  931. * DatetimeHelperTest::testTzOffset()
  932. *
  933. * @return void
  934. */
  935. public function testTzOffset() {
  936. $timezone = Configure::read('Config.timezone');
  937. $phpTimezone = date_default_timezone_get();
  938. Configure::write('Config.timezone', 'UTC');
  939. date_default_timezone_set('UTC');
  940. $result = TimeLib::tzOffset(0, false);
  941. $this->assertEquals(0, $result);
  942. Configure::write('Config.timezone', 'Europe/Berlin');
  943. date_default_timezone_set('Europe/Berlin');
  944. // Buggy
  945. /*
  946. $factor = date('I') ? 2 : 1;
  947. $result = TimeLib::tzOffset($factor * HOUR, false);
  948. $this->assertEquals(0, $result);
  949. */
  950. Configure::write('Config.timezone', $timezone);
  951. date_default_timezone_set($phpTimezone);
  952. }
  953. }