TimeTest.php 32 KB

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