TimeTest.php 31 KB

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