TimeLibTest.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 ' . __d('tools', '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(__d('tools', '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 ' . __d('tools', '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(__d('tools', '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(__d('tools', 'Today'), "(Model.field >= '" . date(FORMAT_DB_DATE) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
  223. array(__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')"),
  224. array(__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')"),
  225. array(__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')"),
  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(null));
  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. // These tests might fail on windows / some strange setup?
  300. $this->assertEquals('2', TimeLib::age('2005-03-01', '2008-02-28'));
  301. $this->assertEquals('2', TimeLib::age('2005-03-01', '2008-02-29'));
  302. $this->assertEquals('3', TimeLib::age('2005-03-01', '2008-03-01'));
  303. $this->assertEquals('3', TimeLib::age('2005-02-29', '2008-03-01'));
  304. // Future
  305. list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years -1 day')));
  306. $this->assertEquals('9', TimeLib::age(null, $yearE . '-' . $monthE . '-' . $dayE));
  307. list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years +1 day')));
  308. $this->assertEquals('10', TimeLib::age(null, $yearE . '-' . $monthE . '-' . $dayE));
  309. $birthday = '2033-04-09';
  310. $this->assertEquals(-1, TimeLib::age($birthday));
  311. $birthday = '1985-04-08';
  312. $relativeDate = '2010-04-07';
  313. $this->assertEquals('24', TimeLib::age($birthday, $relativeDate));
  314. $relativeDate = '2010-04-08';
  315. $this->assertEquals('25', TimeLib::age($birthday, $relativeDate));
  316. $relativeDate = '2010-04-09';
  317. $this->assertEquals('25', TimeLib::age($birthday, $relativeDate));
  318. }
  319. /**
  320. * TimeLibTest::testAgeBounds()
  321. *
  322. * @return void
  323. */
  324. public function testAgeBounds() {
  325. $this->out($this->_header(__FUNCTION__), true);
  326. $values = array(
  327. array(20, 20, array('min' => '1990-07-07', 'max' => '1991-07-06')),
  328. array(10, 30, array('min' => '1980-07-07', 'max' => '2001-07-06')),
  329. array(11, 12, array('min' => '1998-07-07', 'max' => '2000-07-06'))
  330. );
  331. foreach ($values as $v) {
  332. //echo $v[0].'/'.$v[1];
  333. $ret = TimeLib::ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
  334. //pr($ret);
  335. if (isset($v[2])) {
  336. $this->assertSame($v[2], $ret);
  337. $this->assertEquals($v[0], TimeLib::age($v[2]['max'], '2011-07-06'));
  338. $this->assertEquals($v[1], TimeLib::age($v[2]['min'], '2011-07-06'));
  339. }
  340. }
  341. }
  342. /**
  343. * TimeLibTest::testAgeByYear()
  344. *
  345. * @return void
  346. */
  347. public function testAgeByYear() {
  348. $this->out($this->_header(__FUNCTION__), true);
  349. // year only
  350. $is = TimeLib::ageByYear(2000);
  351. //$this->out($is);
  352. if (date('n') == 1 && date('j') == 1) {
  353. $this->assertEquals(date('Y') - 2000, $is);
  354. } else {
  355. $this->assertEquals((date('Y') - 2001) . '/' . (date('Y') - 2000), $is);
  356. }
  357. $is = TimeLib::ageByYear(1985);
  358. $this->assertEquals((date('Y') - 1986) . '/' . (date('Y') - 1985), $is);
  359. // with month
  360. if (($month = date('n') + 1) <= 12) {
  361. $is = TimeLib::ageByYear(2000, $month);
  362. $this->out($is);
  363. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  364. $this->assertSame(date('Y') - 2001, $is); //null, '2000/'.$month
  365. }
  366. if (($month = date('n') - 1) >= 1) {
  367. $is = TimeLib::ageByYear(2000, $month);
  368. $this->out($is);
  369. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  370. $this->assertSame(date('Y') - 2000, $is); //null, '2000/'.$month)
  371. }
  372. }
  373. /**
  374. * TimeLibTest::testDaysInMonth()
  375. *
  376. * @return void
  377. */
  378. public function testDaysInMonth() {
  379. $this->out($this->_header(__FUNCTION__), true);
  380. $ret = TimeLib::daysInMonth('2004', '3');
  381. $this->assertEquals(31, $ret);
  382. $ret = TimeLib::daysInMonth('2006', '4');
  383. $this->assertEquals(30, $ret);
  384. $ret = TimeLib::daysInMonth('2007', '2');
  385. $this->assertEquals(28, $ret);
  386. $ret = TimeLib::daysInMonth('2008', '2');
  387. $this->assertEquals(29, $ret);
  388. }
  389. /**
  390. * TimeLibTest::testDay()
  391. *
  392. * @return void
  393. */
  394. public function testDay() {
  395. $this->out($this->_header(__FUNCTION__), true);
  396. $ret = TimeLib::day('0');
  397. $this->assertEquals(__d('tools', 'Sunday'), $ret);
  398. $ret = TimeLib::day(2, true);
  399. $this->assertEquals(__d('tools', 'Tue'), $ret);
  400. $ret = TimeLib::day(6);
  401. $this->assertEquals(__d('tools', 'Saturday'), $ret);
  402. $ret = TimeLib::day(6, false, 1);
  403. $this->assertEquals(__d('tools', 'Sunday'), $ret);
  404. $ret = TimeLib::day(0, false, 2);
  405. $this->assertEquals(__d('tools', 'Tuesday'), $ret);
  406. $ret = TimeLib::day(1, false, 6);
  407. $this->assertEquals(__d('tools', 'Sunday'), $ret);
  408. }
  409. /**
  410. * TimeLibTest::testMonth()
  411. *
  412. * @return void
  413. */
  414. public function testMonth() {
  415. $this->out($this->_header(__FUNCTION__), true);
  416. $ret = TimeLib::month('11');
  417. $this->assertEquals(__d('tools', 'November'), $ret);
  418. $ret = TimeLib::month(1);
  419. $this->assertEquals(__d('tools', 'January'), $ret);
  420. $ret = TimeLib::month(2, true, array('appendDot' => true));
  421. $this->assertEquals(__d('tools', 'Feb') . '.', $ret);
  422. $ret = TimeLib::month(5, true, array('appendDot' => true));
  423. $this->assertEquals(__d('tools', 'May'), $ret);
  424. }
  425. /**
  426. * TimeLibTest::testDays()
  427. *
  428. * @return void
  429. */
  430. public function testDays() {
  431. $this->out($this->_header(__FUNCTION__), true);
  432. $ret = TimeLib::days();
  433. $this->assertTrue(count($ret) === 7);
  434. }
  435. /**
  436. * TimeLibTest::testMonths()
  437. *
  438. * @return void
  439. */
  440. public function testMonths() {
  441. $this->out($this->_header(__FUNCTION__), true);
  442. $ret = TimeLib::months();
  443. $this->assertTrue(count($ret) === 12);
  444. }
  445. /**
  446. * TimeLibTest::testRelLengthOfTime()
  447. *
  448. * @return void
  449. */
  450. public function testRelLengthOfTime() {
  451. $ret = TimeLib::relLengthOfTime('1990-11-20');
  452. //pr($ret);
  453. $ret = TimeLib::relLengthOfTime('2012-11-20');
  454. //pr($ret);
  455. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
  456. //pr($res);
  457. $this->assertTrue(!empty($res));
  458. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR), null, array('plural' => 'n'));
  459. //pr($res);
  460. //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__d('tools', 'Hours'));
  461. $this->assertEquals(__d('tools', '%s ago', '4 ' . __d('tools', 'Days') . 'n' . ', ' . '5 ' . __d('tools', 'Hours')), $res);
  462. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() + 4 * DAY + 5 * HOUR), null, array('plural' => 'n'));
  463. //pr($res);
  464. $this->assertEquals(__d('tools', 'In %s', '4 ' . __d('tools', 'Days') . 'n' . ', ' . '5 ' . __d('tools', 'Hours')), $res);
  465. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, array('plural' => 'n'));
  466. //pr($res);
  467. $this->assertEquals($res, __d('tools', 'justNow'));
  468. }
  469. /**
  470. * Test cweek
  471. *
  472. * @return void
  473. */
  474. public function testCweek() {
  475. $year = 2008;
  476. $month = 12;
  477. $day = 29;
  478. $date = mktime(0, 0, 0, $month, $day, $year);
  479. $this->assertEquals('01/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  480. $year = 2009;
  481. $month = 1;
  482. $day = 1;
  483. $date = mktime(0, 0, 0, $month, $day, $year);
  484. $this->assertEquals('01/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  485. $year = 2009;
  486. $month = 1;
  487. $day = 9;
  488. $date = mktime(0, 0, 0, $month, $day, $year);
  489. $this->assertEquals('02/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day . ' 00:00:00'));
  490. $year = 2009;
  491. $month = 12;
  492. $day = 26;
  493. $date = mktime(0, 0, 0, $month, $day, $year);
  494. $this->assertEquals('52/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  495. }
  496. /**
  497. * Test IsInTheFuture
  498. *
  499. * @return void
  500. */
  501. public function testIsInTheFuture() {
  502. $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
  503. $is = TimeLib::isInTheFuture($testDate);
  504. $this->assertTrue($is);
  505. $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
  506. $is = TimeLib::isInTheFuture($testDate);
  507. $this->assertFalse($is);
  508. }
  509. /**
  510. * Test IsNotTodayAndInTheFuture
  511. *
  512. * @return void
  513. */
  514. public function testIsNotTodayAndInTheFuture() {
  515. $testDate = date(FORMAT_DB_DATE, time());
  516. $is = TimeLib::isNotTodayAndInTheFuture($testDate);
  517. $this->assertFalse($is);
  518. $testDate = date(FORMAT_DB_DATETIME, time() + 1 * DAY);
  519. $is = TimeLib::isNotTodayAndInTheFuture($testDate);
  520. $this->assertTrue($is);
  521. }
  522. /**
  523. * Test IsDayAfterTomorrow
  524. *
  525. * @return void
  526. */
  527. public function testIsDayAfterTomorrow() {
  528. $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
  529. $is = TimeLib::isDayAfterTomorrow($testDate);
  530. $this->assertTrue($is);
  531. $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
  532. $is = TimeLib::isDayAfterTomorrow($testDate);
  533. $this->assertFalse($is);
  534. }
  535. /**
  536. * TimeLibTest::testLengthOfTime()
  537. *
  538. * @return void
  539. */
  540. public function testLengthOfTime() {
  541. $this->out($this->_header(__FUNCTION__), true);
  542. $ret = TimeLib::lengthOfTime(60);
  543. //pr($ret);
  544. // FIX ME! Doesn't work!
  545. $ret = TimeLib::lengthOfTime(-60);
  546. //pr($ret);
  547. $ret = TimeLib::lengthOfTime(-121);
  548. //pr($ret);
  549. $this->assertEquals('6 ' . __d('tools', 'Minutes') . ', 40 ' . __d('tools', 'Seconds'), TimeLib::lengthOfTime(400));
  550. $res = TimeLib::lengthOfTime(400, 'i');
  551. //pr($res);
  552. $this->assertEquals('6 ' . __d('tools', 'Minutes'), $res);
  553. $res = TimeLib::lengthOfTime(6 * DAY);
  554. //pr($res);
  555. $this->assertEquals('6 ' . __d('tools', 'Days') . ', 0 ' . __d('tools', 'Hours'), $res);
  556. }
  557. /**
  558. * TimeLibTest::testFuzzyFromOffset()
  559. *
  560. * @return void
  561. */
  562. public function testFuzzyFromOffset() {
  563. $this->out($this->_header(__FUNCTION__), true);
  564. $ret = TimeLib::fuzzyFromOffset(MONTH);
  565. //pr($ret);
  566. $ret = TimeLib::fuzzyFromOffset(120);
  567. //pr($ret);
  568. $ret = TimeLib::fuzzyFromOffset(DAY);
  569. //pr($ret);
  570. $ret = TimeLib::fuzzyFromOffset(DAY + 2 * MINUTE);
  571. //pr($ret);
  572. // FIX ME! Doesn't work!
  573. $ret = TimeLib::fuzzyFromOffset(-DAY);
  574. //pr($ret);
  575. }
  576. /**
  577. * TimeLibTest::testCweekMod()
  578. *
  579. * @return void
  580. */
  581. public function testCweekMod() {
  582. $result = TimeLib::cWeekMod(0);
  583. $this->assertEquals(0, $result);
  584. $result = TimeLib::cWeekMod(1);
  585. $this->assertEquals(1, $result);
  586. $result = TimeLib::cWeekMod(6);
  587. $this->assertEquals(0, $result);
  588. }
  589. /**
  590. * TimeLibTest::testTimezoneByCoordinates()
  591. *
  592. * @return void
  593. */
  594. public function testTimezoneByCoordinates() {
  595. $result = TimeLib::timezoneByCoordinates(48, 11);
  596. $this->assertEquals('Europe/Vaduz', $result);
  597. }
  598. /**
  599. * TimeLibTest::testCweekDay()
  600. *
  601. * @return void
  602. */
  603. public function testCweekDay() {
  604. $this->out($this->_header(__FUNCTION__), true);
  605. // wednesday
  606. $ret = TimeLib::cweekDay(51, 2011, 2);
  607. $this->out('51, 2011, 2');
  608. $this->out(date(FORMAT_DB_DATETIME, $ret));
  609. $this->assertTrue($ret >= 1324422000 && $ret <= 1324425600);
  610. //$this->assertEquals(1324422000, $ret);
  611. }
  612. public function testCweeks() {
  613. $this->out($this->_header(__FUNCTION__), true);
  614. $ret = TimeLib::cweeks('2004');
  615. $this->assertEquals(53, $ret);
  616. $ret = TimeLib::cweeks('2010');
  617. $this->assertEquals(52, $ret);
  618. $ret = TimeLib::cweeks('2006');
  619. $this->assertEquals(52, $ret);
  620. $ret = TimeLib::cweeks('2007');
  621. $this->assertEquals(52, $ret);
  622. /*
  623. for ($i = 1990; $i < 2020; $i++) {
  624. $this->out(TimeLib::cweeks($i).BR;
  625. }
  626. */
  627. }
  628. public function testCweekBeginning() {
  629. $this->out($this->_header(__FUNCTION__), true);
  630. $values = array(
  631. '2001' => 978303600, # Mon 01.01.2001, 00:00
  632. '2006' => 1136156400, # Mon 02.01.2006, 00:00
  633. '2010' => 1262559600, # Mon 04.01.2010, 00:00
  634. '2013' => 1356908400, # Mon 31.12.2012, 00:00
  635. );
  636. foreach ($values as $year => $expected) {
  637. $ret = TimeLib::cweekBeginning($year);
  638. $this->out($ret);
  639. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  640. //$this->assertEquals($ret, $expected, null, $year);
  641. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  642. }
  643. $values = array(
  644. array('2001', '1', 978303600), # Mon 01.01.2001, 00:00:00
  645. array('2001', '2', 978908400), # Mon 08.01.2001, 00:00:00
  646. array('2001', '5', 980722800), # Mon 29.01.2001, 00:00:00
  647. array('2001', '52', 1009148400), # Mon 24.12.2001, 00:00:00
  648. array('2013', '11', 1362956400), # Mon 11.03.2013, 00:00:00
  649. array('2006', '3', 1137366000), # Mon 16.01.2006, 00:00:00
  650. );
  651. foreach ($values as $v) {
  652. $ret = TimeLib::cweekBeginning($v[0], $v[1]);
  653. $this->out($ret);
  654. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  655. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  656. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  657. }
  658. }
  659. public function testCweekEnding() {
  660. $this->out($this->_header(__FUNCTION__), true);
  661. $values = array(
  662. '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
  663. '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
  664. '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
  665. '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
  666. );
  667. foreach ($values as $year => $expected) {
  668. $ret = TimeLib::cweekEnding($year);
  669. $this->out($ret);
  670. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  671. //$this->assertSame($expected, $ret);
  672. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  673. }
  674. $values = array(
  675. array('2001', '1', 978908399), # Sun 07.01.2001, 23:59:59
  676. array('2001', '2', 979513199), # Sun 14.01.2001, 23:59:59
  677. array('2001', '5', 981327599), # Sun 04.02.2001, 23:59:59
  678. array('2001', '52', 1009753199), # Sun 30.12.2001, 23:59:59
  679. array('2013', '11', 1363561199), # Sun 17.03.2013, 23:59:59
  680. array('2006', '3', 1137970799), # Sun 22.01.2006, 23:59:59
  681. );
  682. foreach ($values as $v) {
  683. $ret = TimeLib::cweekEnding($v[0], $v[1]);
  684. $this->out($ret);
  685. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  686. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  687. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  688. }
  689. }
  690. /**
  691. * TimeLibTest::testAgeByHoroscop()
  692. *
  693. * @return void
  694. */
  695. public function testAgeByHoroscop() {
  696. App::uses('ZodiacLib', 'Tools.Misc');
  697. $this->skipIf(true, 'Fix these tests');
  698. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
  699. // between xxxx-08-24 and xxxx-09-23 the latter, otherwise the first:
  700. $this->assertEquals(date('Y') - 2000 - 1, $is);
  701. $this->assertEquals(array(date('Y') - 2000 - 1, date('Y') - 2000), $is);
  702. $is = TimeLib::ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
  703. //pr($is);
  704. $this->assertEquals(date('Y') - 1991 - 1, $is);
  705. $is = TimeLib::ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
  706. //pr($is);
  707. $this->assertEquals(array(date('Y') - 1986 - 1, date('Y') - 1986), $is);
  708. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
  709. //debug($is);
  710. $this->assertEquals(date('Y') - 2000 - 1, $is); //array(10, 11)
  711. }
  712. /**
  713. * TimeLibTest::testAgeRange()
  714. *
  715. * @return void
  716. */
  717. public function testAgeRange() {
  718. $is = TimeLib::ageRange(2000);
  719. $this->assertEquals(date('Y') - 2000 - 1, $is);
  720. $is = TimeLib::ageRange(date('Y') - 11, null, null, 5);
  721. $this->assertEquals(array(6, 10), $is);
  722. $is = TimeLib::ageRange(date('Y') - 13, null, null, 5);
  723. $this->assertEquals(array(11, 15), $is);
  724. $is = TimeLib::ageRange(1985, 23, 11);
  725. $this->assertEquals(date('Y') - 1985 - 1, $is);
  726. $is = TimeLib::ageRange(date('Y') - 29, null, null, 6);
  727. $this->assertEquals(array(25, 30), $is);
  728. $is = TimeLib::ageRange(date('Y') - 29, 21, 11, 7);
  729. $this->assertEquals(array(22, 28), $is);
  730. }
  731. /**
  732. * TimeLibTest::testParseDate()
  733. *
  734. * @return void
  735. */
  736. public function testParseDate() {
  737. //echo $this->_header(__FUNCTION__);
  738. $tests = array(
  739. '2010-12-11' => 1292022000,
  740. '2010-01-02' => 1262386800,
  741. '10-01-02' => 1262386800,
  742. '2.1.2010' => 1262386800,
  743. '2.1.10' => 1262386800,
  744. '02.01.10' => 1262386800,
  745. '02.01.2010' => 1262386800,
  746. '02.01.2010 22:11' => 1262386800,
  747. '2010-01-02 22:11' => 1262386800,
  748. );
  749. foreach ($tests as $was => $expected) {
  750. $is = TimeLib::parseDate($was);
  751. $this->assertTrue($is <= $expected + HOUR && $is >= $expected);
  752. }
  753. }
  754. /**
  755. * TimeLibTest::testParseTime()
  756. *
  757. * @return void
  758. */
  759. public function testParseTime() {
  760. //echo $this->_header(__FUNCTION__);
  761. $tests = array(
  762. '2:4' => 7440,
  763. '2:04' => 7440,
  764. '2' => 7200,
  765. '1,5' => 3600 + 1800,
  766. '1.5' => 3600 + 1800,
  767. '1.50' => 3600 + 1800,
  768. '1.01' => 3660,
  769. ':4' => 240,
  770. ':04' => 240,
  771. ':40' => 40 * MINUTE,
  772. '1:2:4' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  773. '01:2:04' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  774. '0:2:04' => 2 * MINUTE + 4 * SECOND,
  775. '::4' => 4 * SECOND,
  776. '::04' => 4 * SECOND,
  777. '::40' => 40 * SECOND,
  778. '2011-11-12 10:10:10' => 10 * HOUR + 10 * MINUTE + 10 * SECOND,
  779. );
  780. // positive
  781. foreach ($tests as $was => $expected) {
  782. $is = TimeLib::parseTime($was);
  783. //pr($is);
  784. $this->assertEquals($expected, $is);
  785. }
  786. unset($tests['2011-11-12 10:10:10']);
  787. // negative
  788. foreach ($tests as $was => $expected) {
  789. $is = TimeLib::parseTime('-' . $was);
  790. //pr($is);
  791. $this->assertEquals(-$expected, $is);
  792. }
  793. }
  794. /**
  795. * TimeLibTest::testBuildTime()
  796. *
  797. * @return void
  798. */
  799. public function testBuildTime() {
  800. //echo $this->_header(__FUNCTION__);
  801. $tests = array(
  802. 7440 => '2:04',
  803. 7220 => '2:00', # 02:00:20 => rounded to 2:00:00
  804. 5400 => '1:30',
  805. 3660 => '1:01',
  806. );
  807. // positive
  808. foreach ($tests as $was => $expected) {
  809. $is = TimeLib::buildTime($was);
  810. //pr($is);
  811. $this->assertEquals($expected, $is);
  812. }
  813. // negative
  814. foreach ($tests as $was => $expected) {
  815. $is = TimeLib::buildTime(-$was);
  816. //pr($is);
  817. $this->assertEquals('-' . $expected, $is);
  818. }
  819. }
  820. /**
  821. * TimeLibTest::testBuildDefaultTime()
  822. *
  823. * @return void
  824. */
  825. public function testBuildDefaultTime() {
  826. //echo $this->_header(__FUNCTION__);
  827. $tests = array(
  828. 7440 => '02:04:00',
  829. 7220 => '02:00:20',
  830. 5400 => '01:30:00',
  831. 3660 => '01:01:00',
  832. 1 * HOUR + 2 * MINUTE + 4 * SECOND => '01:02:04',
  833. );
  834. foreach ($tests as $was => $expected) {
  835. $is = TimeLib::buildDefaultTime($was);
  836. //pr($is);
  837. $this->assertEquals($expected, $is);
  838. }
  839. }
  840. /**
  841. * 9.30 => 9.50
  842. *
  843. * @return void
  844. */
  845. public function testStandardDecimal() {
  846. //echo $this->_header(__FUNCTION__);
  847. $value = '9.30';
  848. $is = TimeLib::standardToDecimalTime($value);
  849. $this->assertEquals('9.50', round($is, 2));
  850. $value = '9.3';
  851. $is = TimeLib::standardToDecimalTime($value);
  852. $this->assertEquals('9.50', round($is, 2));
  853. }
  854. /**
  855. * 9.50 => 9.30
  856. *
  857. * @return void
  858. */
  859. public function testDecimalStandard() {
  860. //echo $this->_header(__FUNCTION__);
  861. $value = '9.50';
  862. $is = TimeLib::decimalToStandardTime($value);
  863. $this->assertEquals('9.3', round($is, 2));
  864. $value = '9.5';
  865. $is = TimeLib::decimalToStandardTime($value);
  866. //pr($is);
  867. $this->assertEquals('9.3', $is);
  868. $is = TimeLib::decimalToStandardTime($value, 2);
  869. //pr($is);
  870. $this->assertEquals('9.30', $is);
  871. $is = TimeLib::decimalToStandardTime($value, 2, ':');
  872. //pr($is);
  873. $this->assertEquals('9:30', $is);
  874. }
  875. /**
  876. * TimeLibTest::testHasDaylightSavingTime()
  877. *
  878. * @return void
  879. */
  880. public function testHasDaylightSavingTime() {
  881. $timezone = 'Europe/Berlin';
  882. $x = TimeLib::hasDaylightSavingTime($timezone);
  883. $this->assertTrue($x);
  884. $timezone = 'Asia/Baghdad';
  885. $x = TimeLib::hasDaylightSavingTime($timezone);
  886. $this->assertFalse($x);
  887. }
  888. /**
  889. * TimeLibTest::testTimezone()
  890. *
  891. * @return void
  892. */
  893. public function testTimezone() {
  894. $timezone = TimeLib::timezone();
  895. // usually UTC
  896. $name = $timezone->getName();
  897. $this->debug($name);
  898. $this->assertTrue(!empty($name));
  899. $location = $timezone->getLocation();
  900. $this->debug($location);
  901. $this->assertTrue(!empty($location['country_code']));
  902. $this->assertTrue(isset($location['latitude']));
  903. $this->assertTrue(isset($location['longitude']));
  904. $offset = $timezone->getOffset(new DateTime('@' . mktime(0, 0, 0, 1, 1, date('Y'))));
  905. $this->debug($offset);
  906. $phpTimezone = date_default_timezone_get();
  907. $this->assertEquals($name, $phpTimezone);
  908. }
  909. /**
  910. * TimeLibTest::testGetGmtOffset()
  911. *
  912. * @return void
  913. */
  914. public function testGetGmtOffset() {
  915. $timezone = Configure::read('Config.timezone');
  916. $phpTimezone = date_default_timezone_get();
  917. Configure::write('Config.timezone', 'UTC');
  918. date_default_timezone_set('UTC');
  919. $result = TimeLib::getGmtOffset();
  920. $this->assertEquals(0, $result);
  921. $result = TimeLib::getGmtOffset('Europe/Berlin');
  922. $this->assertTrue($result > 0, $result);
  923. $result = TimeLib::getGmtOffset('America/Los_Angeles');
  924. $this->assertTrue($result < 0, $result);
  925. Configure::write('Config.timezone', $timezone);
  926. date_default_timezone_set($phpTimezone);
  927. }
  928. /**
  929. * DatetimeHelperTest::testTzOffset()
  930. *
  931. * @return void
  932. */
  933. public function testTzOffset() {
  934. $timezone = Configure::read('Config.timezone');
  935. $phpTimezone = date_default_timezone_get();
  936. Configure::write('Config.timezone', 'UTC');
  937. date_default_timezone_set('UTC');
  938. $result = TimeLib::tzOffset(0, false);
  939. $this->assertEquals(0, $result);
  940. Configure::write('Config.timezone', 'Europe/Berlin');
  941. date_default_timezone_set('Europe/Berlin');
  942. $factor = date('I') ? 2 : 1;
  943. $result = TimeLib::tzOffset($factor * HOUR, false);
  944. $this->assertEquals(0, $result);
  945. Configure::write('Config.timezone', $timezone);
  946. date_default_timezone_set($phpTimezone);
  947. }
  948. }