TimeLibTest.php 32 KB

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