212 public static $Format =
'c';
213 private static $UTCzone;
217 public function __construct($date =
null, $dtz =
null, $is_date =
null ) {
219 $this->is_date =
false;
220 if ( isset($is_date) ) $this->is_date = $is_date;
221 if ( !isset($date) ) {
222 $date = date(
'Ymd\THis');
224 $dtz = self::$UTCzone;
228 if ( is_object($date) && method_exists($date,
'GetParameterValue') ) {
229 $tzid = $date->GetParameterValue(
'TZID');
230 $actual_date = $date->Value();
231 if ( isset($tzid) ) {
233 $this->tzid = $dtz->tzid();
236 $dtz = self::$UTCzone;
237 if ( substr($actual_date,-1) ==
'Z' ) {
239 $actual_date = substr($actual_date, 0, strlen($actual_date) - 1);
242 if ( strlen($actual_date) == 8 ) {
244 $this->is_date =
true;
248 $date = $actual_date;
249 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Date%s property%s: %s%s", ($this->is_date ?
"" :
"Time"),
250 (isset($this->tzid) ?
' with timezone' :
''), $date,
251 (isset($this->tzid) ?
' in '.$this->tzid :
'') );
253 elseif (preg_match(
'/;TZID= ([^:;]+) (?: ;.* )? : ( \d{8} (?:T\d{6})? ) (Z)?/x', $date, $matches) ) {
255 $this->is_date = (strlen($date) == 8);
256 if ( isset($matches[3]) && $matches[3] ==
'Z' ) {
257 $dtz = self::$UTCzone;
260 else if ( isset($matches[1]) && $matches[1] !=
'' ) {
262 $this->tzid = $dtz->tzid();
265 $dtz = self::$UTCzone;
268 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Date%s property%s: %s%s", ($this->is_date ?
"" :
"Time"),
269 (isset($this->tzid) ?
' with timezone' :
''), $date,
270 (isset($this->tzid) ?
' in '.$this->tzid :
'') );
272 elseif ( ( $dtz ===
null || $dtz ==
'' )
273 && preg_match(
'{;VALUE=DATE (?:;[^:]+) : ((?:[12]\d{3}) (?:0[1-9]|1[012]) (?:0[1-9]|[12]\d|3[01]Z?) )$}x', $date, $matches) ) {
274 $this->is_date =
true;
277 $dtz = self::$UTCzone;
279 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Floating Date value: %s", $date );
281 elseif ( $dtz ===
null || $dtz ==
'' ) {
282 $dtz = self::$UTCzone;
283 if ( preg_match(
'/(\d{8}(T\d{6})?) ?(.*)$/', $date, $matches) ) {
285 if ( $matches[3] ==
'Z' ) {
289 $this->tzid = $dtz->tzid();
292 $this->is_date = (strlen($date) == 8 );
293 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Date%s value with timezone 1: %s in %s", ($this->is_date?
"":
"Time"), $date, $this->tzid );
295 elseif ( is_string($dtz) ) {
297 $this->tzid = $dtz->tzid();
298 $type = gettype($date);
299 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Date%s $type with timezone 2: %s in %s", ($this->is_date?
"":
"Time"), $date, $this->tzid );
302 $this->tzid = $dtz->getName();
303 $type = gettype($date);
304 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Date%s $type with timezone 3: %s in %s", ($this->is_date?
"":
"Time"), $date, $this->tzid );
307 parent::__construct($date, $dtz);
308 if ( isset($is_date) ) $this->is_date = $is_date;
313 public static function withFallbackTzid( $date, $fallback_tzid ) {
316 if ($date->GetParameterValue(
'VALUE') ==
'DATE' && isset($fallback_tzid)) {
318 }
else if ($date->GetParameterValue(
'TZID') ===
null && isset($fallback_tzid)) {
326 public function __toString() {
327 return (
string)parent::format(self::$Format) .
' ' . parent::getTimeZone()->getName();
331 public function AsDate() {
332 return $this->format(
'Ymd');
336 public function setAsFloat() {
341 public function isFloating() {
342 return !isset($this->tzid);
345 public function isDate() {
346 return $this->is_date;
350 public function setAsDate() {
351 $this->is_date =
true;
355 #[\ReturnTypeWillChange]
358 if ( preg_match(
'{^(-)?P(([0-9-]+)W)?(([0-9-]+)D)?T?(([0-9-]+)H)?(([0-9-]+)M)?(([0-9-]+)S)?$}', $interval, $matches) ) {
359 $minus = (isset($matches[1])?$matches[1]:
'');
361 if ( isset($matches[2]) && $matches[2] !=
'' ) $interval .= $minus . $matches[3] .
' weeks ';
362 if ( isset($matches[4]) && $matches[4] !=
'' ) $interval .= $minus . $matches[5] .
' days ';
363 if ( isset($matches[6]) && $matches[6] !=
'' ) $interval .= $minus . $matches[7] .
' hours ';
364 if ( isset($matches[8]) && $matches[8] !=
'' ) $interval .= $minus . $matches[9] .
' minutes ';
365 if (isset($matches[10]) &&$matches[10] !=
'' ) $interval .= $minus . $matches[11] .
' seconds ';
367 if ( DEBUG_RRULE) dbg_error_log(
'RRULE',
"Modify '%s' by: >>%s<<\n", $this->__toString(), $interval );
369 if ( !isset($interval) || $interval ==
'' ) $interval =
'1 day';
370 parent::modify($interval);
371 if (DEBUG_RRULE) dbg_error_log(
'RRULE',
"Modified to '%s'", $this->__toString() );
372 return $this->__toString();
383 public function UTC($fmt =
'Ymd\THis\Z' ) {
385 if ( $this->tzid !=
'UTC' ) {
386 if ( isset($this->tzid)) {
387 $dtz = parent::getTimezone();
390 $dtz =
new DateTimeZone(date_default_timezone_get());
392 $offset = 0 - $dtz->getOffset($gmt);
393 $gmt->modify( $offset .
' seconds' );
395 return $gmt->format($fmt);
410 public function FloatOrUTC($return_floating_times =
false) {
412 if ( !$return_floating_times && isset($this->tzid) && $this->tzid !=
'UTC' ) {
413 $gmt->setTimezone(
'UTC');
415 if ( $this->is_date )
return $gmt->format(
'Ymd');
416 if ( $return_floating_times )
return $gmt->format(
'Ymd\THis');
417 return $gmt->format(
'Ymd\THis') . (!$return_floating_times && isset($this->tzid) ?
'Z' :
'');
424 public function RFC5545($return_floating_times =
false) {
426 if ( isset($this->tzid) && $this->tzid !=
'UTC' ) {
427 $result =
';TZID='.$this->tzid;
429 if ( $this->is_date ) {
430 $result .=
';VALUE=DATE:' . $this->format(
'Ymd');
433 $result .=
':' . $this->format(
'Ymd\THis');
434 if ( !$return_floating_times && isset($this->tzid) && $this->tzid ==
'UTC' ) {
442 #[\ReturnTypeWillChange]
443 public function setTimeZone( $tz ) {
444 if ( is_string($tz) ) {
445 $tz =
new RepeatRuleTimeZone($tz);
446 $this->tzid = $tz->tzid();
448 parent::setTimeZone( $tz );
453 #[\ReturnTypeWillChange]
454 public function getTimeZone() {
464 public static function hasLeapDay($year) {
465 if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) )
return 1;
475 public static function daysInMonth( $year, $month ) {
476 if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
return 30;
477 else if ($month != 2)
return 31;
478 return 28 + RepeatRuleDateTime::hasLeapDay($year);
482 #[\ReturnTypeWillChange]
483 function setDate( $year=
null, $month=
null, $day=
null ) {
484 if ( !isset($year) ) $year = parent::format(
'Y');
485 if ( !isset($month) ) $month = parent::format(
'm');
486 if ( !isset($day) ) $day = parent::format(
'd');
488 $day += RepeatRuleDateTime::daysInMonth($year, $month) + 1;
490 parent::setDate( $year , $month , $day );
494 function setYearDay( $yearday ) {
495 if ( $yearday > 0 ) {
496 $current_yearday = parent::format(
'z') + 1;
499 $current_yearday = (parent::format(
'z') - (365 + parent::format(
'L')));
501 $diff = $yearday - $current_yearday;
502 if ( $diff < 0 ) $this->
modify(
'-P'.-$diff.
'D');
503 else if ( $diff > 0 ) $this->
modify(
'P'.$diff.
'D');
510 return parent::format(
'Y');
514 return parent::format(
'm');
518 return parent::format(
'd');
522 return parent::format(
'H');
526 return parent::format(
'i');
530 return parent::format(
's');
534 return parent::format(
'U');
634 private $current_base;
635 private $current_set;
636 private $original_rule;
637 private $frequency_string;
639 public function __construct( $basedate, $rrule, $is_date=
null, $return_floating_times=
false ) {
640 if ( $return_floating_times ) $basedate->setAsFloat();
641 $this->base = (is_object($basedate) ? $basedate :
new RepeatRuleDateTime($basedate) );
642 $this->original_rule = $rrule;
645 dbg_error_log(
'RRULE',
"Constructing RRULE based on: '%s', rrule: '%s' (float: %s)", $basedate, $rrule, ($return_floating_times ?
"yes" :
"no") );
648 if ( preg_match(
'{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
650 if ( preg_match(
'{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
652 if ( preg_match(
'{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
653 if ( preg_match(
'{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
655 if ( preg_match(
'{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
657 if ( preg_match(
'{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
658 $this->byday = explode(
',',$m[1]);
660 if ( preg_match(
'{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(
',',$m[1]);
661 if ( preg_match(
'{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(
',',$m[1]);
662 if ( preg_match(
'{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(
',',$m[1]);
663 if ( preg_match(
'{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(
',',$m[1]);
664 if ( preg_match(
'{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(
',',$m[1]);
666 if ( preg_match(
'{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(
',',$m[1]);
667 if ( preg_match(
'{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(
',',$m[1]);
668 if ( preg_match(
'{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(
',',$m[1]);
670 if ( !isset($this->interval) ) $this->interval = 1;
673 switch( $this->freq ) {
674 case 'SECONDLY': $freq_name =
'second';
break;
675 case 'MINUTELY': $freq_name =
'minute';
break;
676 case 'HOURLY': $freq_name =
'hour';
break;
677 case 'DAILY': $freq_name =
'day';
break;
678 case 'WEEKLY': $freq_name =
'week';
break;
679 case 'MONTHLY': $freq_name =
'month';
break;
680 case 'YEARLY': $freq_name =
'year';
break;
684 $this->frequency_string = sprintf(
'+%d %s', $this->interval, $freq_name );
685 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Frequency modify string is: '%s', base is: '%s', TZ: %s", $this->frequency_string, $this->base->format(
'c'), $this->base->getTimeZone() );
686 $this->Start($return_floating_times);
695 return ( isset($this->count) || isset($this->until) );
699 public function set_timezone( $tzstring ) {
700 $this->base->setTimezone(
new DateTimeZone($tzstring));
704 public function Start($return_floating_times=
false) {
705 $this->instances = array();
706 $this->GetMoreInstances($return_floating_times);
708 $this->finished =
false;
712 public function rewind() {
713 $this->position = -1;
722 public function next($return_floating_times=
false) {
724 return $this->current($return_floating_times);
728 public function current($return_floating_times=
false) {
729 if ( !$this->valid() ) {
730 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
'current: not valid at top, return null' );
734 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
736 if ( !isset($this->instances[$this->position]) ) {
737 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"current: \$this->instances[%s] isn't set, return null", $this->position );
741 if ( !$this->valid() ) {
742 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
'current: not valid after GetMoreInstances, return null' );
746 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Returning date from position %d: %s (%s)", $this->position,
747 $this->instances[$this->position]->format(
'c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
749 return $this->instances[$this->position];
753 public function key($return_floating_times=
false) {
754 if ( !$this->valid() )
return null;
755 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
756 if ( !isset($this->keys[$this->position]) ) {
757 $this->keys[$this->position] = $this->instances[$this->position];
759 return $this->keys[$this->position];
763 public function valid() {
764 if ( DEBUG_RRULE && isset($this->instances[$this->position])) {
765 $current = $this->instances[$this->position];
766 dbg_error_log(
'RRULE',
"TimeZone: " . $current->getTimeZone());
767 dbg_error_log(
'RRULE',
"Date: " . $current->format(
'r'));
768 dbg_log_array(
'RRULE',
"Errors:", $current->getLastErrors());
770 if ( isset($this->instances[$this->position]) || !$this->finished )
return true;
785 return array(
'bymonth' =>
'expand',
'byweekno' =>
'expand',
'byyearday' =>
'expand',
'bymonthday' =>
'expand',
786 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
788 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'expand',
789 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
791 return array(
'bymonth' =>
'limit',
792 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
794 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
795 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
797 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
798 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
800 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
801 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'expand' );
803 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
804 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'limit' );
806 dbg_error_log(
'ERROR',
'Invalid frequency code "%s" - pretending it is "DAILY"', $freq);
807 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
808 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
811 private function GetMoreInstances($return_floating_times=
false) {
813 if ( $this->finished )
return;
816 if ( $return_floating_times ) $this->base->setAsFloat();
817 while( !$this->finished && !$got_more) {
818 if ($loops++ > $c->rrule_loop_limit ) {
819 dbg_error_log (
'ERROR',
"RRULE, loop limit has been hit in GetMoreInstances, you probably want to increase \$c->rrule_loop_limit (currently %d)", $c->rrule_loop_limit);
823 if ( !isset($this->current_base) ) {
824 $this->current_base = clone($this->base);
827 $this->current_base->modify( $this->frequency_string );
829 if ( $return_floating_times ) $this->current_base->setAsFloat();
830 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Getting more instances from: '%s' - %d, TZ: %s, Loop: %s", $this->current_base->format(
'c'), count($this->instances), $this->current_base->getTimeZone(), $loops );
831 $this->current_set = array( clone($this->current_base) );
832 foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
833 if ( isset($this->{$bytype}) ) {
834 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Going to find more instances by running %s_%s()", $action, $bytype );
835 $this->{$action.
'_'.$bytype}();
836 if ( !isset($this->current_set[0]) )
break;
840 sort($this->current_set);
841 if ( isset($this->bysetpos) ) $this->limit_bysetpos();
843 $position = count($this->instances) - 1;
844 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Inserting %d from current_set into position %d", count($this->current_set), $position + 1 );
846 foreach( $this->current_set AS $k => $instance ) {
847 if ( $instance < $this->base )
continue;
848 if ( isset($this->until) && $instance > $this->until ) {
849 $this->finished =
true;
852 if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
855 if ( isset($this->count) && $position >= $this->count ) {
856 $this->finished =
true;
859 $this->instances[$position] = $instance;
860 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Added date %s into position %d in current set", $instance->format(
'c'), $position );
867 public static function rrule_day_number( $day ) {
881 static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
882 $date_parts = explode(
',',$date->format(
'Y,m,d,H,i,s'));
884 if ( isset($y) || isset($mo) || isset($d) ) {
885 if ( isset($y) ) $date_parts[0] = $y;
886 if ( isset($mo) ) $date_parts[1] = $mo;
887 if ( isset($d) ) $date_parts[2] = $d;
888 $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
890 if ( isset($h) || isset($mi) || isset($s) ) {
891 if ( isset($h) ) $date_parts[3] = $h;
892 if ( isset($mi) ) $date_parts[4] = $mi;
893 if ( isset($s) ) $date_parts[5] = $s;
894 $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
900 private function expand_bymonth() {
901 $instances = $this->current_set;
902 $this->current_set = array();
903 foreach( $instances AS $k => $instance ) {
904 foreach( $this->bymonth AS $k => $month ) {
905 $expanded = $this->date_mask( clone($instance),
null, $month,
null,
null,
null,
null);
906 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYMONTH $month into date %s", $expanded->format(
'c') );
907 $this->current_set[] = $expanded;
912 private function expand_bymonthday() {
913 $instances = $this->current_set;
914 $this->current_set = array();
915 foreach( $instances AS $k => $instance ) {
916 foreach( $this->bymonthday AS $k => $monthday ) {
917 $expanded = $this->date_mask( clone($instance),
null,
null, $monthday,
null,
null,
null);
918 if ($monthday == -1 || $expanded->format(
'd') == $monthday) {
919 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYMONTHDAY $monthday into date %s from %s", $expanded->format(
'c'), $instance->format(
'c') );
920 $this->current_set[] = $expanded;
922 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYMONTHDAY $monthday into date %s from %s, which is not the same day of month, skipping.", $expanded->format(
'c'), $instance->format(
'c') );
928 private function expand_byyearday() {
929 $instances = $this->current_set;
930 $this->current_set = array();
932 foreach( $instances AS $k => $instance ) {
933 foreach( $this->byyearday AS $k => $yearday ) {
934 $on_yearday = clone($instance);
935 $on_yearday->setYearDay($yearday);
936 if ( isset($days_set[$on_yearday->UTC()]) )
continue;
937 $this->current_set[] = $on_yearday;
938 $days_set[$on_yearday->UTC()] =
true;
950 $dow_of_instance = $day_in_week->format(
'w');
951 foreach( $this->byday AS $k => $weekday ) {
952 $dow = self::rrule_day_number($weekday);
953 $offset = $dow - $dow_of_instance;
954 if ( $offset < 0 ) $offset += 7;
955 $expanded = clone($day_in_week);
956 $expanded->modify( sprintf(
'+%d day', $offset) );
957 $this->current_set[] = $expanded;
958 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(W) $weekday into date %s", $expanded->format(
'c') );
963 private function expand_byday_in_month( $day_in_month ) {
965 $first_of_month = $this->date_mask( clone($day_in_month),
null,
null, 1,
null,
null,
null);
966 $dow_of_first = $first_of_month->format(
'w');
967 $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format(
'm'), $first_of_month->format(
'Y'));
968 foreach( $this->byday AS $k => $weekday ) {
969 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
970 $dow = self::rrule_day_number($matches[3]);
971 $first_dom = 1 + $dow - $dow_of_first;
if ( $first_dom < 1 ) $first_dom +=7;
972 $whichweek = intval($matches[2]);
973 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanding BYDAY(M) $weekday in month of %s", $first_of_month->format(
'c') );
974 if ( $whichweek > 0 ) {
976 $monthday = $first_dom;
977 if ( $matches[1] ==
'-' ) {
979 while( $monthday > $days_in_month ) $monthday -= 7;
980 $monthday -= (7 * $whichweek);
983 $monthday += (7 * $whichweek);
985 if ( $monthday > 0 && $monthday <= $days_in_month ) {
986 $expanded = $this->date_mask( clone($day_in_month),
null,
null, $monthday,
null,
null,
null);
987 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(M) $weekday now $monthday into date %s", $expanded->format(
'c') );
988 $this->current_set[] = $expanded;
992 for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
993 $expanded = $this->date_mask( clone($day_in_month),
null,
null, $monthday,
null,
null,
null);
994 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(M) $weekday now $monthday into date %s", $expanded->format(
'c') );
995 $this->current_set[] = $expanded;
1003 private function expand_byday_in_year( $day_in_year ) {
1005 $first_of_year = $this->date_mask( clone($day_in_year),
null, 1, 1,
null,
null,
null);
1006 $dow_of_first = $first_of_year->format(
'w');
1007 $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format(
'Y'));
1008 foreach( $this->byday AS $k => $weekday ) {
1009 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
1010 $expanded = clone($first_of_year);
1011 $dow = self::rrule_day_number($matches[3]);
1012 $first_doy = 1 + $dow - $dow_of_first;
if ( $first_doy < 1 ) $first_doy +=7;
1013 $whichweek = intval($matches[2]);
1014 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanding BYDAY(Y) $weekday from date %s", $instance->format(
'c') );
1015 if ( $whichweek > 0 ) {
1017 $yearday = $first_doy;
1018 if ( $matches[1] ==
'-' ) {
1020 while( $yearday > $days_in_year ) $yearday -= 7;
1021 $yearday -= (7 * $whichweek);
1024 $yearday += (7 * $whichweek);
1026 if ( $yearday > 0 && $yearday <= $days_in_year ) {
1027 $expanded->modify(sprintf(
'+%d day', $yearday - 1));
1028 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(Y) $weekday now $yearday into date %s", $expanded->format(
'c') );
1029 $this->current_set[] = $expanded;
1033 $expanded->modify(sprintf(
'+%d day', $first_doy - 1));
1034 for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
1035 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(Y) $weekday now $yearday into date %s", $expanded->format(
'c') );
1036 $this->current_set[] = clone($expanded);
1037 $expanded->modify(
'+1 week');
1046 if ( !isset($this->current_set[0]) )
return;
1047 if ( $this->freq ==
'MONTHLY' || $this->freq ==
'YEARLY' ) {
1048 if ( isset($this->bymonthday) || isset($this->byyearday) ) {
1049 $this->limit_byday();
1053 $instances = $this->current_set;
1054 $this->current_set = array();
1055 foreach( $instances AS $k => $instance ) {
1056 if ( $this->freq ==
'MONTHLY' ) {
1057 $this->expand_byday_in_month($instance);
1059 else if ( $this->freq ==
'WEEKLY' ) {
1063 if ( isset($this->bymonth) ) {
1064 $this->expand_byday_in_month($instance);
1066 else if ( isset($this->byweekno) ) {
1070 $this->expand_byday_in_year($instance);
1077 private function expand_byhour() {
1078 $instances = $this->current_set;
1079 $this->current_set = array();
1080 foreach( $instances AS $k => $instance ) {
1081 foreach( $this->byhour AS $k => $hour ) {
1082 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null, $hour,
null,
null);
1087 private function expand_byminute() {
1088 $instances = $this->current_set;
1089 $this->current_set = array();
1090 foreach( $instances AS $k => $instance ) {
1091 foreach( $this->byminute AS $k => $minute ) {
1092 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null,
null, $minute,
null);
1097 private function expand_bysecond() {
1098 $instances = $this->current_set;
1099 $this->current_set = array();
1100 foreach( $instances AS $k => $instance ) {
1101 foreach( $this->bysecond AS $k => $second ) {
1102 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null,
null,
null, $second);
1108 private function limit_generally( $fmt_char, $element_name ) {
1109 $instances = $this->current_set;
1110 $this->current_set = array();
1111 foreach( $instances AS $k => $instance ) {
1112 foreach( $this->{$element_name} AS $k => $element_value ) {
1113 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Limiting '$fmt_char' on '%s' => '%s' ?=? '%s' ? %s", $instance->format(
'c'), $instance->format($fmt_char), $element_value, ($instance->format($fmt_char) == $element_value ?
'Yes' :
'No') );
1114 if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1119 private function limit_byday() {
1121 $instances = $this->current_set;
1122 $this->current_set = array();
1123 foreach( $this->byday AS $k => $weekday ) {
1124 $dow = self::rrule_day_number($weekday);
1125 foreach( $instances AS $k => $instance ) {
1126 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Limiting '$fmt_char' on '%s' => '%s' ?=? '%s' (%d) ? %s", $instance->format(
'c'), $instance->format($fmt_char), $weekday, $dow, ($instance->format($fmt_char) == $dow ?
'Yes' :
'No') );
1127 if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1132 private function limit_bymonth() { $this->limit_generally(
'm',
'bymonth' ); }
1133 private function limit_byyearday() { $this->limit_generally(
'z',
'byyearday' ); }
1134 private function limit_bymonthday() { $this->limit_generally(
'd',
'bymonthday' ); }
1135 private function limit_byhour() { $this->limit_generally(
'H',
'byhour' ); }
1136 private function limit_byminute() { $this->limit_generally(
'i',
'byminute' ); }
1137 private function limit_bysecond() { $this->limit_generally(
's',
'bysecond' ); }
1140 private function limit_bysetpos( ) {
1141 $instances = $this->current_set;
1142 $count = count($instances);
1143 $this->current_set = array();
1144 foreach( $this->bysetpos AS $k => $element_value ) {
1145 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Limiting bysetpos %s of %d instances", $element_value, $count );
1146 if ( $element_value > 0 ) {
1147 $this->current_set[] = $instances[$element_value - 1];
1149 else if ( $element_value < 0 ) {
1150 $this->current_set[] = $instances[$count + $element_value];