TimeTest.php 31 KB

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