TimeTest.php 31 KB

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