TimeLibTest.php 32 KB

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