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 $dtz = parent::getTimezone();
414 $offset = 0 - $dtz->getOffset($gmt);
415 $gmt->modify( $offset .
' seconds' );
417 if ( $this->is_date )
return $gmt->format(
'Ymd');
418 if ( $return_floating_times )
return $gmt->format(
'Ymd\THis');
419 return $gmt->format(
'Ymd\THis') . (!$return_floating_times && isset($this->tzid) ?
'Z' :
'');
426 public function RFC5545($return_floating_times =
false) {
428 if ( isset($this->tzid) && $this->tzid !=
'UTC' ) {
429 $result =
';TZID='.$this->tzid;
431 if ( $this->is_date ) {
432 $result .=
';VALUE=DATE:' . $this->format(
'Ymd');
435 $result .=
':' . $this->format(
'Ymd\THis');
436 if ( !$return_floating_times && isset($this->tzid) && $this->tzid ==
'UTC' ) {
444 #[\ReturnTypeWillChange]
445 public function setTimeZone( $tz ) {
446 if ( is_string($tz) ) {
447 $tz =
new RepeatRuleTimeZone($tz);
448 $this->tzid = $tz->tzid();
450 parent::setTimeZone( $tz );
455 #[\ReturnTypeWillChange]
456 public function getTimeZone() {
466 public static function hasLeapDay($year) {
467 if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) )
return 1;
477 public static function daysInMonth( $year, $month ) {
478 if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
return 30;
479 else if ($month != 2)
return 31;
480 return 28 + RepeatRuleDateTime::hasLeapDay($year);
484 #[\ReturnTypeWillChange]
485 function setDate( $year=
null, $month=
null, $day=
null ) {
486 if ( !isset($year) ) $year = parent::format(
'Y');
487 if ( !isset($month) ) $month = parent::format(
'm');
488 if ( !isset($day) ) $day = parent::format(
'd');
490 $day += RepeatRuleDateTime::daysInMonth($year, $month) + 1;
492 parent::setDate( $year , $month , $day );
496 function setYearDay( $yearday ) {
497 if ( $yearday > 0 ) {
498 $current_yearday = parent::format(
'z') + 1;
501 $current_yearday = (parent::format(
'z') - (365 + parent::format(
'L')));
503 $diff = $yearday - $current_yearday;
504 if ( $diff < 0 ) $this->
modify(
'-P'.-$diff.
'D');
505 else if ( $diff > 0 ) $this->
modify(
'P'.$diff.
'D');
512 return parent::format(
'Y');
516 return parent::format(
'm');
520 return parent::format(
'd');
524 return parent::format(
'H');
528 return parent::format(
'i');
532 return parent::format(
's');
536 return parent::format(
'U');
636 private $current_base;
637 private $current_set;
638 private $original_rule;
639 private $frequency_string;
641 public function __construct( $basedate, $rrule, $is_date=
null, $return_floating_times=
false ) {
642 if ( $return_floating_times ) $basedate->setAsFloat();
643 $this->base = (is_object($basedate) ? $basedate :
new RepeatRuleDateTime($basedate) );
644 $this->original_rule = $rrule;
647 dbg_error_log(
'RRULE',
"Constructing RRULE based on: '%s', rrule: '%s' (float: %s)", $basedate, $rrule, ($return_floating_times ?
"yes" :
"no") );
650 if ( preg_match(
'{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
652 if ( preg_match(
'{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
654 if ( preg_match(
'{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
655 if ( preg_match(
'{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
657 if ( preg_match(
'{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
659 if ( preg_match(
'{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
660 $this->byday = explode(
',',$m[1]);
662 if ( preg_match(
'{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(
',',$m[1]);
663 if ( preg_match(
'{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(
',',$m[1]);
664 if ( preg_match(
'{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(
',',$m[1]);
665 if ( preg_match(
'{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(
',',$m[1]);
666 if ( preg_match(
'{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(
',',$m[1]);
668 if ( preg_match(
'{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(
',',$m[1]);
669 if ( preg_match(
'{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(
',',$m[1]);
670 if ( preg_match(
'{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(
',',$m[1]);
672 if ( !isset($this->interval) ) $this->interval = 1;
675 switch( $this->freq ) {
676 case 'SECONDLY': $freq_name =
'second';
break;
677 case 'MINUTELY': $freq_name =
'minute';
break;
678 case 'HOURLY': $freq_name =
'hour';
break;
679 case 'DAILY': $freq_name =
'day';
break;
680 case 'WEEKLY': $freq_name =
'week';
break;
681 case 'MONTHLY': $freq_name =
'month';
break;
682 case 'YEARLY': $freq_name =
'year';
break;
686 $this->frequency_string = sprintf(
'+%d %s', $this->interval, $freq_name );
687 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() );
688 $this->Start($return_floating_times);
697 return ( isset($this->count) || isset($this->until) );
701 public function set_timezone( $tzstring ) {
702 $this->base->setTimezone(
new DateTimeZone($tzstring));
706 public function Start($return_floating_times=
false) {
707 $this->instances = array();
708 $this->GetMoreInstances($return_floating_times);
710 $this->finished =
false;
714 public function rewind() {
715 $this->position = -1;
724 public function next($return_floating_times=
false) {
726 return $this->current($return_floating_times);
730 public function current($return_floating_times=
false) {
731 if ( !$this->valid() ) {
732 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
'current: not valid at top, return null' );
736 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
738 if ( !isset($this->instances[$this->position]) ) {
739 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"current: \$this->instances[%s] isn't set, return null", $this->position );
743 if ( !$this->valid() ) {
744 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
'current: not valid after GetMoreInstances, return null' );
748 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Returning date from position %d: %s (%s)", $this->position,
749 $this->instances[$this->position]->format(
'c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
751 return $this->instances[$this->position];
755 public function key($return_floating_times=
false) {
756 if ( !$this->valid() )
return null;
757 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
758 if ( !isset($this->keys[$this->position]) ) {
759 $this->keys[$this->position] = $this->instances[$this->position];
761 return $this->keys[$this->position];
765 public function valid() {
766 if ( DEBUG_RRULE && isset($this->instances[$this->position])) {
767 $current = $this->instances[$this->position];
768 dbg_error_log(
'RRULE',
"TimeZone: " . $current->getTimeZone());
769 dbg_error_log(
'RRULE',
"Date: " . $current->format(
'r'));
770 dbg_log_array(
'RRULE',
"Errors:", $current->getLastErrors());
772 if ( isset($this->instances[$this->position]) || !$this->finished )
return true;
787 return array(
'bymonth' =>
'expand',
'byweekno' =>
'expand',
'byyearday' =>
'expand',
'bymonthday' =>
'expand',
788 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
790 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'expand',
791 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
793 return array(
'bymonth' =>
'limit',
794 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
796 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
797 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
799 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
800 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
802 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
803 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'expand' );
805 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
806 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'limit' );
808 dbg_error_log(
'ERROR',
'Invalid frequency code "%s" - pretending it is "DAILY"', $freq);
809 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
810 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
813 private function GetMoreInstances($return_floating_times=
false) {
815 if ( $this->finished )
return;
818 if ( $return_floating_times ) $this->base->setAsFloat();
819 while( !$this->finished && !$got_more) {
820 if ($loops++ > $c->rrule_loop_limit ) {
821 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);
825 if ( !isset($this->current_base) ) {
826 $this->current_base = clone($this->base);
829 $this->current_base->modify( $this->frequency_string );
831 if ( $return_floating_times ) $this->current_base->setAsFloat();
832 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 );
833 $this->current_set = array( clone($this->current_base) );
834 foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
835 if ( isset($this->{$bytype}) ) {
836 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Going to find more instances by running %s_%s()", $action, $bytype );
837 $this->{$action.
'_'.$bytype}();
838 if ( !isset($this->current_set[0]) )
break;
842 sort($this->current_set);
843 if ( isset($this->bysetpos) ) $this->limit_bysetpos();
845 $position = count($this->instances) - 1;
846 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Inserting %d from current_set into position %d", count($this->current_set), $position + 1 );
848 foreach( $this->current_set AS $k => $instance ) {
849 if ( $instance < $this->base )
continue;
850 if ( isset($this->until) && $instance > $this->until ) {
851 $this->finished =
true;
854 if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
857 if ( isset($this->count) && $position >= $this->count ) {
858 $this->finished =
true;
861 $this->instances[$position] = $instance;
862 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Added date %s into position %d in current set", $instance->format(
'c'), $position );
869 public static function rrule_day_number( $day ) {
883 static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
884 $date_parts = explode(
',',$date->format(
'Y,m,d,H,i,s'));
886 if ( isset($y) || isset($mo) || isset($d) ) {
887 if ( isset($y) ) $date_parts[0] = $y;
888 if ( isset($mo) ) $date_parts[1] = $mo;
889 if ( isset($d) ) $date_parts[2] = $d;
890 $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
892 if ( isset($h) || isset($mi) || isset($s) ) {
893 if ( isset($h) ) $date_parts[3] = $h;
894 if ( isset($mi) ) $date_parts[4] = $mi;
895 if ( isset($s) ) $date_parts[5] = $s;
896 $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
902 private function expand_bymonth() {
903 $instances = $this->current_set;
904 $this->current_set = array();
905 foreach( $instances AS $k => $instance ) {
906 foreach( $this->bymonth AS $k => $month ) {
907 $expanded = $this->date_mask( clone($instance),
null, $month,
null,
null,
null,
null);
908 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYMONTH $month into date %s", $expanded->format(
'c') );
909 $this->current_set[] = $expanded;
914 private function expand_bymonthday() {
915 $instances = $this->current_set;
916 $this->current_set = array();
917 foreach( $instances AS $k => $instance ) {
918 foreach( $this->bymonthday AS $k => $monthday ) {
919 $expanded = $this->date_mask( clone($instance),
null,
null, $monthday,
null,
null,
null);
920 if ($monthday == -1 || $expanded->format(
'd') == $monthday) {
921 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYMONTHDAY $monthday into date %s from %s", $expanded->format(
'c'), $instance->format(
'c') );
922 $this->current_set[] = $expanded;
924 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') );
930 private function expand_byyearday() {
931 $instances = $this->current_set;
932 $this->current_set = array();
934 foreach( $instances AS $k => $instance ) {
935 foreach( $this->byyearday AS $k => $yearday ) {
936 $on_yearday = clone($instance);
937 $on_yearday->setYearDay($yearday);
938 if ( isset($days_set[$on_yearday->UTC()]) )
continue;
939 $this->current_set[] = $on_yearday;
940 $days_set[$on_yearday->UTC()] =
true;
952 $dow_of_instance = $day_in_week->format(
'w');
953 foreach( $this->byday AS $k => $weekday ) {
954 $dow = self::rrule_day_number($weekday);
955 $offset = $dow - $dow_of_instance;
956 if ( $offset < 0 ) $offset += 7;
957 $expanded = clone($day_in_week);
958 $expanded->modify( sprintf(
'+%d day', $offset) );
959 $this->current_set[] = $expanded;
960 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(W) $weekday into date %s", $expanded->format(
'c') );
965 private function expand_byday_in_month( $day_in_month ) {
967 $first_of_month = $this->date_mask( clone($day_in_month),
null,
null, 1,
null,
null,
null);
968 $dow_of_first = $first_of_month->format(
'w');
969 $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format(
'm'), $first_of_month->format(
'Y'));
970 foreach( $this->byday AS $k => $weekday ) {
971 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
972 $dow = self::rrule_day_number($matches[3]);
973 $first_dom = 1 + $dow - $dow_of_first;
if ( $first_dom < 1 ) $first_dom +=7;
974 $whichweek = intval($matches[2]);
975 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanding BYDAY(M) $weekday in month of %s", $first_of_month->format(
'c') );
976 if ( $whichweek > 0 ) {
978 $monthday = $first_dom;
979 if ( $matches[1] ==
'-' ) {
981 while( $monthday > $days_in_month ) $monthday -= 7;
982 $monthday -= (7 * $whichweek);
985 $monthday += (7 * $whichweek);
987 if ( $monthday > 0 && $monthday <= $days_in_month ) {
988 $expanded = $this->date_mask( clone($day_in_month),
null,
null, $monthday,
null,
null,
null);
989 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(M) $weekday now $monthday into date %s", $expanded->format(
'c') );
990 $this->current_set[] = $expanded;
994 for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
995 $expanded = $this->date_mask( clone($day_in_month),
null,
null, $monthday,
null,
null,
null);
996 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(M) $weekday now $monthday into date %s", $expanded->format(
'c') );
997 $this->current_set[] = $expanded;
1005 private function expand_byday_in_year( $day_in_year ) {
1007 $first_of_year = $this->date_mask( clone($day_in_year),
null, 1, 1,
null,
null,
null);
1008 $dow_of_first = $first_of_year->format(
'w');
1009 $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format(
'Y'));
1010 foreach( $this->byday AS $k => $weekday ) {
1011 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
1012 $expanded = clone($first_of_year);
1013 $dow = self::rrule_day_number($matches[3]);
1014 $first_doy = 1 + $dow - $dow_of_first;
if ( $first_doy < 1 ) $first_doy +=7;
1015 $whichweek = intval($matches[2]);
1016 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanding BYDAY(Y) $weekday from date %s", $instance->format(
'c') );
1017 if ( $whichweek > 0 ) {
1019 $yearday = $first_doy;
1020 if ( $matches[1] ==
'-' ) {
1022 while( $yearday > $days_in_year ) $yearday -= 7;
1023 $yearday -= (7 * $whichweek);
1026 $yearday += (7 * $whichweek);
1028 if ( $yearday > 0 && $yearday <= $days_in_year ) {
1029 $expanded->modify(sprintf(
'+%d day', $yearday - 1));
1030 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(Y) $weekday now $yearday into date %s", $expanded->format(
'c') );
1031 $this->current_set[] = $expanded;
1035 $expanded->modify(sprintf(
'+%d day', $first_doy - 1));
1036 for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
1037 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Expanded BYDAY(Y) $weekday now $yearday into date %s", $expanded->format(
'c') );
1038 $this->current_set[] = clone($expanded);
1039 $expanded->modify(
'+1 week');
1048 if ( !isset($this->current_set[0]) )
return;
1049 if ( $this->freq ==
'MONTHLY' || $this->freq ==
'YEARLY' ) {
1050 if ( isset($this->bymonthday) || isset($this->byyearday) ) {
1051 $this->limit_byday();
1055 $instances = $this->current_set;
1056 $this->current_set = array();
1057 foreach( $instances AS $k => $instance ) {
1058 if ( $this->freq ==
'MONTHLY' ) {
1059 $this->expand_byday_in_month($instance);
1061 else if ( $this->freq ==
'WEEKLY' ) {
1065 if ( isset($this->bymonth) ) {
1066 $this->expand_byday_in_month($instance);
1068 else if ( isset($this->byweekno) ) {
1072 $this->expand_byday_in_year($instance);
1079 private function expand_byhour() {
1080 $instances = $this->current_set;
1081 $this->current_set = array();
1082 foreach( $instances AS $k => $instance ) {
1083 foreach( $this->byhour AS $k => $hour ) {
1084 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null, $hour,
null,
null);
1089 private function expand_byminute() {
1090 $instances = $this->current_set;
1091 $this->current_set = array();
1092 foreach( $instances AS $k => $instance ) {
1093 foreach( $this->byminute AS $k => $minute ) {
1094 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null,
null, $minute,
null);
1099 private function expand_bysecond() {
1100 $instances = $this->current_set;
1101 $this->current_set = array();
1102 foreach( $instances AS $k => $instance ) {
1103 foreach( $this->bysecond AS $k => $second ) {
1104 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null,
null,
null, $second);
1110 private function limit_generally( $fmt_char, $element_name ) {
1111 $instances = $this->current_set;
1112 $this->current_set = array();
1113 foreach( $instances AS $k => $instance ) {
1114 foreach( $this->{$element_name} AS $k => $element_value ) {
1115 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') );
1116 if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1121 private function limit_byday() {
1123 $instances = $this->current_set;
1124 $this->current_set = array();
1125 foreach( $this->byday AS $k => $weekday ) {
1126 $dow = self::rrule_day_number($weekday);
1127 foreach( $instances AS $k => $instance ) {
1128 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') );
1129 if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1134 private function limit_bymonth() { $this->limit_generally(
'm',
'bymonth' ); }
1135 private function limit_byyearday() { $this->limit_generally(
'z',
'byyearday' ); }
1136 private function limit_bymonthday() { $this->limit_generally(
'd',
'bymonthday' ); }
1137 private function limit_byhour() { $this->limit_generally(
'H',
'byhour' ); }
1138 private function limit_byminute() { $this->limit_generally(
'i',
'byminute' ); }
1139 private function limit_bysecond() { $this->limit_generally(
's',
'bysecond' ); }
1142 private function limit_bysetpos( ) {
1143 $instances = $this->current_set;
1144 $count = count($instances);
1145 $this->current_set = array();
1146 foreach( $this->bysetpos AS $k => $element_value ) {
1147 if ( DEBUG_RRULE ) dbg_error_log(
'RRULE',
"Limiting bysetpos %s of %d instances", $element_value, $count );
1148 if ( $element_value > 0 ) {
1149 $this->current_set[] = $instances[$element_value - 1];
1151 else if ( $element_value < 0 ) {
1152 $this->current_set[] = $instances[$count + $element_value];