DatetimeLibTest.php 13 KB

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