date_repeat.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * @file
  4. * Test Date Repeat calculations.
  5. */
  6. class DateRepeatTestCase extends DrupalWebTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => t('Date Repeat'),
  10. 'description' => t('Test Date Repeat functions to create arrays of dates from iCal rules.') ,
  11. 'group' => t('Date'),
  12. );
  13. }
  14. /**
  15. * Implements setUp().
  16. */
  17. public function setUp() {
  18. // Load the date_repeat module.
  19. parent::setUp('date_api', 'date_repeat');
  20. }
  21. public function testDateRepeat() {
  22. require_once('./' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc');
  23. require_once('./' . drupal_get_path('module', 'date_repeat') . '/date_repeat_calc.inc');
  24. // Examples adapted from http://www.faqs.org/rfcs/rfc2445.html and
  25. // http://www.kanzaki.com/docs/ical/rrule.html.
  26. // Invalid value:
  27. $start = "1997-09-02 09:00:00";
  28. $end = "1997-09-30 09:00:00";
  29. $rule = "RRULE:FREQ=NONE;INTERVAL=0;COUNT=10";
  30. $dates = date_repeat_calc($rule, $start, $end, array());
  31. // should be (1997 9:00 AM EDT)September 2-11
  32. $shouldbe = '';
  33. $result = implode(', ', $dates);
  34. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  35. // Daily for 10 occurrences:
  36. $start = "1997-09-02 09:00:00";
  37. $end = "1997-09-30 09:00:00";
  38. $rule = "RRULE:FREQ=DAILY;COUNT=10";
  39. $dates = date_repeat_calc($rule, $start, $end, array());
  40. // should be (1997 9:00 AM EDT)September 2-11
  41. $shouldbe = '1997-09-02 09:00:00, 1997-09-03 09:00:00, 1997-09-04 09:00:00, 1997-09-05 09:00:00, 1997-09-06 09:00:00, 1997-09-07 09:00:00, 1997-09-08 09:00:00, 1997-09-09 09:00:00, 1997-09-10 09:00:00, 1997-09-11 09:00:00';
  42. $result = implode(', ', $dates);
  43. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  44. //Daily until September 24, 1997:
  45. $start = "1997-09-02 09:00:00";
  46. $end = "1997-09-30 09:00:00";
  47. $rule = "RRULE:FREQ=DAILY;UNTIL=19970924T000000Z";
  48. $dates = date_repeat_calc($rule, $start, $end, array());
  49. // should be (1997 9:00 AM EDT)September 2-23
  50. $shouldbe = '1997-09-02 09:00:00, 1997-09-03 09:00:00, 1997-09-04 09:00:00, 1997-09-05 09:00:00, 1997-09-06 09:00:00, 1997-09-07 09:00:00, 1997-09-08 09:00:00, 1997-09-09 09:00:00, 1997-09-10 09:00:00, 1997-09-11 09:00:00, 1997-09-12 09:00:00, 1997-09-13 09:00:00, 1997-09-14 09:00:00, 1997-09-15 09:00:00, 1997-09-16 09:00:00, 1997-09-17 09:00:00, 1997-09-18 09:00:00, 1997-09-19 09:00:00, 1997-09-20 09:00:00, 1997-09-21 09:00:00, 1997-09-22 09:00:00, 1997-09-23 09:00:00';
  51. $result = implode(', ', $dates);
  52. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  53. //Every other day - until September 30:
  54. $start = "1997-09-02 09:00:00";
  55. $end = "1997-09-30 09:00:00";
  56. $rule = "RRULE:FREQ=DAILY;INTERVAL=2";
  57. $dates = date_repeat_calc($rule, $start, $end, array());
  58. // should be (1997 9:00 AM EDT)September2,4,6,8...24,26,28,30;
  59. $shouldbe = '1997-09-02 09:00:00, 1997-09-04 09:00:00, 1997-09-06 09:00:00, 1997-09-08 09:00:00, 1997-09-10 09:00:00, 1997-09-12 09:00:00, 1997-09-14 09:00:00, 1997-09-16 09:00:00, 1997-09-18 09:00:00, 1997-09-20 09:00:00, 1997-09-22 09:00:00, 1997-09-24 09:00:00, 1997-09-26 09:00:00, 1997-09-28 09:00:00, 1997-09-30 09:00:00';
  60. $result = implode(', ', $dates);
  61. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  62. //Every 10 days, 2 occurrences:
  63. $start = "1997-09-02 09:00:00";
  64. $end = "1997-09-30 09:00:00";
  65. $rule = "RRULE:FREQ=DAILY;INTERVAL=10;COUNT=2";
  66. $dates = date_repeat_calc($rule, $start, $end, array());
  67. // should be (1997 9:00 AM EDT)September 2,12
  68. $shouldbe = '1997-09-02 09:00:00, 1997-09-12 09:00:00';
  69. $result = implode(', ', $dates);
  70. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  71. //Weekly for 3 occurrences
  72. $start = "1997-09-02 09:00:00";
  73. $end = "1997-09-30 09:00:00";
  74. $rule = "RRULE:FREQ=WEEKLY;COUNT=3";
  75. $dates = date_repeat_calc($rule, $start, $end, array());
  76. // should be (1997 9:00 AM EDT)September 2,9,16
  77. $shouldbe = '1997-09-02 09:00:00, 1997-09-09 09:00:00, 1997-09-16 09:00:00';
  78. $result = implode(', ', $dates);
  79. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  80. //Weekly until September 24, 1997
  81. $start = "1997-09-02 09:00:00";
  82. $end = "1997-09-30 09:00:00";
  83. $rule = "RRULE:FREQ=WEEKLY;UNTIL=19970924T000000Z";
  84. // ==> (1997 9:00 AM EDT)September 2,9,16,23
  85. $dates = date_repeat_calc($rule, $start, $end, array());
  86. $shouldbe = '1997-09-02 09:00:00, 1997-09-09 09:00:00, 1997-09-16 09:00:00, 1997-09-23 09:00:00';
  87. $result = implode(', ', $dates);
  88. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  89. //Every other week - forever:
  90. $start = "1997-09-02 09:00:00";
  91. $end = "1997-09-30 09:00:00";
  92. $rule = "RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=SU";
  93. // should be (1997 9:00 AM EDT)September 2,16,30
  94. $dates = date_repeat_calc($rule, $start, $end, array());
  95. $shouldbe = '1997-09-02 09:00:00, 1997-09-16 09:00:00, 1997-09-30 09:00:00';
  96. $result = implode(', ', $dates);
  97. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  98. //Weekly on Tuesday and Thursday for 4 weeks:
  99. $start = "1997-09-02 09:00:00";
  100. $end = "1997-09-30 09:00:00";
  101. $rule = "RRULE:FREQ=WEEKLY;COUNT=8;WKST=SU;BYDAY=TU,TH";
  102. // should be(1997 9:00 AM EDT)September 2,4,9,11,16,18,23,25
  103. $dates = date_repeat_calc($rule, $start, $end, array());
  104. $shouldbe = '1997-09-02 09:00:00, 1997-09-04 09:00:00, 1997-09-09 09:00:00, 1997-09-11 09:00:00, 1997-09-16 09:00:00, 1997-09-18 09:00:00, 1997-09-23 09:00:00, 1997-09-25 09:00:00';
  105. $result = implode(', ', $dates);
  106. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  107. //Every other week on Tuesday and Thursday, for 5 occurrences:
  108. $start = "1997-09-02 09:00:00";
  109. $end = "1997-09-30 09:00:00";
  110. $rule = "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=5;WKST=SU;BYDAY=TU,TH";
  111. // should be (1997 9:00 AM EDT)September 2,4,16,18,30
  112. $dates = date_repeat_calc($rule, $start, $end, array());
  113. $shouldbe = '1997-09-02 09:00:00, 1997-09-04 09:00:00, 1997-09-16 09:00:00, 1997-09-18 09:00:00, 1997-09-30 09:00:00';
  114. $result = implode(', ', $dates);
  115. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  116. //Every other week on Monday, Wednesday and Friday until September 24, 1997,
  117. $start = "1997-09-02 09:00:00";
  118. $end = "1997-09-30 09:00:00";
  119. $rule = "RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19970924T000000Z;WKST=SU;BYDAY=MO,WE,FR";
  120. // should be (1997 9:00 AM EDT)September 2,3,5,15,17,19
  121. $dates = date_repeat_calc($rule, $start, $end, array());
  122. $shouldbe = '1997-09-02 09:00:00, 1997-09-03 09:00:00, 1997-09-05 09:00:00, 1997-09-15 09:00:00, 1997-09-17 09:00:00, 1997-09-19 09:00:00';
  123. $result = implode(', ', $dates);
  124. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  125. //Monthly on the 1st Friday for 2 occurrences:
  126. $start = "1997-09-05 09:00:00";
  127. $end = "1997-10-31 09:00:00";
  128. $rule = "RRULE:FREQ=MONTHLY;COUNT=2;BYDAY=1FR";
  129. // should be (1997 9:00 AM EDT)September 5;October 3
  130. $dates = date_repeat_calc($rule, $start, $end, array());
  131. $shouldbe = '1997-09-05 09:00:00, 1997-10-03 09:00:00';
  132. $result = implode(', ', $dates);
  133. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  134. //Monthly on the 1st Friday until December 24, 1997:
  135. $start = "1997-09-05 09:00:00";
  136. $end = "1998-10-01 09:00:00";
  137. $rule = "RRULE:FREQ=MONTHLY;UNTIL=19971224T000000Z;BYDAY=1FR";
  138. $dates = date_repeat_calc($rule, $start, $end, array());
  139. $shouldbe = '1997-09-05 09:00:00, 1997-10-03 09:00:00, 1997-11-07 09:00:00, 1997-12-05 09:00:00';
  140. $result = implode(', ', $dates);
  141. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  142. //Every other month on the 1st and last Sunday of the month for 10 occurrences:
  143. $start = "1997-09-07 09:00:00";
  144. $end = "1998-10-01 09:00:00";
  145. $rule = "RRULE:FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU";
  146. // ==> (1997 9:00 AM EDT)September 7,28
  147. // (1997 9:00 AM EST)November 2,30
  148. // (1998 9:00 AM EST)January 4,25;March 1,29
  149. // (1998 9:00 AM EDT)May 3,31
  150. $dates = date_repeat_calc($rule, $start, $end, array());
  151. $shouldbe = '1997-09-07 09:00:00, 1997-09-28 09:00:00, 1997-11-02 09:00:00, 1997-11-30 09:00:00, 1998-01-04 09:00:00, 1998-01-25 09:00:00, 1998-03-01 09:00:00, 1998-03-29 09:00:00, 1998-05-03 09:00:00, 1998-05-31 09:00:00';
  152. $result = implode(', ', $dates);
  153. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  154. //Monthly on the second to last Monday of the month for 6 months:
  155. $start = "1997-09-22 09:00:00";
  156. $end = "1998-10-01 09:00:00";
  157. $rule = "RRULE:FREQ=MONTHLY;COUNT=6;BYDAY=-2MO";
  158. //==> (1997 9:00 AM EDT)September 22;October 20
  159. // (1997 9:00 AM EST)November 17;December 22
  160. // (1998 9:00 AM EST)January 19;February 16
  161. $dates = date_repeat_calc($rule, $start, $end, array());
  162. $shouldbe = '1997-09-22 09:00:00, 1997-10-20 09:00:00, 1997-11-17 09:00:00, 1997-12-22 09:00:00, 1998-01-19 09:00:00, 1998-02-16 09:00:00';
  163. $result = implode(', ', $dates);
  164. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  165. //Every Tuesday, every other month:
  166. $start = "1997-09-02 09:00:00";
  167. $end = "1998-02-01 09:00:00";
  168. $rule = "RRULE:FREQ=MONTHLY;INTERVAL=2;BYDAY=TU";
  169. // ==> (1997 9:00 AM EDT)September 2,9,16,23,30
  170. // (1997 9:00 AM EST)November 4,11,18,25
  171. // (1998 9:00 AM EST)January 6,13,20,27;March 3,10,17,24,31
  172. $dates = date_repeat_calc($rule, $start, $end, array());
  173. $shouldbe = '1997-09-02 09:00:00, 1997-09-09 09:00:00, 1997-09-16 09:00:00, 1997-09-23 09:00:00, 1997-09-30 09:00:00, 1997-11-04 09:00:00, 1997-11-11 09:00:00, 1997-11-18 09:00:00, 1997-11-25 09:00:00, 1998-01-06 09:00:00, 1998-01-13 09:00:00, 1998-01-20 09:00:00, 1998-01-27 09:00:00';
  174. $result = implode(', ', $dates);
  175. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  176. //Yearly in June and July for 10 occurrences:
  177. $start = "1997-06-10 09:00:00";
  178. $end = "2002-01-01 09:00:00";
  179. $rule = "RRULE:FREQ=YEARLY;COUNT=10;BYMONTH=6,7";
  180. // ==> (1997 9:00 AM EDT)June 10;July 10
  181. // (1998 9:00 AM EDT)June 10;July 10
  182. // (1999 9:00 AM EDT)June 10;July 10
  183. // (2000 9:00 AM EDT)June 10;July 10
  184. // (2001 9:00 AM EDT)June 10;July 10
  185. // Note: Since none of the BYDAY, BYMONTHDAY or BYYEARDAY components
  186. // are specified, the day is gotten from DTSTART
  187. $dates = date_repeat_calc($rule, $start, $end, array());
  188. $shouldbe = '1997-06-10 09:00:00, 1997-07-10 09:00:00, 1998-06-10 09:00:00, 1998-07-10 09:00:00, 1999-06-10 09:00:00, 1999-07-10 09:00:00, 2000-06-10 09:00:00, 2000-07-10 09:00:00, 2001-06-10 09:00:00, 2001-07-10 09:00:00';
  189. $result = implode(', ', $dates);
  190. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  191. //Every other year on January, February, and March for 10 occurrences:
  192. $start = "1997-03-10 09:00:00";
  193. $end = "2004-01-01 09:00:00";
  194. $rule = "RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3";
  195. // ==> (1997 9:00 AM EST)March 10
  196. // (1999 9:00 AM EST)January 10;February 10;March 10
  197. // (2001 9:00 AM EST)January 10;February 10;March 10
  198. // (2003 9:00 AM EST)January 10;February 10;March 10
  199. $dates = date_repeat_calc($rule, $start, $end, array());
  200. $shouldbe = '1997-03-10 09:00:00, 1999-01-10 09:00:00, 1999-02-10 09:00:00, 1999-03-10 09:00:00, 2001-01-10 09:00:00, 2001-02-10 09:00:00, 2001-03-10 09:00:00, 2003-01-10 09:00:00, 2003-02-10 09:00:00, 2003-03-10 09:00:00';
  201. $result = implode(', ', $dates);
  202. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  203. //An example where the days generated makes a difference because of WKST:
  204. $start = "1997-08-05 09:00:00";
  205. $end = "2004-01-01 09:00:00";
  206. $rule = "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO";
  207. // ==> (1997 EDT)Aug 5,10,19,24
  208. $dates = date_repeat_calc($rule, $start, $end, array());
  209. $shouldbe = '1997-08-05 09:00:00, 1997-08-10 09:00:00, 1997-08-19 09:00:00, 1997-08-24 09:00:00';
  210. $result = implode(', ', $dates);
  211. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  212. //changing only WKST from MO to SU, yields different results...
  213. $start = "1997-08-05 09:00:00";
  214. $end = "2004-01-01 09:00:00";
  215. $rule = "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU";
  216. // Result: 1997 EDT August 5,17,19,31;
  217. $dates = date_repeat_calc($rule, $start, $end, array());
  218. $shouldbe = '1997-08-05 09:00:00, 1997-08-17 09:00:00, 1997-08-19 09:00:00, 1997-08-31 09:00:00';
  219. $result = implode(', ', $dates);
  220. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  221. //Every 18 months on the 10th thru 15th of the month for 10 occurrences:
  222. $start = "1997-09-10 09:00:00";
  223. $end = "2004-01-01 09:00:00";
  224. $rule = "RRULE:FREQ=MONTHLY;INTERVAL=18;COUNT=10;BYMONTHDAY=10,11,12,13,14,15";
  225. // ==> (1997 9:00 AM EDT)September 10,11,12,13,14,15
  226. // (1999 9:00 AM EST)March 10,11,12,13
  227. $dates = date_repeat_calc($rule, $start, $end, array());
  228. $shouldbe = '1997-09-10 09:00:00, 1997-09-11 09:00:00, 1997-09-12 09:00:00, 1997-09-13 09:00:00, 1997-09-14 09:00:00, 1997-09-15 09:00:00, 1999-03-10 09:00:00, 1999-03-11 09:00:00, 1999-03-12 09:00:00, 1999-03-13 09:00:00';
  229. $result = implode(', ', $dates);
  230. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  231. //Monthly on the third to the last day of the month, forever:
  232. $start = "1997-09-28 09:00:00";
  233. $end = "1998-03-01 09:00:00";
  234. $rule = "RRULE:FREQ=MONTHLY;BYMONTHDAY=-3";
  235. // ==> (1997 9:00 AM EDT)September 28
  236. // (1997 9:00 AM EST)October 29;November 28;December 29
  237. // (1998 9:00 AM EST)January 29;February 26
  238. $dates = date_repeat_calc($rule, $start, $end, array());
  239. $shouldbe = '1997-09-28 09:00:00, 1997-10-29 09:00:00, 1997-11-28 09:00:00, 1997-12-29 09:00:00, 1998-01-29 09:00:00, 1998-02-26 09:00:00';
  240. $result = implode(', ', $dates);
  241. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  242. //Every Thursday in March, forever:
  243. // ==> (1997 9:00 AM EST)March 13,20,27
  244. // (1998 9:00 AM EST)March 5,12,19,26
  245. // (1999 9:00 AM EST)March 4,11,18,25
  246. $start = "1997-03-13 09:00:00";
  247. $end = "1999-03-31 09:00:00";
  248. $rule = "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH";
  249. $dates = date_repeat_calc($rule, $start, $end, array());
  250. $shouldbe = '1997-03-13 09:00:00, 1997-03-20 09:00:00, 1997-03-27 09:00:00, 1998-03-05 09:00:00, 1998-03-12 09:00:00, 1998-03-19 09:00:00, 1998-03-26 09:00:00, 1999-03-04 09:00:00, 1999-03-11 09:00:00, 1999-03-18 09:00:00, 1999-03-25 09:00:00';
  251. $result = implode(', ', $dates);
  252. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  253. //Every Thursday, but only during June, July, and August, forever:
  254. // ==> (1997 9:00 AM EDT)June 5,12,19,26;July 3,10,17,24,31;August 7,14,21,28
  255. // (1998 9:00 AM EDT)June 4,11,18,25;July 2,9,16,23,30;August 6,13,20,27
  256. // (1999 9:00 AM EDT)June 3,10,17,24;July 1,8,15,22,29;August 5,12,19,26
  257. $start = "1997-06-05 09:00:00";
  258. $end = "1999-08-31 09:00:00";
  259. $rule = "RRULE:FREQ=YEARLY;BYDAY=TH;BYMONTH=6,7,8";
  260. $dates = date_repeat_calc($rule, $start, $end, array());
  261. $shouldbe = '1997-06-05 09:00:00, 1997-06-12 09:00:00, 1997-06-19 09:00:00, 1997-06-26 09:00:00, 1997-07-03 09:00:00, 1997-07-10 09:00:00, 1997-07-17 09:00:00, 1997-07-24 09:00:00, 1997-07-31 09:00:00, 1997-08-07 09:00:00, 1997-08-14 09:00:00, 1997-08-21 09:00:00, 1997-08-28 09:00:00, 1998-06-04 09:00:00, 1998-06-11 09:00:00, 1998-06-18 09:00:00, 1998-06-25 09:00:00, 1998-07-02 09:00:00, 1998-07-09 09:00:00, 1998-07-16 09:00:00, 1998-07-23 09:00:00, 1998-07-30 09:00:00, 1998-08-06 09:00:00, 1998-08-13 09:00:00, 1998-08-20 09:00:00, 1998-08-27 09:00:00, 1999-06-03 09:00:00, 1999-06-10 09:00:00, 1999-06-17 09:00:00, 1999-06-24 09:00:00, 1999-07-01 09:00:00, 1999-07-08 09:00:00, 1999-07-15 09:00:00, 1999-07-22 09:00:00, 1999-07-29 09:00:00, 1999-08-05 09:00:00, 1999-08-12 09:00:00, 1999-08-19 09:00:00, 1999-08-26 09:00:00';
  262. $result = implode(', ', $dates);
  263. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  264. //Monthly on the 2nd and 15th of the month for 10 occurrences:
  265. // ==> (1997 9:00 AM EDT)September 2,15;October 2,15
  266. // (1997 9:00 AM EST)November 2,15;December 2,15
  267. // (1998 9:00 AM EST)January 2,15
  268. $start = "1997-09-02 09:00:00";
  269. $end = "1998-01-31 09:00:00";
  270. $rule = "RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15";
  271. $dates = date_repeat_calc($rule, $start, $end, array());
  272. $shouldbe = '1997-09-02 09:00:00, 1997-09-15 09:00:00, 1997-10-02 09:00:00, 1997-10-15 09:00:00, 1997-11-02 09:00:00, 1997-11-15 09:00:00, 1997-12-02 09:00:00, 1997-12-15 09:00:00, 1998-01-02 09:00:00, 1998-01-15 09:00:00';
  273. $result = implode(', ', $dates);
  274. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  275. //Monthly on the first and last day of the month for 10 occurrences:
  276. // ==> (1997 9:00 AM EDT)September 30;October 1
  277. // (1997 9:00 AM EST)October 31;November 1,30;December 1,31
  278. // (1998 9:00 AM EST)January 1,31;February 1
  279. $start = "1997-09-30 09:00:00";
  280. $end = "1998-03-31 09:00:00";
  281. $rule = "RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1";
  282. $dates = date_repeat_calc($rule, $start, $end, array());
  283. $shouldbe = '1997-09-30 09:00:00, 1997-10-01 09:00:00, 1997-10-31 09:00:00, 1997-11-01 09:00:00, 1997-11-30 09:00:00, 1997-12-01 09:00:00, 1997-12-31 09:00:00, 1998-01-01 09:00:00, 1998-01-31 09:00:00, 1998-02-01 09:00:00';
  284. $result = implode(', ', $dates);
  285. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  286. //Every Friday the 13th, forever:
  287. $rule = "EXDATE;TZID=US-Eastern:19970902T090000";
  288. // ==> (1998 9:00 AM EST)February 13;March 13;November 13
  289. // (1999 9:00 AM EDT)August 13
  290. // (2000 9:00 AM EDT)October 13
  291. $start = "1997-09-02 09:00:00";
  292. $end = "2000-12-31 09:00:00";
  293. $rule = "RRULE:FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13";
  294. $dates = date_repeat_calc($rule, $start, $end, array());
  295. $shouldbe = '1997-09-02 09:00:00, 1998-02-13 09:00:00, 1998-03-13 09:00:00, 1998-11-13 09:00:00, 1999-08-13 09:00:00, 2000-10-13 09:00:00';
  296. $result = implode(', ', $dates);
  297. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  298. //The first Saturday that follows the first Sunday of the month, forever:
  299. // ==> (1997 9:00 AM EDT)September 13;October 11
  300. // (1997 9:00 AM EST)November 8;December 13
  301. // (1998 9:00 AM EST)January 10;February 7;March 7
  302. // (1998 9:00 AM EDT)April 11;May 9;June 13...
  303. $start = "1997-09-13 09:00:00";
  304. $end = "1998-06-30 09:00:00";
  305. $rule = "RRULE:FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13";
  306. $dates = date_repeat_calc($rule, $start, $end, array());
  307. $shouldbe = '1997-09-13 09:00:00, 1997-10-11 09:00:00, 1997-11-08 09:00:00, 1997-12-13 09:00:00, 1998-01-10 09:00:00, 1998-02-07 09:00:00, 1998-03-07 09:00:00, 1998-04-11 09:00:00, 1998-05-09 09:00:00, 1998-06-13 09:00:00';
  308. $result = implode(', ', $dates);
  309. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  310. //Every four years, the first Tuesday after a Monday in November,
  311. //forever (U.S. Presidential Election day):
  312. // ==> (1996 9:00 AM EST)November 5
  313. // (2000 9:00 AM EST)November 7
  314. // (2004 9:00 AM EST)November 2
  315. $start = "1996-11-05 09:00:00";
  316. $end = "2004-11-30 09:00:00";
  317. $rule = "RRULE:FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8";
  318. $shouldbe = '1996-11-05 09:00:00, 2000-11-07 09:00:00, 2004-11-02 09:00:00';
  319. $dates = date_repeat_calc($rule, $start, $end, array());
  320. $result = implode(', ', $dates);
  321. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  322. //Every 20th Monday of the year, forever:
  323. $start = "1997-05-19 09:00:00";
  324. $end = "2000-01-01 09:00:00";
  325. $rule = "RRULE:FREQ=YEARLY;BYDAY=20MO";
  326. // ==> (1997 9:00 AM EDT)May 19
  327. // (1998 9:00 AM EDT)May 18
  328. // (1999 9:00 AM EDT)May 17
  329. $dates = date_repeat_calc($rule, $start, $end, array());
  330. $shouldbe = '1997-05-19 09:00:00, 1998-05-18 09:00:00, 1999-05-17 09:00:00';
  331. $result = implode(', ', $dates);
  332. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  333. //Every Sunday in January, every other year, forever:
  334. $start = "1997-01-05 09:00:00";
  335. $end = "2001-02-01 09:00:00";
  336. $rule = 'RRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU';
  337. // ==> (1997 9:00 AM EDT)January 5,12,19,26
  338. // (1999 9:00 AM EDT)January 3,10,17,24,31
  339. // (2001 9:00 AM EDT)January 7,14,21,28
  340. $dates = date_repeat_calc($rule, $start, $end, array());
  341. $shouldbe = '1997-01-05 09:00:00, 1997-01-12 09:00:00, 1997-01-19 09:00:00, 1997-01-26 09:00:00, 1999-01-03 09:00:00, 1999-01-10 09:00:00, 1999-01-17 09:00:00, 1999-01-24 09:00:00, 1999-01-31 09:00:00, 2001-01-07 09:00:00, 2001-01-14 09:00:00, 2001-01-21 09:00:00, 2001-01-28 09:00:00';
  342. $result = implode(', ', $dates);
  343. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  344. //Every Last Thursday in November, every year, five times:
  345. $start = "2014-11-27 00:00:00";
  346. $rule = 'FREQ=YEARLY;INTERVAL=1;BYDAY=-1TH;BYMONTH=11;COUNT=5;WKST=SU';
  347. // ==> (2014 00:00 AM EDT)November 27
  348. // (2015 00:00 AM EDT)November 26
  349. // (2016 00:00 AM EDT)November 24
  350. // (2017 00:00 AM EDT)November 30
  351. // (2018 00:00 AM EDT)November 29
  352. $dates = date_repeat_calc($rule, $start, NULL, array());
  353. $shouldbe = '2014-11-27 00:00:00, 2015-11-26 00:00:00, 2016-11-24 00:00:00, 2017-11-30 00:00:00, 2018-11-29 00:00:00';
  354. $result = implode(', ', $dates);
  355. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  356. return;
  357. //Every Thanksgiving, forever:
  358. $start = "1997-01-01 09:00:00";
  359. $end = "2001-02-01 09:00:00";
  360. $rule = 'RRULE:FREQ=YEARLY;INTERVAL=1;BYMONTH=11;BYDAY=4TH';
  361. // ==> (1997 9:00 AM EDT)Nov
  362. // (1999 9:00 AM EDT)Nov
  363. // (2001 9:00 AM EDT)Nov
  364. $dates = date_repeat_calc($rule, $start, $end, array());
  365. $shouldbe = '';
  366. $result = implode(', ', $dates);
  367. $this->assertEqual($result, $shouldbe, $rule . '; Starting ' . $start . '; results: ' . $result);
  368. // TODO:
  369. // BYYEARDAY, BYSETPOS,
  370. // BYHOUR, BYMINUTE, HOURLY, MINUTELY, SECONDLY
  371. // have not yet been implemented in date_repeat.
  372. //Every 3rd year on the 1st, 100th and 200th day for 10 occurrences:
  373. $date = "DTSTART;TZID=US-Eastern:19970101T090000";
  374. $rule = "RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200";
  375. // ==> (1997 9:00 AM EST)January 1
  376. // (1997 9:00 AM EDT)April 10;July 19
  377. // (2000 9:00 AM EST)January 1
  378. // (2000 9:00 AM EDT)April 9;July 18
  379. // (2003 9:00 AM EST)January 1
  380. // (2003 9:00 AM EDT)April 10;July 19
  381. // (2006 9:00 AM EST)January 1
  382. //Monday of week number 20 (where the default start of the week is Monday), forever:
  383. $date = "DTSTART;TZID=US-Eastern:19970512T090000";
  384. $rule = "RRULE:FREQ=YEARLY;BYWEEKNO=20;BYDAY=MO";
  385. // ==> (1997 9:00 AM EDT)May 12
  386. // (1998 9:00 AM EDT)May 11
  387. // (1999 9:00 AM EDT)May 17
  388. //The 3rd instance into the month of one of Tuesday, Wednesday or
  389. //Thursday, for the next 3 months:
  390. $date = "DTSTART;TZID=US-Eastern:19970904T090000";
  391. $rule = "RRULE:FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,TH;BYSETPOS=3";
  392. // ==> (1997 9:00 AM EDT)September 4;October 7
  393. // (1997 9:00 AM EST)November 6
  394. //The 2nd to last weekday of the month:
  395. $date = "DTSTART;TZID=US-Eastern:19970929T090000";
  396. $rule = "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2";
  397. // ==> (1997 9:00 AM EDT)September 29
  398. // (1997 9:00 AM EST)October 30;November 27;December 30
  399. // (1998 9:00 AM EST)January 29;February 26;March 30
  400. //Every 3 hours from 9:00 AM to 5:00 PM on a specific day:
  401. $date = "DTSTART;TZID=US-Eastern:19970902T090000";
  402. $rule = "RRULE:FREQ=HOURLY;INTERVAL=3;UNTIL=19970902T170000Z";
  403. // ==> (September 2, 1997 EDT)09:00,12:00,15:00
  404. //Every 15 minutes for 6 occurrences:
  405. $date = "DTSTART;TZID=US-Eastern:19970902T090000";
  406. $rule = "RRULE:FREQ=MINUTELY;INTERVAL=15;COUNT=6";
  407. // ==> (September 2, 1997 EDT)09:00,09:15,09:30,09:45,10:00,10:15
  408. //Every hour and a half for 4 occurrences:
  409. $date = "DTSTART;TZID=US-Eastern:19970902T090000";
  410. $rule = "RRULE:FREQ=MINUTELY;INTERVAL=90;COUNT=4";
  411. // ==> (September 2, 1997 EDT)09:00,10:30;12:00;13:30
  412. //Every 20 minutes from 9:00 AM to 4:40 PM every day:
  413. $date = "DTSTART;TZID=US-Eastern:19970902T090000";
  414. $rule = "RRULE:FREQ=DAILY;BYHOUR=9,10,11,12,13,14,15,16;BYMINUTE=0,20,40";
  415. // or
  416. $rule = "RRULE:FREQ=MINUTELY;INTERVAL=20;BYHOUR=9,10,11,12,13,14,15,16";
  417. // ==> (September 2, 1997 EDT)9:00,9:20,9:40,10:00,10:20,16:00,16:20,16:40
  418. // (September 3, 1997 EDT)9:00,9:20,9:40,10:00,10:20,16:00,16:20,16:40
  419. }
  420. }