TimeLibTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 ' . __('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(__('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 ' . __('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(__('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(__('Today'), "(Model.field >= '" . date(FORMAT_DB_DATE) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
  223. array(__('Yesterday') . ' ' . __('until') . ' ' . __('Today'), "(Model.field >= '" . date(FORMAT_DB_DATE, time() - DAY) . " 00:00:00') AND (Model.field <= '" . date(FORMAT_DB_DATE) . " 23:59:59')"),
  224. array(__('Today') . ' ' . __('until') . ' ' . __('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(__('Yesterday') . ' ' . __('until') . ' ' . __('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());
  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. if (WINDOWS) {
  301. // On some local devs this seems to return the wrong age 3...
  302. $this->assertEquals('3', TimeLib::age('2005-03-01', '2008-02-29'));
  303. } else {
  304. $this->assertEquals('2', TimeLib::age('2005-03-01', '2008-02-29'));
  305. }
  306. $this->assertEquals('3', TimeLib::age('2005-03-01', '2008-03-01'));
  307. $this->assertEquals('3', TimeLib::age('2005-02-29', '2008-03-01'));
  308. // Future
  309. list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years -1 day')));
  310. $this->assertEquals('9', TimeLib::age(null, $yearE . '-' . $monthE . '-' . $dayE));
  311. list($yearE, $monthE, $dayE) = explode('-', date('Y-m-d', strtotime('+10 years +1 day')));
  312. $this->assertEquals('10', TimeLib::age(null, $yearE . '-' . $monthE . '-' . $dayE));
  313. $birthday = '2033-04-09';
  314. $this->assertEquals(-1, TimeLib::age($birthday));
  315. $birthday = '1985-04-08';
  316. $relativeDate = '2010-04-07';
  317. $this->assertEquals('24', TimeLib::age($birthday, $relativeDate));
  318. $relativeDate = '2010-04-08';
  319. $this->assertEquals('25', TimeLib::age($birthday, $relativeDate));
  320. $relativeDate = '2010-04-09';
  321. $this->assertEquals('25', TimeLib::age($birthday, $relativeDate));
  322. }
  323. /**
  324. * TimeLibTest::testAgeBounds()
  325. *
  326. * @return void
  327. */
  328. public function testAgeBounds() {
  329. $this->out($this->_header(__FUNCTION__), true);
  330. $values = array(
  331. array(20, 20, array('min' => '1990-07-07', 'max' => '1991-07-06')),
  332. array(10, 30, array('min' => '1980-07-07', 'max' => '2001-07-06')),
  333. array(11, 12, array('min' => '1998-07-07', 'max' => '2000-07-06'))
  334. );
  335. foreach ($values as $v) {
  336. //echo $v[0].'/'.$v[1];
  337. $ret = TimeLib::ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
  338. //pr($ret);
  339. if (isset($v[2])) {
  340. $this->assertSame($v[2], $ret);
  341. $this->assertEquals($v[0], TimeLib::age($v[2]['max'], '2011-07-06'));
  342. $this->assertEquals($v[1], TimeLib::age($v[2]['min'], '2011-07-06'));
  343. }
  344. }
  345. }
  346. /**
  347. * TimeLibTest::testAgeByYear()
  348. *
  349. * @return void
  350. */
  351. public function testAgeByYear() {
  352. $this->out($this->_header(__FUNCTION__), true);
  353. // year only
  354. $is = TimeLib::ageByYear(2000);
  355. $this->out($is);
  356. $this->assertEquals((date('Y') - 2001) . '/' . (date('Y') - 2000), $is);
  357. $is = TimeLib::ageByYear(1985);
  358. $this->assertEquals((date('Y') - 1986) . '/' . (date('Y') - 1985), $is);
  359. // with month
  360. if (($month = date('n') + 1) <= 12) {
  361. $is = TimeLib::ageByYear(2000, $month);
  362. $this->out($is);
  363. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  364. $this->assertSame(date('Y') - 2001, $is); //null, '2000/'.$month
  365. }
  366. if (($month = date('n') - 1) >= 1) {
  367. $is = TimeLib::ageByYear(2000, $month);
  368. $this->out($is);
  369. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  370. $this->assertSame(date('Y') - 2000, $is); //null, '2000/'.$month)
  371. }
  372. }
  373. /**
  374. * TimeLibTest::testDaysInMonth()
  375. *
  376. * @return void
  377. */
  378. public function testDaysInMonth() {
  379. $this->out($this->_header(__FUNCTION__), true);
  380. $ret = TimeLib::daysInMonth('2004', '3');
  381. $this->assertEquals(31, $ret);
  382. $ret = TimeLib::daysInMonth('2006', '4');
  383. $this->assertEquals(30, $ret);
  384. $ret = TimeLib::daysInMonth('2007', '2');
  385. $this->assertEquals(28, $ret);
  386. $ret = TimeLib::daysInMonth('2008', '2');
  387. $this->assertEquals(29, $ret);
  388. }
  389. /**
  390. * TimeLibTest::testDay()
  391. *
  392. * @return void
  393. */
  394. public function testDay() {
  395. $this->out($this->_header(__FUNCTION__), true);
  396. $ret = TimeLib::day('0');
  397. $this->assertEquals(__('Sunday'), $ret);
  398. $ret = TimeLib::day(2, true);
  399. $this->assertEquals(__('Tue'), $ret);
  400. $ret = TimeLib::day(6);
  401. $this->assertEquals(__('Saturday'), $ret);
  402. $ret = TimeLib::day(6, false, 1);
  403. $this->assertEquals(__('Sunday'), $ret);
  404. $ret = TimeLib::day(0, false, 2);
  405. $this->assertEquals(__('Tuesday'), $ret);
  406. $ret = TimeLib::day(1, false, 6);
  407. $this->assertEquals(__('Sunday'), $ret);
  408. }
  409. /**
  410. * TimeLibTest::testMonth()
  411. *
  412. * @return void
  413. */
  414. public function testMonth() {
  415. $this->out($this->_header(__FUNCTION__), true);
  416. $ret = TimeLib::month('11');
  417. $this->assertEquals(__('November'), $ret);
  418. $ret = TimeLib::month(1);
  419. $this->assertEquals(__('January'), $ret);
  420. $ret = TimeLib::month(2, true, array('appendDot' => true));
  421. $this->assertEquals(__('Feb') . '.', $ret);
  422. $ret = TimeLib::month(5, true, array('appendDot' => true));
  423. $this->assertEquals(__('May'), $ret);
  424. }
  425. /**
  426. * TimeLibTest::testDays()
  427. *
  428. * @return void
  429. */
  430. public function testDays() {
  431. $this->out($this->_header(__FUNCTION__), true);
  432. $ret = TimeLib::days();
  433. $this->assertTrue(count($ret) === 7);
  434. }
  435. /**
  436. * TimeLibTest::testMonths()
  437. *
  438. * @return void
  439. */
  440. public function testMonths() {
  441. $this->out($this->_header(__FUNCTION__), true);
  442. $ret = TimeLib::months();
  443. $this->assertTrue(count($ret) === 12);
  444. }
  445. /**
  446. * TimeLibTest::testRelLengthOfTime()
  447. *
  448. * @return void
  449. */
  450. public function testRelLengthOfTime() {
  451. $ret = TimeLib::relLengthOfTime('1990-11-20');
  452. //pr($ret);
  453. $ret = TimeLib::relLengthOfTime('2012-11-20');
  454. //pr($ret);
  455. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
  456. //pr($res);
  457. $this->assertTrue(!empty($res));
  458. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR), null, array('plural' => 'n'));
  459. //pr($res);
  460. //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__('Hours'));
  461. $this->assertEquals(__('%s ago', '4 ' . __('Days') . ', ' . '5 ' . __('Hours')), $res);
  462. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time() + 4 * DAY + 5 * HOUR), null, array('plural' => 'n'));
  463. //pr($res);
  464. $this->assertEquals(__('In %s', '4 ' . __('Days') . ', ' . '5 ' . __('Hours')), $res);
  465. $res = TimeLib::relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, array('plural' => 'n'));
  466. //pr($res);
  467. $this->assertEquals($res, __('justNow'));
  468. }
  469. /**
  470. * Test cweek
  471. *
  472. * @return void
  473. */
  474. public function testCweek() {
  475. $year = 2008;
  476. $month = 12;
  477. $day = 29;
  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 = 1;
  483. $date = mktime(0, 0, 0, $month, $day, $year);
  484. $this->assertEquals('01/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  485. $year = 2009;
  486. $month = 1;
  487. $day = 9;
  488. $date = mktime(0, 0, 0, $month, $day, $year);
  489. $this->assertEquals('02/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day . ' 00:00:00'));
  490. $year = 2009;
  491. $month = 12;
  492. $day = 26;
  493. $date = mktime(0, 0, 0, $month, $day, $year);
  494. $this->assertEquals('52/' . $year, TimeLib::cweek($year . '-' . $month . '-' . $day));
  495. }
  496. /**
  497. * Test IsInTheFuture
  498. *
  499. * @return void
  500. */
  501. public function testIsInTheFuture() {
  502. $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
  503. $is = TimeLib::isInTheFuture($testDate);
  504. $this->assertTrue($is);
  505. $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
  506. $is = TimeLib::isInTheFuture($testDate);
  507. $this->assertFalse($is);
  508. }
  509. /**
  510. * Test IsNotTodayAndInTheFuture
  511. *
  512. * @return void
  513. */
  514. public function testIsNotTodayAndInTheFuture() {
  515. $testDate = date(FORMAT_DB_DATE, time());
  516. $is = TimeLib::isNotTodayAndInTheFuture($testDate);
  517. $this->assertFalse($is);
  518. $testDate = date(FORMAT_DB_DATETIME, time() + 1 * DAY);
  519. $is = TimeLib::isNotTodayAndInTheFuture($testDate);
  520. $this->assertTrue($is);
  521. }
  522. /**
  523. * Test IsDayAfterTomorrow
  524. *
  525. * @return void
  526. */
  527. public function testIsDayAfterTomorrow() {
  528. $testDate = date(FORMAT_DB_DATE, time() + 2 * DAY);
  529. $is = TimeLib::isDayAfterTomorrow($testDate);
  530. $this->assertTrue($is);
  531. $testDate = date(FORMAT_DB_DATETIME, time() - 1 * MINUTE);
  532. $is = TimeLib::isDayAfterTomorrow($testDate);
  533. $this->assertFalse($is);
  534. }
  535. /**
  536. * TimeLibTest::testLengthOfTime()
  537. *
  538. * @return void
  539. */
  540. public function testLengthOfTime() {
  541. $this->out($this->_header(__FUNCTION__), true);
  542. $ret = TimeLib::lengthOfTime(60);
  543. //pr($ret);
  544. // FIX ME! Doesn't work!
  545. $ret = TimeLib::lengthOfTime(-60);
  546. //pr($ret);
  547. $ret = TimeLib::lengthOfTime(-121);
  548. //pr($ret);
  549. $this->assertEquals('6 ' . __('Minutes') . ', 40 ' . __('Seconds'), TimeLib::lengthOfTime(400));
  550. $res = TimeLib::lengthOfTime(400, 'i');
  551. //pr($res);
  552. $this->assertEquals('6 ' . __('Minutes'), $res);
  553. $res = TimeLib::lengthOfTime(6 * DAY);
  554. //pr($res);
  555. $this->assertEquals('6 ' . __('Days') . ', 0 ' . __('Hours'), $res);
  556. }
  557. /**
  558. * TimeLibTest::testFuzzyFromOffset()
  559. *
  560. * @return void
  561. */
  562. public function testFuzzyFromOffset() {
  563. $this->out($this->_header(__FUNCTION__), true);
  564. $ret = TimeLib::fuzzyFromOffset(MONTH);
  565. //pr($ret);
  566. $ret = TimeLib::fuzzyFromOffset(120);
  567. //pr($ret);
  568. $ret = TimeLib::fuzzyFromOffset(DAY);
  569. //pr($ret);
  570. $ret = TimeLib::fuzzyFromOffset(DAY + 2 * MINUTE);
  571. //pr($ret);
  572. // FIX ME! Doesn't work!
  573. $ret = TimeLib::fuzzyFromOffset(-DAY);
  574. //pr($ret);
  575. }
  576. /**
  577. * TimeLibTest::testCweekMod()
  578. *
  579. * @return void
  580. */
  581. public function testCweekMod() {
  582. $result = TimeLib::cWeekMod(0);
  583. $this->assertEquals(0, $result);
  584. $result = TimeLib::cWeekMod(1);
  585. $this->assertEquals(1, $result);
  586. $result = TimeLib::cWeekMod(6);
  587. $this->assertEquals(0, $result);
  588. }
  589. /**
  590. * TimeLibTest::testTimezoneByCoordinates()
  591. *
  592. * @return void
  593. */
  594. public function testTimezoneByCoordinates() {
  595. $result = TimeLib::timezoneByCoordinates(48, 11);
  596. $this->assertEquals('Europe/Vaduz', $result);
  597. }
  598. /**
  599. * TimeLibTest::testCweekDay()
  600. *
  601. * @return void
  602. */
  603. public function testCweekDay() {
  604. $this->out($this->_header(__FUNCTION__), true);
  605. // wednesday
  606. $ret = TimeLib::cweekDay(51, 2011, 2);
  607. $this->out('51, 2011, 2');
  608. $this->out(date(FORMAT_DB_DATETIME, $ret));
  609. $this->assertTrue($ret >= 1324422000 && $ret <= 1324425600);
  610. //$this->assertEquals(1324422000, $ret);
  611. }
  612. public function testCweeks() {
  613. $this->out($this->_header(__FUNCTION__), true);
  614. $ret = TimeLib::cweeks('2004');
  615. $this->assertEquals(53, $ret);
  616. $ret = TimeLib::cweeks('2010');
  617. $this->assertEquals(52, $ret);
  618. $ret = TimeLib::cweeks('2006');
  619. $this->assertEquals(52, $ret);
  620. $ret = TimeLib::cweeks('2007');
  621. $this->assertEquals(52, $ret);
  622. /*
  623. for ($i = 1990; $i < 2020; $i++) {
  624. $this->out(TimeLib::cweeks($i).BR;
  625. }
  626. */
  627. }
  628. public function testCweekBeginning() {
  629. $this->out($this->_header(__FUNCTION__), true);
  630. $values = array(
  631. '2001' => 978303600, # Mon 01.01.2001, 00:00
  632. '2006' => 1136156400, # Mon 02.01.2006, 00:00
  633. '2010' => 1262559600, # Mon 04.01.2010, 00:00
  634. '2013' => 1356908400, # Mon 31.12.2012, 00:00
  635. );
  636. foreach ($values as $year => $expected) {
  637. $ret = TimeLib::cweekBeginning($year);
  638. $this->out($ret);
  639. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  640. //$this->assertEquals($ret, $expected, null, $year);
  641. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  642. }
  643. $values = array(
  644. array('2001', '1', 978303600), # Mon 01.01.2001, 00:00:00
  645. array('2001', '2', 978908400), # Mon 08.01.2001, 00:00:00
  646. array('2001', '5', 980722800), # Mon 29.01.2001, 00:00:00
  647. array('2001', '52', 1009148400), # Mon 24.12.2001, 00:00:00
  648. array('2013', '11', 1362956400), # Mon 11.03.2013, 00:00:00
  649. array('2006', '3', 1137366000), # Mon 16.01.2006, 00:00:00
  650. );
  651. foreach ($values as $v) {
  652. $ret = TimeLib::cweekBeginning($v[0], $v[1]);
  653. $this->out($ret);
  654. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  655. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  656. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  657. }
  658. }
  659. public function testCweekEnding() {
  660. $this->out($this->_header(__FUNCTION__), true);
  661. $values = array(
  662. '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
  663. '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
  664. '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
  665. '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
  666. );
  667. foreach ($values as $year => $expected) {
  668. $ret = TimeLib::cweekEnding($year);
  669. $this->out($ret);
  670. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  671. //$this->assertSame($expected, $ret);
  672. $this->assertTrue($ret <= $expected + HOUR && $ret >= $expected);
  673. }
  674. $values = array(
  675. array('2001', '1', 978908399), # Sun 07.01.2001, 23:59:59
  676. array('2001', '2', 979513199), # Sun 14.01.2001, 23:59:59
  677. array('2001', '5', 981327599), # Sun 04.02.2001, 23:59:59
  678. array('2001', '52', 1009753199), # Sun 30.12.2001, 23:59:59
  679. array('2013', '11', 1363561199), # Sun 17.03.2013, 23:59:59
  680. array('2006', '3', 1137970799), # Sun 22.01.2006, 23:59:59
  681. );
  682. foreach ($values as $v) {
  683. $ret = TimeLib::cweekEnding($v[0], $v[1]);
  684. $this->out($ret);
  685. $this->out(TimeLib::niceDate($ret, 'D') . ' ' . TimeLib::niceDate($ret, FORMAT_NICE_YMDHMS));
  686. //$this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  687. $this->assertTrue($ret <= $v[2] + HOUR && $ret >= $v[2]);
  688. }
  689. }
  690. /**
  691. * TimeLibTest::testAgeByHoroscop()
  692. *
  693. * @return void
  694. */
  695. public function testAgeByHoroscop() {
  696. App::uses('ZodiacLib', 'Tools.Misc');
  697. $this->skipIf(php_sapi_name() === 'cli', 'Fix these tests');
  698. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
  699. // between xxxx-08-24 and xxxx-09-23 the latter, otherwise the first:
  700. $this->assertEquals(date('Y') - 2000 - 1, $is);
  701. $this->assertEquals(array(date('Y') - 2000 - 1, date('Y') - 2000), $is);
  702. $is = TimeLib::ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
  703. //pr($is);
  704. $this->assertEquals(date('Y') - 1991 - 1, $is);
  705. $is = TimeLib::ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
  706. //pr($is);
  707. $this->assertEquals(array(date('Y') - 1986 - 1, date('Y') - 1986), $is);
  708. $is = TimeLib::ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
  709. //debug($is);
  710. $this->assertEquals(date('Y') - 2000 - 1, $is); //array(10, 11)
  711. }
  712. /**
  713. * TimeLibTest::testAgeRange()
  714. *
  715. * @return void
  716. */
  717. public function testAgeRange() {
  718. $is = TimeLib::ageRange(2000);
  719. $this->assertEquals(date('Y') - 2000 - 1, $is);
  720. $is = TimeLib::ageRange(date('Y') - 11, null, null, 5);
  721. $this->assertEquals(array(6, 10), $is);
  722. $is = TimeLib::ageRange(date('Y') - 13, null, null, 5);
  723. $this->assertEquals(array(11, 15), $is);
  724. $is = TimeLib::ageRange(1985, 23, 11);
  725. $this->assertEquals(date('Y') - 1985 - 1, $is);
  726. $is = TimeLib::ageRange(date('Y') - 29, null, null, 6);
  727. $this->assertEquals(array(25, 30), $is);
  728. $is = TimeLib::ageRange(date('Y') - 29, 21, 11, 7);
  729. $this->assertEquals(array(22, 28), $is);
  730. }
  731. /**
  732. * TimeLibTest::testParseDate()
  733. *
  734. * @return void
  735. */
  736. public function testParseDate() {
  737. //echo $this->_header(__FUNCTION__);
  738. $tests = array(
  739. '2010-12-11' => 1292022000,
  740. '2010-01-02' => 1262386800,
  741. '10-01-02' => 1262386800,
  742. '2.1.2010' => 1262386800,
  743. '2.1.10' => 1262386800,
  744. '02.01.10' => 1262386800,
  745. '02.01.2010' => 1262386800,
  746. '02.01.2010 22:11' => 1262386800,
  747. '2010-01-02 22:11' => 1262386800,
  748. );
  749. foreach ($tests as $was => $expected) {
  750. $is = TimeLib::parseDate($was);
  751. $this->assertTrue($is <= $expected + HOUR && $is >= $expected);
  752. }
  753. }
  754. /**
  755. * TimeLibTest::testParseTime()
  756. *
  757. * @return void
  758. */
  759. public function testParseTime() {
  760. //echo $this->_header(__FUNCTION__);
  761. $tests = array(
  762. '2:4' => 7440,
  763. '2:04' => 7440,
  764. '2' => 7200,
  765. '1,5' => 3600 + 1800,
  766. '1.5' => 3600 + 1800,
  767. '1.50' => 3600 + 1800,
  768. '1.01' => 3660,
  769. ':4' => 240,
  770. ':04' => 240,
  771. ':40' => 40 * MINUTE,
  772. '1:2:4' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  773. '01:2:04' => 1 * HOUR + 2 * MINUTE + 4 * SECOND,
  774. '0:2:04' => 2 * MINUTE + 4 * SECOND,
  775. '::4' => 4 * SECOND,
  776. '::04' => 4 * SECOND,
  777. '::40' => 40 * SECOND,
  778. '2011-11-12 10:10:10' => 10 * HOUR + 10 * MINUTE + 10 * SECOND,
  779. );
  780. // positive
  781. foreach ($tests as $was => $expected) {
  782. $is = TimeLib::parseTime($was);
  783. //pr($is);
  784. $this->assertEquals($expected, $is);
  785. }
  786. unset($tests['2011-11-12 10:10:10']);
  787. // negative
  788. foreach ($tests as $was => $expected) {
  789. $is = TimeLib::parseTime('-' . $was);
  790. //pr($is);
  791. $this->assertEquals(-$expected, $is);
  792. }
  793. }
  794. /**
  795. * TimeLibTest::testBuildTime()
  796. *
  797. * @return void
  798. */
  799. public function testBuildTime() {
  800. //echo $this->_header(__FUNCTION__);
  801. $tests = array(
  802. 7440 => '2:04',
  803. 7220 => '2:00', # 02:00:20 => rounded to 2:00:00
  804. 5400 => '1:30',
  805. 3660 => '1:01',
  806. );
  807. // positive
  808. foreach ($tests as $was => $expected) {
  809. $is = TimeLib::buildTime($was);
  810. //pr($is);
  811. $this->assertEquals($expected, $is);
  812. }
  813. // negative
  814. foreach ($tests as $was => $expected) {
  815. $is = TimeLib::buildTime(-$was);
  816. //pr($is);
  817. $this->assertEquals('-' . $expected, $is);
  818. }
  819. }
  820. /**
  821. * TimeLibTest::testBuildDefaultTime()
  822. *
  823. * @return void
  824. */
  825. public function testBuildDefaultTime() {
  826. //echo $this->_header(__FUNCTION__);
  827. $tests = array(
  828. 7440 => '02:04:00',
  829. 7220 => '02:00:20',
  830. 5400 => '01:30:00',
  831. 3660 => '01:01:00',
  832. 1 * HOUR + 2 * MINUTE + 4 * SECOND => '01:02:04',
  833. );
  834. foreach ($tests as $was => $expected) {
  835. $is = TimeLib::buildDefaultTime($was);
  836. //pr($is);
  837. $this->assertEquals($expected, $is);
  838. }
  839. }
  840. /**
  841. * 9.30 => 9.50
  842. *
  843. * @return void
  844. */
  845. public function testStandardDecimal() {
  846. //echo $this->_header(__FUNCTION__);
  847. $value = '9.30';
  848. $is = TimeLib::standardToDecimalTime($value);
  849. $this->assertEquals('9.50', round($is, 2));
  850. $value = '9.3';
  851. $is = TimeLib::standardToDecimalTime($value);
  852. $this->assertEquals('9.50', round($is, 2));
  853. }
  854. /**
  855. * 9.50 => 9.30
  856. *
  857. * @return void
  858. */
  859. public function testDecimalStandard() {
  860. //echo $this->_header(__FUNCTION__);
  861. $value = '9.50';
  862. $is = TimeLib::decimalToStandardTime($value);
  863. $this->assertEquals('9.3', round($is, 2));
  864. $value = '9.5';
  865. $is = TimeLib::decimalToStandardTime($value);
  866. //pr($is);
  867. $this->assertEquals('9.3', $is);
  868. $is = TimeLib::decimalToStandardTime($value, 2);
  869. //pr($is);
  870. $this->assertEquals('9.30', $is);
  871. $is = TimeLib::decimalToStandardTime($value, 2, ':');
  872. //pr($is);
  873. $this->assertEquals('9:30', $is);
  874. }
  875. /**
  876. * TimeLibTest::testHasDaylightSavingTime()
  877. *
  878. * @return void
  879. */
  880. public function testHasDaylightSavingTime() {
  881. $timezone = 'Europe/Berlin';
  882. $x = TimeLib::hasDaylightSavingTime($timezone);
  883. $this->assertTrue($x);
  884. $timezone = 'Asia/Baghdad';
  885. $x = TimeLib::hasDaylightSavingTime($timezone);
  886. $this->assertFalse($x);
  887. }
  888. /**
  889. * TimeLibTest::testTimezone()
  890. *
  891. * @return void
  892. */
  893. public function testTimezone() {
  894. $timezone = TimeLib::timezone();
  895. // usually UTC
  896. $name = $timezone->getName();
  897. $this->debug($name);
  898. $this->assertTrue(!empty($name));
  899. $location = $timezone->getLocation();
  900. $this->debug($location);
  901. $this->assertTrue(!empty($location['country_code']));
  902. $this->assertTrue(isset($location['latitude']));
  903. $this->assertTrue(isset($location['longitude']));
  904. $offset = $timezone->getOffset(new DateTime('@' . mktime(0, 0, 0, 1, 1, date('Y'))));
  905. $this->debug($offset);
  906. $phpTimezone = date_default_timezone_get();
  907. $this->assertEquals($name, $phpTimezone);
  908. }
  909. /**
  910. * TimeLibTest::testGetGmtOffset()
  911. *
  912. * @return void
  913. */
  914. public function testGetGmtOffset() {
  915. $timezone = Configure::read('Config.timezone');
  916. $phpTimezone = date_default_timezone_get();
  917. Configure::write('Config.timezone', 'UTC');
  918. date_default_timezone_set('UTC');
  919. $result = TimeLib::getGmtOffset();
  920. $this->assertEquals(0, $result);
  921. $result = TimeLib::getGmtOffset('Europe/Berlin');
  922. $this->assertTrue($result > 0, $result);
  923. $result = TimeLib::getGmtOffset('America/Los_Angeles');
  924. $this->assertTrue($result < 0, $result);
  925. Configure::write('Config.timezone', $timezone);
  926. date_default_timezone_set($phpTimezone);
  927. }
  928. /**
  929. * DatetimeHelperTest::testTzOffset()
  930. *
  931. * @return void
  932. */
  933. public function testTzOffset() {
  934. $timezone = Configure::read('Config.timezone');
  935. $phpTimezone = date_default_timezone_get();
  936. Configure::write('Config.timezone', 'UTC');
  937. date_default_timezone_set('UTC');
  938. $result = TimeLib::tzOffset(0, false);
  939. $this->assertEquals(0, $result);
  940. Configure::write('Config.timezone', 'Europe/Berlin');
  941. date_default_timezone_set('Europe/Berlin');
  942. $result = TimeLib::tzOffset(2 * HOUR, false);
  943. $this->assertEquals(0, $result);
  944. Configure::write('Config.timezone', $timezone);
  945. date_default_timezone_set($phpTimezone);
  946. }
  947. }