TimeLibTest.php 31 KB

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