TimeTest.php 32 KB

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