3 * Class for parsing RRule and getting us the dates
7 * @author Andrew McMillan <andrew@catalyst.net.nz>
8 * @copyright Catalyst .Net Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
12 $ical_weekdays = array( 'SU' => 0, 'MO' => 1, 'TU' => 2, 'WE' => 3, 'TH' => 4, 'FR' => 5, 'SA' => 6 );
15 * A Class for handling dates in iCalendar format. We do make the simplifying assumption
16 * that all date handling in here is normalised to GMT. One day we might provide some
17 * functions to do that, but for now it is done externally.
32 /** Fragmented parts */
41 /** Which day of the week does the week start on */
47 * The constructor takes either an iCalendar date, a text string formatted as
48 * an iCalendar date, or epoch seconds.
50 function iCalDate( $input ) {
51 if ( gettype($input) == 'object' ) {
52 $this->_text
= $input->_text
;
53 $this->_epoch
= $input->_epoch
;
54 $this->_yy
= $input->_yy
;
55 $this->_mo
= $input->_mo
;
56 $this->_dd
= $input->_dd
;
57 $this->_hh
= $input->_hh
;
58 $this->_mi
= $input->_mi
;
59 $this->_ss
= $input->_ss
;
60 $this->_tz
= $input->_tz
;
64 $this->_wkst
= 1; // Monday
65 if ( preg_match( '/^\d{8}[T ]\d{6}$/', $input ) ) {
66 $this->SetLocalDate($input);
68 else if ( preg_match( '/^\d{8}[T ]\d{6}Z$/', $input ) ) {
69 $this->SetGMTDate($input);
71 else if ( intval($input) == 0 ) {
72 $this->SetLocalDate(strtotime($input));
76 $this->SetEpochDate($input);
82 * Set the date from a text string
84 function SetGMTDate( $input ) {
85 $this->_text
= $input;
86 $this->_PartsFromText();
87 $this->_GMTEpochFromParts();
92 * Set the date from a text string
94 function SetLocalDate( $input ) {
95 $this->_text
= $input;
96 $this->_PartsFromText();
97 $this->_EpochFromParts();
102 * Set the date from an epoch
104 function SetEpochDate( $input ) {
105 $this->_epoch
= intval($input);
106 $this->_TextFromEpoch();
107 $this->_PartsFromText();
112 * Given an epoch date, convert it to text
114 function _TextFromEpoch() {
115 $this->_text
= date('Ymd\THis', $this->_epoch
);
116 // dbg_error_log( "RRule", " Text %s from epoch %d", $this->_text, $this->_epoch );
120 * Given a GMT epoch date, convert it to text
122 function _GMTTextFromEpoch() {
123 $this->_text
= gmdate('Ymd\THis', $this->_epoch
);
124 // dbg_error_log( "RRule", " Text %s from epoch %d", $this->_text, $this->_epoch );
128 * Given a text date, convert it to parts
130 function _PartsFromText() {
131 $this->_yy
= intval(substr($this->_text
,0,4));
132 $this->_mo
= intval(substr($this->_text
,4,2));
133 $this->_dd
= intval(substr($this->_text
,6,2));
134 $this->_hh
= intval(substr($this->_text
,9,2));
135 $this->_mi
= intval(substr($this->_text
,11,2));
136 $this->_ss
= intval(substr($this->_text
,13,2));
141 * Given a GMT text date, convert it to an epoch
143 function _GMTEpochFromParts() {
144 $this->_epoch
= gmmktime ( $this->_hh
, $this->_mi
, $this->_ss
, $this->_mo
, $this->_dd
, $this->_yy
);
145 // dbg_error_log( "RRule", " Epoch %d from %04d-%02d-%02d %02d:%02d:%02d", $this->_epoch, $this->_yy, $this->_mo, $this->_dd, $this->_hh, $this->_mi, $this->_ss );
150 * Given a local text date, convert it to an epoch
152 function _EpochFromParts() {
153 $this->_epoch
= mktime ( $this->_hh
, $this->_mi
, $this->_ss
, $this->_mo
, $this->_dd
, $this->_yy
);
154 // dbg_error_log( "RRule", " Epoch %d from %04d-%02d-%02d %02d:%02d:%02d", $this->_epoch, $this->_yy, $this->_mo, $this->_dd, $this->_hh, $this->_mi, $this->_ss );
159 * Set the day of week used for calculation of week starts
161 * @param string $weekstart The day of the week which is the first business day.
163 function SetWeekStart($weekstart) {
164 global $ical_weekdays;
165 $this->_wkst
= $ical_weekdays[$weekstart];
170 * Set the day of week used for calculation of week starts
172 function Render( $fmt = 'Y-m-d H:i:s' ) {
173 return date( $fmt, $this->_epoch
);
178 * Render the date as GMT
180 function RenderGMT( $fmt = 'Ymd\THis\Z' ) {
181 return gmdate( $fmt, $this->_epoch
);
186 * No of days in a month 1(Jan) - 12(Dec)
188 function DaysInMonth( $mo=false, $yy=false ) {
189 if ( $mo === false ) $mo = $this->_mo
;
209 if ( $yy === false ) $yy = $this->_yy
;
210 if ( (($yy %
4) == 0) && ((($yy %
100) != 0) ||
(($yy %
400) == 0) ) ) return 29;
215 dbg_error_log( "ERROR"," Invalid month of '%s' passed to DaysInMonth", $mo );
223 * Set the day in the month to what we have been given
225 function SetMonthDay( $dd ) {
226 if ( $dd == $this->_dd
) return; // Shortcut
227 $dd = min($dd,$this->DaysInMonth());
229 $this->_EpochFromParts();
230 $this->_TextFromEpoch();
235 * Add some number of months to a date
237 function AddMonths( $mo ) {
238 // dbg_error_log( "RRule", " Adding %d months to %s", $mo, $this->_text );
240 while ( $this->_mo
< 1 ) {
244 while ( $this->_mo
> 12 ) {
249 if ( ($this->_dd
> 28 && $this->_mo
== 2) ||
$this->_dd
> 30 ) {
250 // Ensure the day of month is still reasonable and coerce to last day of month if needed
251 $dim = $this->DaysInMonth();
252 if ( $this->_dd
> $dim ) {
256 $this->_EpochFromParts();
257 $this->_TextFromEpoch();
258 // dbg_error_log( "RRule", " Added %d months and got %s", $mo, $this->_text );
263 * Add some integer number of days to a date
265 function AddDays( $dd ) {
266 $at_start = $this->_text
;
268 while ( 1 > $this->_dd
) {
270 if ( $this->_mo
< 1 ) {
274 $this->_dd +
= $this->DaysInMonth();
276 while ( ($dim = $this->DaysInMonth($this->_mo
)) < $this->_dd
) {
279 if ( $this->_mo
> 12 ) {
284 $this->_EpochFromParts();
285 $this->_TextFromEpoch();
286 // dbg_error_log( "RRule", " Added %d days to %s and got %s", $dd, $at_start, $this->_text );
293 function AddDuration( $duration ) {
294 if ( strstr($duration,'T') === false ) $duration .= 'T';
295 list( $sign, $days, $time ) = preg_split( '/[PT]/', $duration );
296 $sign = ( $sign == "-" ?
-1 : 1);
297 // dbg_error_log( "RRule", " Adding duration to '%s' of sign: %d, days: %s, time: %s", $this->_text, $sign, $days, $time );
298 if ( preg_match( '/(\d+)(D|W)/', $days, $matches ) ) {
299 $days = intval($matches[1]);
300 if ( $matches[2] == 'W' ) $days *= 7;
301 $this->AddDays( $days * $sign );
303 $hh = 0; $mi = 0; $ss = 0;
304 if ( preg_match( '/(\d+)(H)/', $time, $matches ) ) $hh = $matches[1];
305 if ( preg_match( '/(\d+)(M)/', $time, $matches ) ) $mi = $matches[1];
306 if ( preg_match( '/(\d+)(S)/', $time, $matches ) ) $ss = $matches[1];
308 // dbg_error_log( "RRule", " Adding %02d:%02d:%02d * %d to %02d:%02d:%02d", $hh, $mi, $ss, $sign, $this->_hh, $this->_mi, $this->_ss );
309 $this->_hh +
= ($hh * $sign);
310 $this->_mi +
= ($mi * $sign);
311 $this->_ss +
= ($ss * $sign);
313 if ( $this->_ss
< 0 ) { $this->_mi
-= (intval(abs($this->_ss
/60))+
1); $this->_ss +
= ((intval(abs($this->_mi
/60))+
1) * 60); }
314 if ( $this->_ss
> 59) { $this->_mi +
= (intval(abs($this->_ss
/60))+
1); $this->_ss
-= ((intval(abs($this->_mi
/60))+
1) * 60); }
315 if ( $this->_mi
< 0 ) { $this->_hh
-= (intval(abs($this->_mi
/60))+
1); $this->_mi +
= ((intval(abs($this->_mi
/60))+
1) * 60); }
316 if ( $this->_mi
> 59) { $this->_hh +
= (intval(abs($this->_mi
/60))+
1); $this->_mi
-= ((intval(abs($this->_mi
/60))+
1) * 60); }
317 if ( $this->_hh
< 0 ) { $this->AddDays( -1 * (intval(abs($this->_hh
/24))+
1) ); $this->_hh +
= ((intval(abs($this->_hh
/24))+
1)*24); }
318 if ( $this->_hh
> 23) { $this->AddDays( (intval(abs($this->_hh
/24))+
1) ); $this->_hh
-= ((intval(abs($this->_hh
/24))+
1)*24); }
320 $this->_EpochFromParts();
321 $this->_TextFromEpoch();
326 * Produce an iCalendar format DURATION for the difference between this an another iCalDate
328 * @param date $from The start of the period
329 * @return string The date difference, as an iCalendar duration format
331 function DateDifference( $from ) {
332 if ( !is_object($from) ) {
333 $from = new iCalDate($from);
335 if ( $from->_epoch
< $this->_epoch
) {
336 /** One way to simplify is to always go for positive differences */
337 return( "-". $from->DateDifference( $self ) );
339 // if ( $from->_yy == $this->_yy && $from->_mo == $this->_mo ) {
340 /** Also somewhat simpler if we can use seconds */
341 $diff = $from->_epoch
- $this->_epoch
;
343 if ( $diff >= 86400) {
344 $result = intval($diff / 86400);
345 $diff = $diff %
86400;
346 if ( $diff == 0 && (($result %
7) == 0) ) {
347 // Duration is an integer number of weeks.
348 $result .= intval($result / 7) . "W";
353 $result = "P".$result."T";
354 if ( $diff >= 3600) {
355 $result .= intval($diff / 3600) . "H";
356 $diff = $diff %
3600;
359 $result .= intval($diff / 60) . "M";
363 $result .= intval($diff) . "S";
369 * From an intense reading of RFC2445 it appears that durations which are not expressible
370 * in Weeks/Days/Hours/Minutes/Seconds are invalid.
371 * ==> This code is not needed then :-)
372 $yy = $from->_yy - $this->_yy;
373 $mo = $from->_mo - $this->_mo;
374 $dd = $from->_dd - $this->_dd;
375 $hh = $from->_hh - $this->_hh;
376 $mi = $from->_mi - $this->_mi;
377 $ss = $from->_ss - $this->_ss;
379 if ( $ss < 0 ) { $mi -= 1; $ss += 60; }
380 if ( $mi < 0 ) { $hh -= 1; $mi += 60; }
381 if ( $hh < 0 ) { $dd -= 1; $hh += 24; }
382 if ( $dd < 0 ) { $mo -= 1; $dd += $this->DaysInMonth(); } // Which will use $this->_(mo|yy) - seemingly sensible
383 if ( $mo < 0 ) { $yy -= 1; $mo += 12; }
386 if ( $yy > 0) { $result .= $yy."Y"; }
387 if ( $mo > 0) { $result .= $mo."M"; }
388 if ( $dd > 0) { $result .= $dd."D"; }
390 if ( $hh > 0) { $result .= $hh."H"; }
391 if ( $mi > 0) { $result .= $mi."M"; }
392 if ( $ss > 0) { $result .= $ss."S"; }
398 * Test to see if our _mo matches something in the list of months we have received.
399 * @param string $monthlist A comma-separated list of months.
400 * @return boolean Whether this date falls within one of those months.
402 function TestByMonth( $monthlist ) {
403 // dbg_error_log( "RRule", " Testing BYMONTH %s against month %d", (isset($monthlist) ? $monthlist : "no month list"), $this->_mo );
404 if ( !isset($monthlist) ) return true; // If BYMONTH is not specified any month is OK
405 $months = array_flip(explode( ',',$monthlist ));
406 return isset($months[$this->_mo
]);
410 * Applies any BYDAY to the month to return a set of days
411 * @param string $byday The BYDAY rule
412 * @return array An array of the day numbers for the month which meet the rule.
414 function GetMonthByDay($byday) {
415 // dbg_error_log( "RRule", " Applying BYDAY %s to month", $byday );
416 $days_in_month = $this->DaysInMonth();
417 $dayrules = explode(',',$byday);
419 $first_dow = (date('w',$this->_epoch
) - $this->_dd +
36) %
7;
420 foreach( $dayrules AS $k => $v ) {
421 $days = $this->MonthDays($first_dow,$days_in_month,$v);
422 foreach( $days AS $k2 => $v2 ) {
426 asort( $set, SORT_NUMERIC
);
431 * Applies any BYMONTHDAY to the month to return a set of days
432 * @param string $bymonthday The BYMONTHDAY rule
433 * @return array An array of the day numbers for the month which meet the rule.
435 function GetMonthByMonthDay($bymonthday) {
436 // dbg_error_log( "RRule", " Applying BYMONTHDAY %s to month", $bymonthday );
437 $days_in_month = $this->DaysInMonth();
438 $dayrules = explode(',',$bymonthday);
440 foreach( $dayrules AS $k => $v ) {
442 if ( $v > 0 && $v <= $days_in_month ) $set[$v] = $v;
444 asort( $set, SORT_NUMERIC
);
450 * Applies any BYDAY to the week to return a set of days
451 * @param string $byday The BYDAY rule
452 * @param string $increasing When we are moving by months, we want any day of the week, but when by day we only want to increase. Default false.
453 * @return array An array of the day numbers for the week which meet the rule.
455 function GetWeekByDay($byday, $increasing = false) {
456 global $ical_weekdays;
457 // dbg_error_log( "RRule", " Applying BYDAY %s to week", $byday );
458 $days = explode(',',$byday);
459 $dow = date('w',$this->_epoch
);
461 foreach( $days AS $k => $v ) {
462 $daynum = $ical_weekdays[$v];
463 $dd = $this->_dd
- $dow +
$daynum;
464 if ( $daynum < $this->_wkst
) $dd +
= 7;
465 if ( $dd > $this->_dd ||
!$increasing ) $set[$dd] = $dd;
467 asort( $set, SORT_NUMERIC
);
474 * Test if $this is greater than the date parameter
475 * @param string $lesser The other date, as a local time string
476 * @return boolean True if $this > $lesser
478 function GreaterThan($lesser) {
479 if ( is_object($lesser) ) {
480 // dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $lesser->_text );
481 return ( $this->_text
> $lesser->_text
);
483 // dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $lesser );
484 return ( $this->_text
> $lesser ); // These sorts of dates are designed that way...
489 * Test if $this is less than the date parameter
490 * @param string $greater The other date, as a local time string
491 * @return boolean True if $this < $greater
493 function LessThan($greater) {
494 if ( is_object($greater) ) {
495 // dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $greater->_text );
496 return ( $this->_text
< $greater->_text
);
498 // dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $greater );
499 return ( $this->_text
< $greater ); // These sorts of dates are designed that way...
504 * Given a MonthDays string like "1MO", "-2WE" return an integer day of the month.
506 * @param string $dow_first The day of week of the first of the month.
507 * @param string $days_in_month The number of days in the month.
508 * @param string $dayspec The specification for a month day (or days) which we parse.
510 * @return array An array of the day numbers for the month which meet the rule.
512 function &MonthDays($dow_first, $days_in_month, $dayspec) {
513 global $ical_weekdays;
514 // dbg_error_log( "RRule", "MonthDays: Getting days for '%s'. %d days starting on a %d", $dayspec, $days_in_month, $dow_first );
516 preg_match( '/([0-9-]*)(MO|TU|WE|TH|FR|SA|SU)/', $dayspec, $matches);
517 $numeric = intval($matches[1]);
518 $dow = $ical_weekdays[$matches[2]];
520 $first_matching_day = 1 +
($dow - $dow_first);
521 while ( $first_matching_day < 1 ) $first_matching_day +
= 7;
523 // dbg_error_log( "RRule", " MonthDays: Looking at %d for first match on (%s/%s), %d for numeric", $first_matching_day, $matches[1], $matches[2], $numeric );
525 while( $first_matching_day <= $days_in_month ) {
526 $set[] = $first_matching_day;
527 $first_matching_day +
= 7;
530 if ( $numeric != 0 ) {
531 if ( $numeric < 0 ) {
532 $numeric +
= count($set);
537 $answer = $set[$numeric];
538 $set = array( $answer => $answer );
543 foreach( $answers AS $k => $v ) {
548 // dbg_log_array( "RRule", 'MonthDays', $set, false );
555 * Given set position descriptions like '1', '3', '11', '-3' or '-1' and a set,
556 * return the subset matching the list of set positions.
558 * @param string $bysplist The list of set positions.
559 * @param string $set The set of days that we will apply the positions to.
561 * @return array The subset which matches.
563 function &ApplyBySetPos($bysplist, $set) {
564 // dbg_error_log( "RRule", " ApplyBySetPos: Applying set position '%s' to set of %d days", $bysplist, count($set) );
566 sort( $set, SORT_NUMERIC
);
568 $positions = explode( '[^0-9-]', $bysplist );
569 foreach( $positions AS $k => $v ) {
576 $subset[$set[$v]] = $set[$v];
585 * A Class for handling Events on a calendar which repeat
587 * Here's the spec, from RFC2445:
589 recur = "FREQ"=freq *(
591 ; either UNTIL or COUNT may appear in a 'recur',
592 ; but UNTIL and COUNT MUST NOT occur in the same 'recur'
594 ( ";" "UNTIL" "=" enddate ) /
595 ( ";" "COUNT" "=" 1*DIGIT ) /
597 ; the rest of these keywords are optional,
598 ; but MUST NOT occur more than once
600 ( ";" "INTERVAL" "=" 1*DIGIT ) /
601 ( ";" "BYSECOND" "=" byseclist ) /
602 ( ";" "BYMINUTE" "=" byminlist ) /
603 ( ";" "BYHOUR" "=" byhrlist ) /
604 ( ";" "BYDAY" "=" bywdaylist ) /
605 ( ";" "BYMONTHDAY" "=" bymodaylist ) /
606 ( ";" "BYYEARDAY" "=" byyrdaylist ) /
607 ( ";" "BYWEEKNO" "=" bywknolist ) /
608 ( ";" "BYMONTH" "=" bymolist ) /
609 ( ";" "BYSETPOS" "=" bysplist ) /
610 ( ";" "WKST" "=" weekday ) /
611 ( ";" x-name "=" text )
614 freq = "SECONDLY" / "MINUTELY" / "HOURLY" / "DAILY"
615 / "WEEKLY" / "MONTHLY" / "YEARLY"
618 enddate =/ date-time ;An UTC value
620 byseclist = seconds / ( seconds *("," seconds) )
622 seconds = 1DIGIT / 2DIGIT ;0 to 59
624 byminlist = minutes / ( minutes *("," minutes) )
626 minutes = 1DIGIT / 2DIGIT ;0 to 59
628 byhrlist = hour / ( hour *("," hour) )
630 hour = 1DIGIT / 2DIGIT ;0 to 23
632 bywdaylist = weekdaynum / ( weekdaynum *("," weekdaynum) )
634 weekdaynum = [([plus] ordwk / minus ordwk)] weekday
640 ordwk = 1DIGIT / 2DIGIT ;1 to 53
642 weekday = "SU" / "MO" / "TU" / "WE" / "TH" / "FR" / "SA"
643 ;Corresponding to SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
644 ;FRIDAY, SATURDAY and SUNDAY days of the week.
646 bymodaylist = monthdaynum / ( monthdaynum *("," monthdaynum) )
648 monthdaynum = ([plus] ordmoday) / (minus ordmoday)
650 ordmoday = 1DIGIT / 2DIGIT ;1 to 31
652 byyrdaylist = yeardaynum / ( yeardaynum *("," yeardaynum) )
654 yeardaynum = ([plus] ordyrday) / (minus ordyrday)
656 ordyrday = 1DIGIT / 2DIGIT / 3DIGIT ;1 to 366
658 bywknolist = weeknum / ( weeknum *("," weeknum) )
660 weeknum = ([plus] ordwk) / (minus ordwk)
662 bymolist = monthnum / ( monthnum *("," monthnum) )
664 monthnum = 1DIGIT / 2DIGIT ;1 to 12
666 bysplist = setposday / ( setposday *("," setposday) )
668 setposday = yeardaynum
670 * At this point we are going to restrict ourselves to parts of the RRULE specification
671 * seen in the wild. And by "in the wild" I don't include within people's timezone
672 * definitions. We always convert time zones to canonical names and assume the lower
673 * level libraries can do a better job with them than we can.
675 * We will concentrate on:
676 * FREQ=(YEARLY|MONTHLY|WEEKLY|DAILY)
696 /** The first instance */
699 /** The current instance pointer */
702 /** An array of all the dates so far */
705 /** Whether we have calculated any of the dates */
708 /** Whether we have calculated all of the dates */
711 /** The rule, in all it's glory */
714 /** The rule, in all it's parts */
720 * The constructor takes a start date and an RRULE definition. Both of these
721 * follow the iCalendar standard.
723 function RRule( $start, $rrule ) {
724 $this->_first
= new iCalDate($start);
725 $this->_finished
= false;
726 $this->_started
= false;
727 $this->_dates
= array();
728 $this->_current
= -1;
730 $this->_rule
= preg_replace( '/\s/m', '', $rrule);
731 if ( substr($this->_rule
, 0, 6) == 'RRULE:' ) {
732 $this->_rule
= substr($this->_rule
, 6);
735 dbg_error_log( "RRule", " new RRule: Start: %s, RRULE: %s", $start->Render(), $this->_rule
);
737 $parts = explode(';',$this->_rule
);
738 $this->_part
= array( 'INTERVAL' => 1 );
739 foreach( $parts AS $k => $v ) {
740 list( $type, $value ) = explode( '=', $v, 2);
741 // dbg_error_log( "RRule", " Parts of %s explode into %s and %s", $v, $type, $value );
742 $this->_part
[$type] = $value;
745 // A little bit of validation
746 if ( !isset($this->_part
['FREQ']) ) {
747 dbg_error_log( "ERROR", " RRULE MUST have FREQ=value (%s)", $rrule );
749 if ( isset($this->_part
['COUNT']) && isset($this->_part
['UNTIL']) ) {
750 dbg_error_log( "ERROR", " RRULE MUST NOT have both COUNT=value and UNTIL=value (%s)", $rrule );
752 if ( isset($this->_part
['COUNT']) && intval($this->_part
['COUNT']) < 1 ) {
753 dbg_error_log( "ERROR", " RRULE MUST NOT have both COUNT=value and UNTIL=value (%s)", $rrule );
755 if ( !preg_match( '/(YEAR|MONTH|WEEK|DAI)LY/', $this->_part
['FREQ']) ) {
756 dbg_error_log( "ERROR", " RRULE Only FREQ=(YEARLY|MONTHLY|WEEKLY|DAILY) are supported at present (%s)", $rrule );
758 if ( $this->_part
['FREQ'] == "YEARLY" ) {
759 $this->_part
['INTERVAL'] *= 12;
760 $this->_part
['FREQ'] = "MONTHLY";
766 * Processes the array of $relative_days to $base and removes any
767 * which are not within the scope of our rule.
769 function WithinScope( $base, $relative_days ) {
773 $ptr = $this->_current
;
775 // dbg_error_log( "RRule", " WithinScope: Processing list of %d days relative to %s", count($relative_days), $base->Render() );
776 foreach( $relative_days AS $day => $v ) {
778 $test = new iCalDate($base);
779 $days_in_month = $test->DaysInMonth();
781 // dbg_error_log( "RRule", " WithinScope: Testing for day %d based on %s, with %d days in month", $day, $test->Render(), $days_in_month );
782 if ( $day > $days_in_month ) {
783 $test->SetMonthDay($days_in_month);
785 $day -= $days_in_month;
786 $test->SetMonthDay($day);
788 else if ( $day < 1 ) {
789 $test->SetMonthDay(1);
791 $days_in_month = $test->DaysInMonth();
792 $day +
= $days_in_month;
793 $test->SetMonthDay($day);
796 $test->SetMonthDay($day);
799 // dbg_error_log( "RRule", " WithinScope: Testing if %s is within scope", count($relative_days), $test->Render() );
801 if ( isset($this->_part
['UNTIL']) && $test->GreaterThan($this->_part
['UNTIL']) ) {
802 $this->_finished
= true;
806 // if ( $this->_current >= 0 && $test->LessThan($this->_dates[$this->_current]) ) continue;
808 if ( !$test->LessThan($this->_first
) ) {
809 // dbg_error_log( "RRule", " WithinScope: Looks like %s is within scope", $test->Render() );
810 $ok_days[$day] = $test;
814 if ( isset($this->_part
['COUNT']) && $ptr >= $this->_part
['COUNT'] ) {
815 $this->_finished
= true;
826 * This is most of the meat of the RRULE processing, where we find the next date.
829 function &GetNext( ) {
831 if ( $this->_current
< 0 ) {
832 $next = new iCalDate($this->_first
);
836 $next = new iCalDate($this->_dates
[$this->_current
]);
840 * If we have already found some dates we may just be able to return one of those.
842 if ( isset($this->_dates
[$this->_current
]) ) {
843 // dbg_error_log( "RRule", " GetNext: Returning %s, (%d'th)", $this->_dates[$this->_current]->Render(), $this->_current );
844 return $this->_dates
[$this->_current
];
847 if ( isset($this->_part
['COUNT']) && $this->_current
>= $this->_part
['COUNT'] ) // >= since _current is 0-based and COUNT is 1-based
848 $this->_finished
= true;
852 if ( $this->_finished
) {
858 if ( isset($this->_part
['WKST']) ) $next->SetWeekStart($this->_part
['WKST']);
859 if ( $this->_part
['FREQ'] == "MONTHLY" ) {
860 // dbg_error_log( "RRule", " GetNext: Calculating more dates for MONTHLY rule" );
866 if ( $this->_started
) {
867 $next->AddMonths($this->_part
['INTERVAL']);
870 $this->_started
= true;
873 while ( isset($this->_part
['BYMONTH']) && $limit > 0 && ! $next->TestByMonth($this->_part
['BYMONTH']) );
875 if ( isset($this->_part
['BYDAY']) ) {
876 $days = $next->GetMonthByDay($this->_part
['BYDAY']);
878 else if ( isset($this->_part
['BYMONTHDAY']) ) {
879 $days = $next->GetMonthByMonthDay($this->_part
['BYMONTHDAY']);
882 $days[$next->_dd
] = $next->_dd
;
884 if ( isset($this->_part
['BYSETPOS']) ) {
885 $days = $next->ApplyBySetpos($this->_part
['BYSETPOS'], $days);
888 $days = $this->WithinScope( $next, $days);
890 while( $limit && count($days) < 1 && ! $this->_finished
);
891 // dbg_error_log( "RRule", " GetNext: Found %d days for MONTHLY rule", count($days) );
894 else if ( $this->_part
['FREQ'] == "WEEKLY" ) {
895 // dbg_error_log( "RRule", " GetNext: Calculating more dates for WEEKLY rule" );
899 if ( $this->_started
) {
900 $next->AddDays($this->_part
['INTERVAL'] * 7);
903 $this->_started
= true;
906 if ( isset($this->_part
['BYDAY']) ) {
907 $days = $next->GetWeekByDay($this->_part
['BYDAY'], false );
910 $days[$next->_dd
] = $next->_dd
;
912 if ( isset($this->_part
['BYSETPOS']) ) {
913 $days = $next->ApplyBySetpos($this->_part
['BYSETPOS'], $days);
916 $days = $this->WithinScope( $next, $days);
918 while( $limit && count($days) < 1 && ! $this->_finished
);
920 // dbg_error_log( "RRule", " GetNext: Found %d days for WEEKLY rule", count($days) );
922 else if ( $this->_part
['FREQ'] == "DAILY" ) {
923 // dbg_error_log( "RRule", " GetNext: Calculating more dates for DAILY rule" );
927 if ( $this->_started
) {
928 $next->AddDays($this->_part
['INTERVAL']);
931 if ( isset($this->_part
['BYDAY']) ) {
932 $days = $next->GetWeekByDay($this->_part
['BYDAY'], $this->_started
);
935 $days[$next->_dd
] = $next->_dd
;
937 if ( isset($this->_part
['BYSETPOS']) ) {
938 $days = $next->ApplyBySetpos($this->_part
['BYSETPOS'], $days);
941 $days = $this->WithinScope( $next, $days);
942 $this->_started
= true;
944 while( $limit && count($days) < 1 && ! $this->_finished
);
946 // dbg_error_log( "RRule", " GetNext: Found %d days for DAILY rule", count($days) );
949 $ptr = $this->_current
;
950 foreach( $days AS $k => $v ) {
951 $this->_dates
[$ptr++
] = $v;
954 if ( isset($this->_dates
[$this->_current
]) ) {
955 // dbg_error_log( "RRule", " GetNext: Returning %s, (%d'th)", $this->_dates[$this->_current]->Render(), $this->_current );
956 return $this->_dates
[$this->_current
];
959 // dbg_error_log( "RRule", " GetNext: Returning null date" );