TimeTest.php 32 KB

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