TimeTest.php 32 KB

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