DatetimeLibTest.php 13 KB

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