DatetimeLibTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. App::uses('DatetimeLib', 'Tools.Utility');
  3. App::uses('MyCakeTestCase', 'Tools.Lib');
  4. class DatetimeLibTest extends MyCakeTestCase {
  5. public $Datetime;
  6. public function setUp() {
  7. $this->Datetime = new DatetimeLib();
  8. $this->assertTrue(is_object($this->Datetime));
  9. }
  10. public function tearDown() {
  11. unset($this->Datetime);
  12. }
  13. public function testNiceDate() {
  14. $res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
  15. echo returns($res);
  16. $values = array(
  17. array('2009-12-01 00:00:00', FORMAT_NICE_YMD, '01.12.2009'),
  18. array('2009-12-01 00:00:00', FORMAT_NICE_M_FULL, 'December'),
  19. );
  20. foreach ($values as $v) {
  21. $ret = $this->Datetime->niceDate($v[0], $v[1]);
  22. pr($ret); ob_flush();
  23. $this->assertEquals($ret, $v[2]);
  24. }
  25. }
  26. public function testLocalDate() {
  27. $res = setlocale(LC_TIME, array('de_DE.UTF-8', 'deu_deu'));
  28. echo returns($res);
  29. $values = array(
  30. array('2009-12-01 00:00:00', FORMAT_LOCAL_YMD, '01.12.2009'),
  31. array('2009-12-01 00:00:00', FORMAT_LOCAL_M_FULL, 'Dezember'),
  32. );
  33. foreach ($values as $v) {
  34. $ret = $this->Datetime->localDate($v[0], $v[1]);
  35. pr($ret); ob_flush();
  36. $this->assertEquals($ret, $v[2]);
  37. }
  38. }
  39. public function testParseLocalizedDate() {
  40. $this->out($this->_header(__FUNCTION__));
  41. $ret = $this->Datetime->parseLocalizedDate('15-Feb-2009', 'j-M-Y', 'start');
  42. pr($ret);
  43. $this->assertEquals($ret, '2009-02-15 00:00:00');
  44. # problem when not passing months or days as well - no way of knowing how exact the date was
  45. $ret = $this->Datetime->parseLocalizedDate('2009', 'Y', 'start');
  46. pr($ret);
  47. //$this->assertEquals($ret, '2009-01-01 00:00:00');
  48. $ret = $this->Datetime->parseLocalizedDate('Feb 2009', 'M Y', 'start');
  49. pr($ret);
  50. //$this->assertEquals($ret, '2009-02-01 00:00:00');
  51. $values = array(
  52. array(__('Today'), array(date(FORMAT_DB_DATETIME, mktime(0, 0, 0, date('m'), date('d'), date('Y'))), date(FORMAT_DB_DATETIME, mktime(23, 59, 59, date('m'), date('d'), date('Y'))))),
  53. array('2010', array('2010-01-01 00:00:00', '2010-12-31 23:59:59')),
  54. array('23.02.2011', array('2011-02-23 00:00:00', '2011-02-23 23:59:59')),
  55. array('22/02/2011', array('2011-02-22 00:00:00', '2011-02-22 23:59:59')),
  56. array('3/2/11', array('2011-02-03 00:00:00', '2011-02-03 23:59:59')),
  57. array('2/12/9', array('2009-12-02 00:00:00', '2009-12-02 23:59:59')),
  58. array('12/2009', array('2009-12-01 00:00:00', '2009-12-31 23:59:59')),
  59. );
  60. foreach ($values as $v) {
  61. $ret = $this->Datetime->parseLocalizedDate($v[0], null, 'start');
  62. pr($ret);
  63. $this->assertEquals($ret, $v[1][0]);
  64. $ret = $this->Datetime->parseLocalizedDate($v[0], null, 'end');
  65. pr($ret);
  66. $this->assertEquals($ret, $v[1][1]);
  67. }
  68. }
  69. public function testPeriod() {
  70. $this->out($this->_header(__FUNCTION__));
  71. $values = array(
  72. array(__('Today'), array(date(FORMAT_DB_DATETIME, mktime(0, 0, 0, date('m'), date('d'), date('Y'))), date(FORMAT_DB_DATETIME, mktime(23, 59, 59, date('m'), date('d'), date('Y'))))),
  73. array('2010', array('2010-01-01 00:00:00', '2010-12-31 23:59:59')),
  74. array('2011-02', array('2011-02-01 00:00:00', '2011-02-28 23:59:59')),
  75. array('2012-02', array('2012-02-01 00:00:00', '2012-02-29 23:59:59')),
  76. array('2010-02-23', array('2010-02-23 00:00:00', '2010-02-23 23:59:59')),
  77. array('2010-02-23 bis 2010-02-26', array('2010-02-23 00:00:00', '2010-02-26 23:59:59')),
  78. //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')),
  79. # localized
  80. array('23.02.2011', array('2011-02-23 00:00:00', '2011-02-23 23:59:59')),
  81. array('23.2.2010 bis 26.2.2011', array('2010-02-23 00:00:00', '2011-02-26 23:59:59')),
  82. );
  83. foreach ($values as $v) {
  84. $ret = $this->Datetime->period($v[0]);
  85. pr($ret);
  86. $this->assertEquals($ret, $v[1]);
  87. }
  88. }
  89. public function testPeriodAsSql() {
  90. $this->out($this->_header(__FUNCTION__));
  91. $values = array(
  92. array(__('Today'), "(Model.field >= '".date(FORMAT_DB_DATE)." 00:00:00') AND (Model.field <= '".date(FORMAT_DB_DATE)." 23:59:59')"),
  93. array(__('Yesterday').' '.__('until').' '.__('Today'), "(Model.field >= '".date(FORMAT_DB_DATE, time()-DAY)." 00:00:00') AND (Model.field <= '".date(FORMAT_DB_DATE)." 23:59:59')"),
  94. array(__('Today').' '.__('until').' '.__('Tomorrow'), "(Model.field >= '".date(FORMAT_DB_DATE, time())." 00:00:00') AND (Model.field <= '".date(FORMAT_DB_DATE, time()+DAY)." 23:59:59')"),
  95. array(__('Yesterday').' '.__('until').' '.__('Tomorrow'), "(Model.field >= '".date(FORMAT_DB_DATE, time()-DAY)." 00:00:00') AND (Model.field <= '".date(FORMAT_DB_DATE, time()+DAY)." 23:59:59')"),
  96. );
  97. foreach ($values as $v) {
  98. $ret = $this->Datetime->periodAsSql($v[0], 'Model.field');
  99. pr($v[1]);
  100. pr($ret); ob_flush();
  101. $this->assertSame($v[1], $ret);
  102. }
  103. }
  104. public function testDifference() {
  105. $this->out($this->_header(__FUNCTION__));
  106. $values = array(
  107. array('2010-02-23 11:11:11', '2010-02-23 11:12:01', 50),
  108. array('2010-02-23 11:11:11', '2010-02-24 11:12:01', DAY+50)
  109. );
  110. foreach ($values as $v) {
  111. $ret = $this->Datetime->difference($v[0], $v[1]);
  112. $this->assertEquals($v[2], $ret);
  113. }
  114. }
  115. public function testAgeBounds() {
  116. $this->out($this->_header(__FUNCTION__));
  117. $values = array(
  118. array(20, 20, array('min'=>'1990-07-07', 'max'=>'1991-07-06')),
  119. array(10, 30, array('min'=>'1980-07-07', 'max'=>'2001-07-06')),
  120. array(11, 12, array('min'=>'1998-07-07', 'max'=>'2000-07-06'))
  121. );
  122. foreach ($values as $v) {
  123. echo $v[0].'/'.$v[1];
  124. $ret = $this->Datetime->ageBounds($v[0], $v[1], true, '2011-07-06'); //TODO: relative time
  125. pr($ret);
  126. if (isset($v[2])) {
  127. $this->assertSame($v[2], $ret);
  128. pr($this->Datetime->age($v[2]['min']));
  129. pr($this->Datetime->age($v[2]['max']));
  130. $this->assertEquals($v[0], $this->Datetime->age($v[2]['max']));
  131. $this->assertEquals($v[1], $this->Datetime->age($v[2]['min']));
  132. }
  133. }
  134. }
  135. public function testAgeByYear() {
  136. $this->out($this->_header(__FUNCTION__));
  137. # year only
  138. $is = $this->Datetime->ageByYear(2000);
  139. $this->out($is);
  140. $this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000));
  141. $is = $this->Datetime->ageByYear(1985);
  142. $this->assertEquals($is, (date('Y')-1986).'/'.(date('Y')-1985));
  143. # with month
  144. if (($month = date('n')+1) <= 12) {
  145. $is = $this->Datetime->ageByYear(2000, $month);
  146. $this->out($is);
  147. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  148. $this->assertSame($is, (date('Y')-2001), null, '2000/'.$month);
  149. }
  150. if (($month = date('n')-1) >= 1) {
  151. $is = $this->Datetime->ageByYear(2000, $month);
  152. $this->out($is);
  153. //$this->assertEquals($is, (date('Y')-2001).'/'.(date('Y')-2000), null, '2000/'.$month);
  154. $this->assertSame($is, (date('Y')-2000), null, '2000/'.$month);
  155. }
  156. }
  157. public function testDaysInMonth() {
  158. $this->out($this->_header(__FUNCTION__));
  159. $ret = $this->Datetime->daysInMonth('2004', '3');
  160. $this->assertEquals($ret, 31);
  161. $ret = $this->Datetime->daysInMonth('2006', '4');
  162. $this->assertEquals($ret, 30);
  163. $ret = $this->Datetime->daysInMonth('2007', '2');
  164. $this->assertEquals($ret, 28);
  165. $ret = $this->Datetime->daysInMonth('2008', '2');
  166. $this->assertEquals($ret, 29);
  167. }
  168. public function testDay() {
  169. $this->out($this->_header(__FUNCTION__));
  170. $ret = $this->Datetime->day('0');
  171. $this->assertEquals(__('Sunday'), $ret);
  172. $ret = $this->Datetime->day(2, true);
  173. $this->assertEquals(__('Tue'), $ret);
  174. $ret = $this->Datetime->day(6);
  175. $this->assertEquals(__('Saturday'), $ret);
  176. $ret = $this->Datetime->day(6, false, 1);
  177. $this->assertEquals(__('Sunday'), $ret);
  178. $ret = $this->Datetime->day(0, false, 2);
  179. $this->assertEquals(__('Tuesday'), $ret);
  180. $ret = $this->Datetime->day(1, false, 6);
  181. $this->assertEquals(__('Sunday'), $ret);
  182. }
  183. public function testMonth() {
  184. $this->out($this->_header(__FUNCTION__));
  185. $ret = $this->Datetime->month('11');
  186. $this->assertEquals(__('November'), $ret);
  187. $ret = $this->Datetime->month(1);
  188. $this->assertEquals(__('January'), $ret);
  189. $ret = $this->Datetime->month(2, true, array('appendDot'=>true));
  190. $this->assertEquals(__('Feb').'.', $ret);
  191. $ret = $this->Datetime->month(5, true, array('appendDot'=>true));
  192. $this->assertEquals(__('May'), $ret);
  193. }
  194. public function testDays() {
  195. $this->out($this->_header(__FUNCTION__));
  196. $ret = $this->Datetime->days();
  197. $this->assertTrue(count($ret) === 7);
  198. }
  199. public function testMonths() {
  200. $this->out($this->_header(__FUNCTION__));
  201. $ret = $this->Datetime->months();
  202. $this->assertTrue(count($ret) === 12);
  203. }
  204. public function testRelLengthOfTime() {
  205. $this->out($this->_header(__FUNCTION__));
  206. $ret = $this->Datetime->relLengthOfTime('1990-11-20');
  207. pr($ret);
  208. $ret = $this->Datetime->relLengthOfTime('2012-11-20');
  209. pr($ret);
  210. }
  211. public function testLengthOfTime() {
  212. $this->out($this->_header(__FUNCTION__));
  213. $ret = $this->Datetime->lengthOfTime(60);
  214. pr($ret);
  215. # FIX ME! Doesn't work!
  216. $ret = $this->Datetime->lengthOfTime(-60);
  217. pr($ret);
  218. $ret = $this->Datetime->lengthOfTime(-121);
  219. pr($ret);
  220. }
  221. public function testFuzzyFromOffset() {
  222. $this->out($this->_header(__FUNCTION__));
  223. $ret = $this->Datetime->fuzzyFromOffset(MONTH);
  224. pr($ret);
  225. $ret = $this->Datetime->fuzzyFromOffset(120);
  226. pr($ret);
  227. $ret = $this->Datetime->fuzzyFromOffset(DAY);
  228. pr($ret);
  229. $ret = $this->Datetime->fuzzyFromOffset(DAY+2*MINUTE);
  230. pr($ret);
  231. # FIX ME! Doesn't work!
  232. $ret = $this->Datetime->fuzzyFromOffset(-DAY);
  233. pr($ret);
  234. }
  235. public function testCweekMod() {
  236. }
  237. public function testCweekDay() {
  238. $this->out($this->_header(__FUNCTION__));
  239. # wednesday
  240. $ret = $this->Datetime->cweekDay(51, 2011, 2);
  241. $this->out('51, 2011, 2');
  242. $this->out(date(FORMAT_DB_DATETIME, $ret));
  243. $this->assertEquals(1324422000, $ret);
  244. }
  245. public function testCweeks() {
  246. $this->out($this->_header(__FUNCTION__));
  247. $ret = $this->Datetime->cweeks('2004');
  248. $this->assertEquals($ret, 53);
  249. $ret = $this->Datetime->cweeks('2010');
  250. $this->assertEquals($ret, 52);
  251. $ret = $this->Datetime->cweeks('2006');
  252. $this->assertEquals($ret, 52);
  253. $ret = $this->Datetime->cweeks('2007');
  254. $this->assertEquals($ret, 52);
  255. /*
  256. for ($i = 1990; $i < 2020; $i++) {
  257. $this->out($this->Datetime->cweeks($i).BR;
  258. }
  259. */
  260. }
  261. public function testCweekBeginning() {
  262. $this->out($this->_header(__FUNCTION__));
  263. $values = array(
  264. '2001' => 978303600, # Mon 01.01.2001, 00:00
  265. '2006' => 1136156400, # Mon 02.01.2006, 00:00
  266. '2010' => 1262559600, # Mon 04.01.2010, 00:00
  267. '2013' => 1356908400, # Mon 31.12.2012, 00:00
  268. );
  269. foreach ($values as $year => $expected) {
  270. $ret = $this->Datetime->cweekBeginning($year);
  271. $this->out($ret);
  272. $this->out($this->Datetime->niceDate($ret, 'D').' '.$this->Datetime->niceDate($ret, FORMAT_NICE_YMDHMS));
  273. $this->assertEquals($ret, $expected, null, $year);
  274. }
  275. $values = array(
  276. array('2001', '1', 978303600), # Mon 01.01.2001, 00:00:00
  277. array('2001', '2', 978908400), # Mon 08.01.2001, 00:00:00
  278. array('2001', '5', 980722800), # Mon 29.01.2001, 00:00:00
  279. array('2001', '52', 1009148400), # Mon 24.12.2001, 00:00:00
  280. array('2013', '11', 1362956400), # Mon 11.03.2013, 00:00:00
  281. array('2006', '3', 1137366000), # Mon 16.01.2006, 00:00:00
  282. );
  283. foreach ($values as $v) {
  284. $ret = $this->Datetime->cweekBeginning($v[0], $v[1]);
  285. $this->out($ret);
  286. $this->out($this->Datetime->niceDate($ret, 'D').' '.$this->Datetime->niceDate($ret, FORMAT_NICE_YMDHMS));
  287. $this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  288. }
  289. }
  290. public function testCweekEnding() {
  291. $this->out($this->_header(__FUNCTION__));
  292. $values = array(
  293. '2001' => 1009753199, # Sun 30.12.2001, 23:59:59
  294. '2006' => 1167605999, # Sun 31.12.2006, 23:59:59
  295. '2010' => 1294009199, # Sun 02.01.2011, 23:59:59
  296. '2013' => 1388357999, # Sun 29.12.2013, 23:59:59
  297. );
  298. foreach ($values as $year => $expected) {
  299. $ret = $this->Datetime->cweekEnding($year);
  300. $this->out($ret);
  301. $this->out($this->Datetime->niceDate($ret, 'D').' '.$this->Datetime->niceDate($ret, FORMAT_NICE_YMDHMS));
  302. $this->assertSame($ret, $expected);
  303. }
  304. $values = array(
  305. array('2001', '1', 978908399), # Sun 07.01.2001, 23:59:59
  306. array('2001', '2', 979513199), # Sun 14.01.2001, 23:59:59
  307. array('2001', '5', 981327599), # Sun 04.02.2001, 23:59:59
  308. array('2001', '52', 1009753199), # Sun 30.12.2001, 23:59:59
  309. array('2013', '11', 1363561199), # Sun 17.03.2013, 23:59:59
  310. array('2006', '3', 1137970799), # Sun 22.01.2006, 23:59:59
  311. );
  312. foreach ($values as $v) {
  313. $ret = $this->Datetime->cweekEnding($v[0], $v[1]);
  314. $this->out($ret);
  315. $this->out($this->Datetime->niceDate($ret, 'D').' '.$this->Datetime->niceDate($ret, FORMAT_NICE_YMDHMS));
  316. $this->assertSame($v[2], $ret, null, $v[1].'/'.$v[0]);
  317. }
  318. }
  319. public function testAgeByHoroscop() {
  320. App::uses('ZodiacLib', 'Tools.Misc');
  321. $zodiac = new ZodiacLib();
  322. $is = $this->Datetime->ageByHoroscope(2000, ZodiacLib::SIGN_VIRGO);
  323. pr($is);
  324. $this->assertEquals($is, 11);
  325. $is = $this->Datetime->ageByHoroscope(1991, ZodiacLib::SIGN_LIBRA);
  326. pr($is);
  327. $this->assertEquals($is, 20);
  328. $is = $this->Datetime->ageByHoroscope(1986, ZodiacLib::SIGN_CAPRICORN);
  329. pr($is);
  330. $this->assertEquals($is, array(24, 25));
  331. $is = $this->Datetime->ageByHoroscope(2000, ZodiacLib::SIGN_SCORPIO);
  332. pr($is);
  333. $this->assertEquals($is, array(10, 11));
  334. }
  335. public function testAgeRange() {
  336. $is = $this->Datetime->ageRange(2000);
  337. pr($is);
  338. $this->assertEquals($is, 10);
  339. $is = $this->Datetime->ageRange(2002, null, null, 5);
  340. pr($is);
  341. $this->assertEquals($is, array(6, 10));
  342. $is = $this->Datetime->ageRange(2000, null, null, 5);
  343. pr($is);
  344. $this->assertEquals($is, array(6, 10));
  345. $is = $this->Datetime->ageRange(1985, 23, 11);
  346. pr($is);
  347. $this->assertEquals($is, 25);
  348. $is = $this->Datetime->ageRange(1985, null, null, 6);
  349. pr($is);
  350. $this->assertEquals($is, array(25, 30));
  351. $is = $this->Datetime->ageRange(1985, 21, 11, 7);
  352. pr($is);
  353. $this->assertEquals($is, array(22, 28));
  354. }
  355. }