TimeTest.php 32 KB

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