12if ( !class_exists(
'DateTime') )
return;
19function olson_from_vtimezone( vComponent $vtz ) {
20 $tzid = $vtz->GetProperty(
'TZID');
21 if ( empty($tzid) ) $tzid = $vtz->GetProperty(
'TZID');
22 if ( !empty($tzid) ) {
23 $result = olson_from_tzstring($tzid);
24 if ( !empty($result) )
return $result;
34define(
'DEBUG_RRULE',
false );
42 public function __construct($in_dtz =
null) {
43 $this->tz_defined =
false;
44 if ( !isset($in_dtz) )
return;
46 $olson = olson_from_tzstring($in_dtz);
47 if ( isset($olson) ) {
49 parent::__construct($olson);
50 $this->tz_defined = $olson;
52 catch (Exception $e) {
53 dbg_error_log(
'ERROR',
'Could not handle timezone "%s" (%s) - will use floating time', $in_dtz, $olson );
54 parent::__construct(
'UTC');
55 $this->tz_defined =
false;
59 dbg_error_log(
'ERROR',
'Could not recognize timezone "%s" - will use floating time', $in_dtz );
60 parent::__construct(
'UTC');
61 $this->tz_defined =
false;
66 if ( $this->tz_defined ===
false )
return false;
67 $tzid = $this->getName();
68 if ( $tzid !=
'UTC' )
return $tzid;
69 return $this->tz_defined;
80 private $epoch_seconds =
null;
83 private $as_text =
'';
90 if ( is_integer($in_duration) ) {
91 $this->epoch_seconds = $in_duration;
94 else if ( gettype($in_duration) ==
'string' ) {
96 $this->as_text = $in_duration;
97 $this->epoch_seconds =
null;
110 if ( $this == $other )
return true;
111 if ( $this->
asSeconds() == $other->asSeconds() )
return true;
119 if ( !isset($this->epoch_seconds) ) {
120 if ( preg_match(
'{^(-?)P(?:(\d+W)|(?:(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S?)?)?))$}i', $this->as_text, $matches) ) {
123 if ( !empty($matches[2]) ) {
124 $this->days = (intval($matches[2]) * 7);
127 if ( !empty($matches[3]) ) $this->days = intval($matches[3]);
128 if ( !empty($matches[4]) ) $this->secs += intval($matches[4]) * 3600;
129 if ( !empty($matches[5]) ) $this->secs += intval($matches[5]) * 60;
130 if ( !empty($matches[6]) ) $this->secs += intval($matches[6]);
132 if ( $matches[1] ==
'-' ) {
136 $this->epoch_seconds = ($this->days * 86400) + $this->secs;
140 throw new Exception(
'Invalid epoch: "'+$this->as_text+
"'");
143 return $this->epoch_seconds;
152 if ( empty($this->as_text) ) {
153 $this->as_text = ($this->epoch_seconds < 0 ?
'-P' :
'P');
154 $in_duration = abs($this->epoch_seconds);
155 if ( $in_duration == 0 ) {
156 $this->as_text .=
'0D';
157 } elseif ( $in_duration >= 86400 ) {
158 $this->days = floor($in_duration / 86400);
159 $in_duration -= $this->days * 86400;
160 if ( $in_duration == 0 && ($this->days / 7) == floor($this->days / 7) ) {
161 $this->as_text .= ($this->days/7).
'W';
162 return $this->as_text;
164 $this->as_text .= $this->days.
'D';
166 if ( $in_duration > 0 ) {
167 $secs = $in_duration;
168 $this->as_text .=
'T';
169 $hours = floor($in_duration / 3600);
170 if ( $hours > 0 ) $this->as_text .= $hours .
'H';
171 $minutes = floor(($in_duration % 3600) / 60);
172 if ( $minutes > 0 ) $this->as_text .= $minutes .
'M';
173 $seconds = $in_duration % 60;
174 if ( $seconds > 0 ) $this->as_text .= $seconds .
'S';
177 return $this->as_text;
201 $diff = $d2->epoch() - $d1->epoch();
214 public static $Format =
'c';
215 private static $UTCzone;
219 public function __construct($date =
null, $dtz =
null, $is_date =
null ) {
221 $this->is_date =
false;
222 if ( isset($is_date) ) $this->is_date = $is_date;
223 if ( !isset($date) ) {
224 $date = date(
'Ymd\THis');
226 $dtz = self::$UTCzone;
230 if ( is_object($date) && method_exists($date,
'GetParameterValue') ) {
231 $tzid = $date->GetParameterValue(
'TZID');
232 $actual_date = $date->Value();
233 if ( isset($tzid) ) {
235 $this->tzid = $dtz->tzid();
238 $dtz = self::$UTCzone;
239 if ( substr($actual_date,-1) ==
'Z' ) {
241 $actual_date = substr($actual_date, 0, strlen($actual_date) - 1);
244 if ( strlen($actual_date) == 8 ) {
246 $this->is_date =
true;
250 $date = $actual_date;
251 if ( DEBUG_RRULE ) printf(
"Date%s property%s: %s%s\n", ($this->is_date ?
"" :
"Time"),
252 (isset($this->tzid) ?
' with timezone' :
''), $date,
253 (isset($this->tzid) ?
' in '.$this->tzid :
'') );
255 elseif (preg_match(
'/;TZID= ([^:;]+) (?: ;.* )? : ( \d{8} (?:T\d{6})? ) (Z)?/x', $date, $matches) ) {
257 $this->is_date = (strlen($date) == 8);
258 if ( isset($matches[3]) && $matches[3] ==
'Z' ) {
259 $dtz = self::$UTCzone;
262 else if ( isset($matches[1]) && $matches[1] !=
'' ) {
264 $this->tzid = $dtz->tzid();
267 $dtz = self::$UTCzone;
270 if ( DEBUG_RRULE ) printf(
"Date%s property%s: %s%s\n", ($this->is_date ?
"" :
"Time"),
271 (isset($this->tzid) ?
' with timezone' :
''), $date,
272 (isset($this->tzid) ?
' in '.$this->tzid :
'') );
274 elseif ( ( $dtz ===
null || $dtz ==
'' )
275 && preg_match(
'{;VALUE=DATE (?:;[^:]+) : ((?:[12]\d{3}) (?:0[1-9]|1[012]) (?:0[1-9]|[12]\d|3[01]Z?) )$}x', $date, $matches) ) {
276 $this->is_date =
true;
279 $dtz = self::$UTCzone;
281 if ( DEBUG_RRULE ) printf(
"Floating Date value: %s\n", $date );
283 elseif ( $dtz ===
null || $dtz ==
'' ) {
284 $dtz = self::$UTCzone;
285 if ( preg_match(
'/(\d{8}(T\d{6})?) ?(.*)$/', $date, $matches) ) {
287 if ( $matches[3] ==
'Z' ) {
291 $this->tzid = $dtz->tzid();
294 $this->is_date = (strlen($date) == 8 );
295 if ( DEBUG_RRULE ) printf(
"Date%s value with timezone 1: %s in %s\n", ($this->is_date?
"":
"Time"), $date, $this->tzid );
297 elseif ( is_string($dtz) ) {
299 $this->tzid = $dtz->tzid();
300 $type = gettype($date);
301 if ( DEBUG_RRULE ) printf(
"Date%s $type with timezone 2: %s in %s\n", ($this->is_date?
"":
"Time"), $date, $this->tzid );
304 $this->tzid = $dtz->getName();
305 $type = gettype($date);
306 if ( DEBUG_RRULE ) printf(
"Date%s $type with timezone 3: %s in %s\n", ($this->is_date?
"":
"Time"), $date, $this->tzid );
309 parent::__construct($date, $dtz);
310 if ( isset($is_date) ) $this->is_date = $is_date;
315 public static function withFallbackTzid( $date, $fallback_tzid ) {
318 if ($date->GetParameterValue(
'VALUE') ==
'DATE' && isset($fallback_tzid)) {
320 }
else if ($date->GetParameterValue(
'TZID') ===
null && isset($fallback_tzid)) {
328 public function __toString() {
329 return (
string)parent::format(self::$Format) .
' ' . parent::getTimeZone()->getName();
333 public function AsDate() {
334 return $this->format(
'Ymd');
338 public function setAsFloat() {
343 public function isFloating() {
344 return !isset($this->tzid);
347 public function isDate() {
348 return $this->is_date;
352 public function setAsDate() {
353 $this->is_date =
true;
357 #[\ReturnTypeWillChange]
358 public function modify( $interval ) {
360 if ( preg_match(
'{^(-)?P(([0-9-]+)W)?(([0-9-]+)D)?T?(([0-9-]+)H)?(([0-9-]+)M)?(([0-9-]+)S)?$}', $interval, $matches) ) {
361 $minus = (isset($matches[1])?$matches[1]:
'');
363 if ( isset($matches[2]) && $matches[2] !=
'' ) $interval .= $minus . $matches[3] .
' weeks ';
364 if ( isset($matches[4]) && $matches[4] !=
'' ) $interval .= $minus . $matches[5] .
' days ';
365 if ( isset($matches[6]) && $matches[6] !=
'' ) $interval .= $minus . $matches[7] .
' hours ';
366 if ( isset($matches[8]) && $matches[8] !=
'' ) $interval .= $minus . $matches[9] .
' minutes ';
367 if (isset($matches[10]) &&$matches[10] !=
'' ) $interval .= $minus . $matches[11] .
' seconds ';
371 if ( !isset($interval) || $interval ==
'' ) $interval =
'1 day';
372 if ( parent::format(
'd') > 28 && strstr($interval,
'month') !== false ) {
373 $this->setDate(
null,
null,28);
375 parent::modify($interval);
376 return $this->__toString();
387 public function UTC($fmt =
'Ymd\THis\Z' ) {
389 if ( $this->tzid !=
'UTC' ) {
390 if ( isset($this->tzid)) {
391 $dtz = parent::getTimezone();
394 $dtz =
new DateTimeZone(date_default_timezone_get());
396 $offset = 0 - $dtz->getOffset($gmt);
397 $gmt->modify( $offset .
' seconds' );
399 return $gmt->format($fmt);
416 if ( !$return_floating_times && isset($this->tzid) && $this->tzid !=
'UTC' ) {
417 $dtz = parent::getTimezone();
418 $offset = 0 - $dtz->getOffset($gmt);
419 $gmt->modify( $offset .
' seconds' );
421 if ( $this->is_date )
return $gmt->format(
'Ymd');
422 if ( $return_floating_times )
return $gmt->format(
'Ymd\THis');
423 return $gmt->format(
'Ymd\THis') . (!$return_floating_times && isset($this->tzid) ?
'Z' :
'');
430 public function RFC5545($return_floating_times =
false) {
432 if ( isset($this->tzid) && $this->tzid !=
'UTC' ) {
433 $result =
';TZID='.$this->tzid;
435 if ( $this->is_date ) {
436 $result .=
';VALUE=DATE:' . $this->format(
'Ymd');
439 $result .=
':' . $this->format(
'Ymd\THis');
440 if ( !$return_floating_times && isset($this->tzid) && $this->tzid ==
'UTC' ) {
448 #[\ReturnTypeWillChange]
449 public function setTimeZone( $tz ) {
450 if ( is_string($tz) ) {
452 $this->tzid = $tz->tzid();
454 parent::setTimeZone( $tz );
459 #[\ReturnTypeWillChange]
460 public function getTimeZone() {
471 if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) )
return 1;
482 if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
return 30;
483 else if ($month != 2)
return 31;
488 #[\ReturnTypeWillChange]
489 function setDate( $year=
null, $month=
null, $day=
null ) {
490 if ( !isset($year) ) $year = parent::format(
'Y');
491 if ( !isset($month) ) $month = parent::format(
'm');
492 if ( !isset($day) ) $day = parent::format(
'd');
496 parent::setDate( $year , $month , $day );
500 function setYearDay( $yearday ) {
501 if ( $yearday > 0 ) {
502 $current_yearday = parent::format(
'z') + 1;
505 $current_yearday = (parent::format(
'z') - (365 + parent::format(
'L')));
507 $diff = $yearday - $current_yearday;
508 if ( $diff < 0 ) $this->modify(
'-P'.-$diff.
'D');
509 else if ( $diff > 0 ) $this->modify(
'P'.$diff.
'D');
516 return parent::format(
'Y');
520 return parent::format(
'm');
524 return parent::format(
'd');
528 return parent::format(
'H');
532 return parent::format(
'i');
536 return parent::format(
's');
540 return parent::format(
'U');
565 if ( $date1 !=
null && $date2 !=
null && $date1 > $date2 ) {
566 $this->from = $date2;
567 $this->until = $date1;
570 $this->from = $date1;
571 $this->until = $date2;
581 if ( ($this->until ==
null && $this->from ==
null) || ($other->until ==
null && $other->from ==
null ) )
return true;
582 if ( $this->until ==
null && $other->until ==
null )
return true;
583 if ( $this->from ==
null && $other->from ==
null )
return true;
585 if ( $this->until ==
null )
return ($other->until > $this->from);
586 if ( $this->from ==
null )
return ($other->from < $this->until);
587 if ( $other->until ==
null )
return ($this->until > $other->from);
588 if ( $other->from ==
null )
return ($this->from < $other->until);
590 return !( $this->until < $other->from || $this->from > $other->until );
600 if ( !isset($this->from) )
return null;
601 if ( $this->from->isDate() && !isset($this->until) )
603 else if ( !isset($this->until) )
606 $duration = ( $this->until->epoch() - $this->from->epoch() );
640 private $current_base;
641 private $original_rule;
644 public function __construct( $basedate, $rrule, $is_date=
null, $return_floating_times=
false ) {
645 if ( $return_floating_times ) $basedate->setAsFloat();
646 $this->base = (is_object($basedate) ? $basedate :
new RepeatRuleDateTime($basedate) );
647 $this->original_rule = $rrule;
650 printf(
"Constructing RRULE based on: '%s', rrule: '%s' (float: %s)\n", $basedate, $rrule, ($return_floating_times ?
"yes" :
"no") );
653 if ( preg_match(
'{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
655 if ( preg_match(
'{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
657 if ( preg_match(
'{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
658 if ( preg_match(
'{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
660 if ( preg_match(
'{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
662 if ( preg_match(
'{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
663 $this->byday = explode(
',',$m[1]);
665 if ( preg_match(
'{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(
',',$m[1]);
666 if ( preg_match(
'{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(
',',$m[1]);
667 if ( preg_match(
'{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(
',',$m[1]);
668 if ( preg_match(
'{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(
',',$m[1]);
669 if ( preg_match(
'{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(
',',$m[1]);
671 if ( preg_match(
'{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(
',',$m[1]);
672 if ( preg_match(
'{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(
',',$m[1]);
673 if ( preg_match(
'{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(
',',$m[1]);
675 if ( !isset($this->interval) ) $this->interval = 1;
676 switch( $this->freq ) {
677 case 'SECONDLY': $this->freq_name =
'second';
break;
678 case 'MINUTELY': $this->freq_name =
'minute';
break;
679 case 'HOURLY': $this->freq_name =
'hour';
break;
680 case 'DAILY': $this->freq_name =
'day';
break;
681 case 'WEEKLY': $this->freq_name =
'week';
break;
682 case 'MONTHLY': $this->freq_name =
'month';
break;
683 case 'YEARLY': $this->freq_name =
'year';
break;
687 $this->frequency_string = sprintf(
'+%d %s', $this->interval, $this->freq_name );
688 if ( DEBUG_RRULE ) printf(
"Frequency modify string is: '%s', base is: '%s', TZ: %s\n", $this->frequency_string, $this->base->format(
'c'), $this->base->getTimeZone() );
689 $this->Start($return_floating_times);
698 return ( isset($this->count) || isset($this->until) );
702 public function set_timezone( $tzstring ) {
703 $this->base->setTimezone(
new DateTimeZone($tzstring));
707 public function Start($return_floating_times=
false) {
708 $this->instances = array();
709 $this->GetMoreInstances($return_floating_times);
711 $this->finished =
false;
715 public function rewind() {
716 $this->position = -1;
725 public function next($return_floating_times=
false) {
727 return $this->current($return_floating_times);
731 public function current($return_floating_times=
false) {
732 if ( !$this->valid() )
return null;
733 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
734 if ( !$this->valid() )
return null;
735 if ( DEBUG_RRULE ) printf(
"Returning date from position %d: %s (%s)\n", $this->position,
736 $this->instances[$this->position]->format(
'c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
737 return $this->instances[$this->position];
741 public function key($return_floating_times=
false) {
742 if ( !$this->valid() )
return null;
743 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
744 if ( !isset($this->keys[$this->position]) ) {
745 $this->keys[$this->position] = $this->instances[$this->position];
747 return $this->keys[$this->position];
751 public function valid() {
752 if ( DEBUG_RRULE && isset($this->instances[$this->position])) {
753 $current = $this->instances[$this->position];
754 print
"TimeZone: " . $current->getTimeZone() .
"\n";
755 print
"Date: " . $current->format(
'r') .
"\n";
757 print_r($current->getLastErrors());
759 if ( isset($this->instances[$this->position]) || !$this->finished )
return true;
774 return array(
'bymonth' =>
'expand',
'byweekno' =>
'expand',
'byyearday' =>
'expand',
'bymonthday' =>
'expand',
775 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
777 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'expand',
778 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
780 return array(
'bymonth' =>
'limit',
781 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
783 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
784 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
786 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
787 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
789 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
790 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'expand' );
792 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
793 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'limit' );
795 dbg_error_log(
'ERROR',
'Invalid frequency code "%s" - pretending it is "DAILY"', $freq);
796 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
797 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
800 private function GetMoreInstances($return_floating_times=
false) {
801 if ( $this->finished )
return;
805 if ( $return_floating_times ) $this->base->setAsFloat();
806 while( !$this->finished && !$got_more && $loops++ < $loop_limit ) {
807 if ( !isset($this->current_base) ) {
808 $this->current_base = clone($this->base);
811 $this->current_base->modify( $this->frequency_string );
813 if ( $return_floating_times ) $this->current_base->setAsFloat();
814 if ( DEBUG_RRULE ) printf(
"Getting more instances from: '%s' - %d, TZ: %s\n", $this->current_base->format(
'c'), count($this->instances), $this->current_base->getTimeZone() );
815 $this->current_set = array( clone($this->current_base) );
816 foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
817 if ( isset($this->{$bytype}) ) {
818 $this->{$action.
'_'.$bytype}();
819 if ( !isset($this->current_set[0]) )
break;
823 sort($this->current_set);
824 if ( isset($this->bysetpos) ) $this->limit_bysetpos();
826 $position = count($this->instances) - 1;
827 if ( DEBUG_RRULE ) printf(
"Inserting %d from current_set into position %d\n", count($this->current_set), $position + 1 );
828 foreach( $this->current_set AS $k => $instance ) {
829 if ( $instance < $this->base )
continue;
830 if ( isset($this->until) && $instance > $this->until ) {
831 $this->finished =
true;
834 if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
837 if ( isset($this->count) && $position >= $this->count ) {
838 $this->finished =
true;
841 $this->instances[$position] = $instance;
842 if ( DEBUG_RRULE ) printf(
"Added date %s into position %d in current set\n", $instance->format(
'c'), $position );
849 public static function rrule_day_number( $day ) {
863 static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
864 $date_parts = explode(
',',$date->format(
'Y,m,d,H,i,s'));
866 if ( isset($y) || isset($mo) || isset($d) ) {
867 if ( isset($y) ) $date_parts[0] = $y;
868 if ( isset($mo) ) $date_parts[1] = $mo;
869 if ( isset($d) ) $date_parts[2] = $d;
870 $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
872 if ( isset($h) || isset($mi) || isset($s) ) {
873 if ( isset($h) ) $date_parts[3] = $h;
874 if ( isset($mi) ) $date_parts[4] = $mi;
875 if ( isset($s) ) $date_parts[5] = $s;
876 $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
882 private function expand_bymonth() {
883 $instances = $this->current_set;
884 $this->current_set = array();
885 foreach( $instances AS $k => $instance ) {
886 foreach( $this->bymonth AS $k => $month ) {
887 $expanded = $this->date_mask( clone($instance),
null, $month,
null,
null,
null,
null);
888 if ( DEBUG_RRULE ) printf(
"Expanded BYMONTH $month into date %s\n", $expanded->format(
'c') );
889 $this->current_set[] = $expanded;
894 private function expand_bymonthday() {
895 $instances = $this->current_set;
896 $this->current_set = array();
897 foreach( $instances AS $k => $instance ) {
898 foreach( $this->bymonthday AS $k => $monthday ) {
899 $expanded = $this->date_mask( clone($instance),
null,
null, $monthday,
null,
null,
null);
900 if ($monthday == -1 || $expanded->format(
'd') == $monthday) {
901 if ( DEBUG_RRULE ) printf(
"Expanded BYMONTHDAY $monthday into date %s from %s\n", $expanded->format(
'c'), $instance->format(
'c') );
902 $this->current_set[] = $expanded;
904 if ( DEBUG_RRULE ) printf(
"Expanded BYMONTHDAY $monthday into date %s from %s, which is not the same day of month, skipping.\n", $expanded->format(
'c'), $instance->format(
'c') );
910 private function expand_byyearday() {
911 $instances = $this->current_set;
912 $this->current_set = array();
914 foreach( $instances AS $k => $instance ) {
915 foreach( $this->byyearday AS $k => $yearday ) {
916 $on_yearday = clone($instance);
917 $on_yearday->setYearDay($yearday);
918 if ( isset($days_set[$on_yearday->UTC()]) )
continue;
919 $this->current_set[] = $on_yearday;
920 $days_set[$on_yearday->UTC()] =
true;
932 $dow_of_instance = $day_in_week->format(
'w');
933 foreach( $this->byday AS $k => $weekday ) {
934 $dow = self::rrule_day_number($weekday);
935 $offset = $dow - $dow_of_instance;
936 if ( $offset < 0 ) $offset += 7;
937 $expanded = clone($day_in_week);
938 $expanded->modify( sprintf(
'+%d day', $offset) );
939 $this->current_set[] = $expanded;
940 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(W) $weekday into date %s\n", $expanded->format(
'c') );
945 private function expand_byday_in_month( $day_in_month ) {
947 $first_of_month = $this->date_mask( clone($day_in_month),
null,
null, 1,
null,
null,
null);
948 $dow_of_first = $first_of_month->format(
'w');
949 $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format(
'm'), $first_of_month->format(
'Y'));
950 foreach( $this->byday AS $k => $weekday ) {
951 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
952 $dow = self::rrule_day_number($matches[3]);
953 $first_dom = 1 + $dow - $dow_of_first;
if ( $first_dom < 1 ) $first_dom +=7;
954 $whichweek = intval($matches[2]);
955 if ( DEBUG_RRULE ) printf(
"Expanding BYDAY(M) $weekday in month of %s\n", $first_of_month->format(
'c') );
956 if ( $whichweek > 0 ) {
958 $monthday = $first_dom;
959 if ( $matches[1] ==
'-' ) {
961 while( $monthday > $days_in_month ) $monthday -= 7;
962 $monthday -= (7 * $whichweek);
965 $monthday += (7 * $whichweek);
967 if ( $monthday > 0 && $monthday <= $days_in_month ) {
968 $expanded = $this->date_mask( clone($day_in_month),
null,
null, $monthday,
null,
null,
null);
969 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format(
'c') );
970 $this->current_set[] = $expanded;
974 for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
975 $expanded = $this->date_mask( clone($day_in_month),
null,
null, $monthday,
null,
null,
null);
976 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format(
'c') );
977 $this->current_set[] = $expanded;
985 private function expand_byday_in_year( $day_in_year ) {
987 $first_of_year = $this->date_mask( clone($day_in_year),
null, 1, 1,
null,
null,
null);
988 $dow_of_first = $first_of_year->format(
'w');
989 $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format(
'Y'));
990 foreach( $this->byday AS $k => $weekday ) {
991 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
992 $expanded = clone($first_of_year);
993 $dow = self::rrule_day_number($matches[3]);
994 $first_doy = 1 + $dow - $dow_of_first;
if ( $first_doy < 1 ) $first_doy +=7;
995 $whichweek = intval($matches[2]);
996 if ( DEBUG_RRULE ) printf(
"Expanding BYDAY(Y) $weekday from date %s\n", $instance->format(
'c') );
997 if ( $whichweek > 0 ) {
999 $yearday = $first_doy;
1000 if ( $matches[1] ==
'-' ) {
1002 while( $yearday > $days_in_year ) $yearday -= 7;
1003 $yearday -= (7 * $whichweek);
1006 $yearday += (7 * $whichweek);
1008 if ( $yearday > 0 && $yearday <= $days_in_year ) {
1009 $expanded->modify(sprintf(
'+%d day', $yearday - 1));
1010 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format(
'c') );
1011 $this->current_set[] = $expanded;
1015 $expanded->modify(sprintf(
'+%d day', $first_doy - 1));
1016 for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
1017 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format(
'c') );
1018 $this->current_set[] = clone($expanded);
1019 $expanded->modify(
'+1 week');
1028 if ( !isset($this->current_set[0]) )
return;
1029 if ( $this->freq ==
'MONTHLY' || $this->freq ==
'YEARLY' ) {
1030 if ( isset($this->bymonthday) || isset($this->byyearday) ) {
1031 $this->limit_byday();
1035 $instances = $this->current_set;
1036 $this->current_set = array();
1037 foreach( $instances AS $k => $instance ) {
1038 if ( $this->freq ==
'MONTHLY' ) {
1039 $this->expand_byday_in_month($instance);
1041 else if ( $this->freq ==
'WEEKLY' ) {
1045 if ( isset($this->bymonth) ) {
1046 $this->expand_byday_in_month($instance);
1048 else if ( isset($this->byweekno) ) {
1052 $this->expand_byday_in_year($instance);
1059 private function expand_byhour() {
1060 $instances = $this->current_set;
1061 $this->current_set = array();
1062 foreach( $instances AS $k => $instance ) {
1063 foreach( $this->bymonth AS $k => $month ) {
1064 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null, $hour,
null,
null);
1069 private function expand_byminute() {
1070 $instances = $this->current_set;
1071 $this->current_set = array();
1072 foreach( $instances AS $k => $instance ) {
1073 foreach( $this->bymonth AS $k => $month ) {
1074 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null,
null, $minute,
null);
1079 private function expand_bysecond() {
1080 $instances = $this->current_set;
1081 $this->current_set = array();
1082 foreach( $instances AS $k => $instance ) {
1083 foreach( $this->bymonth AS $k => $second ) {
1084 $this->current_set[] = $this->date_mask( clone($instance),
null,
null,
null,
null,
null, $second);
1090 private function limit_generally( $fmt_char, $element_name ) {
1091 $instances = $this->current_set;
1092 $this->current_set = array();
1093 foreach( $instances AS $k => $instance ) {
1094 foreach( $this->{$element_name} AS $k => $element_value ) {
1095 if ( DEBUG_RRULE ) printf(
"Limiting '$fmt_char' on '%s' => '%s' ?=? '%s' ? %s\n", $instance->format(
'c'), $instance->format($fmt_char), $element_value, ($instance->format($fmt_char) == $element_value ?
'Yes' :
'No') );
1096 if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1101 private function limit_byday() {
1103 $instances = $this->current_set;
1104 $this->current_set = array();
1105 foreach( $this->byday AS $k => $weekday ) {
1106 $dow = self::rrule_day_number($weekday);
1107 foreach( $instances AS $k => $instance ) {
1108 if ( DEBUG_RRULE ) printf(
"Limiting '$fmt_char' on '%s' => '%s' ?=? '%s' (%d) ? %s\n", $instance->format(
'c'), $instance->format($fmt_char), $weekday, $dow, ($instance->format($fmt_char) == $dow ?
'Yes' :
'No') );
1109 if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1114 private function limit_bymonth() { $this->limit_generally(
'm',
'bymonth' ); }
1115 private function limit_byyearday() { $this->limit_generally(
'z',
'byyearday' ); }
1116 private function limit_bymonthday() { $this->limit_generally(
'd',
'bymonthday' ); }
1117 private function limit_byhour() { $this->limit_generally(
'H',
'byhour' ); }
1118 private function limit_byminute() { $this->limit_generally(
'i',
'byminute' ); }
1119 private function limit_bysecond() { $this->limit_generally(
's',
'bysecond' ); }
1122 private function limit_bysetpos( ) {
1123 $instances = $this->current_set;
1124 $count = count($instances);
1125 $this->current_set = array();
1126 foreach( $this->bysetpos AS $k => $element_value ) {
1127 if ( DEBUG_RRULE ) printf(
"Limiting bysetpos %s of %d instances\n", $element_value, $count );
1128 if ( $element_value > 0 ) {
1129 $this->current_set[] = $instances[$element_value - 1];
1131 else if ( $element_value < 0 ) {
1132 $this->current_set[] = $instances[$count + $element_value];
1142require_once(
"vComponent.php");
1153function rdate_expand( $dtstart, $property, $component, $range_end =
null, $is_date=
null, $return_floating_times=
false ) {
1154 $properties = $component->GetProperties($property);
1155 $expansion = array();
1156 foreach( $properties AS $p ) {
1157 $timezone = $p->GetParameterValue(
'TZID');
1158 $rdate = $p->Value();
1159 $rdates = explode(
',', $rdate );
1160 foreach( $rdates AS $k => $v ) {
1162 if ( $return_floating_times ) $rdate->setAsFloat();
1163 $expansion[$rdate->FloatOrUTC($return_floating_times)] = $component;
1164 if ( $rdate > $range_end )
break;
1181function rrule_expand( $dtstart, $property, $component, $range_end, $is_date=
null, $return_floating_times=
false, $fallback_tzid=
null ) {
1183 $expansion = array();
1185 $recur = $component->GetProperty($property);
1186 if ( !isset($recur) )
return $expansion;
1187 $recur = $recur->Value();
1189 $this_start = $component->GetProperty(
'DTSTART');
1190 if ( isset($this_start) ) {
1191 $this_start = RepeatRuleDateTime::withFallbackTzid($this_start, $fallback_tzid);
1194 $this_start = clone($dtstart);
1196 if ( $return_floating_times ) $this_start->setAsFloat();
1199 if ( DEBUG_RRULE ) printf(
"RRULE: %s (floating: %s)\n", $recur, ($return_floating_times?
"yes":
"no") );
1200 $rule =
new RepeatRule( $this_start, $recur, $is_date, $return_floating_times );
1203 if ( !isset($c->rrule_expansion_limit) ) $c->rrule_expansion_limit = 5000;
1204 while( $date = $rule->next($return_floating_times) ) {
1206 $expansion[$date->FloatOrUTC($return_floating_times)] = $component;
1207 if ( $date > $range_end )
break;
1208 if ( $i++ >= $c->rrule_expansion_limit ) {
1209 dbg_error_log(
'ERROR',
"Hit rrule expansion limit of ".$c->rrule_expansion_limit.
" on %s %s - increase rrule_expansion_limit in config to avoid events missing from freebusy", $component->GetType(), $component->GetProperty(
'UID'));
1228function expand_event_instances( vComponent $vResource, $range_start =
null, $range_end =
null, $return_floating_times=
false, $fallback_tzid=
null ) {
1230 $components = $vResource->GetComponents();
1232 $clear_instance_props = array(
1237 if ( empty( $c->expanded_instances_include_rrule ) ) {
1238 $clear_instance_props += array(
1245 if ( empty($range_start) ) { $range_start =
new RepeatRuleDateTime(); $range_start->modify(
'-6 weeks'); }
1246 if ( empty($range_end) ) {
1247 $range_end = clone($range_start);
1248 $range_end->modify(
'+6 months');
1251 $instances = array();
1255 $has_repeats =
false;
1256 $dtstart_type =
'DTSTART';
1258 $components_prefix = [];
1259 $components_base_events = [];
1260 $components_override_events = [];
1262 foreach ($components AS $k => $comp) {
1263 if ( $comp->GetType() !=
'VEVENT' && $comp->GetType() !=
'VTODO' && $comp->GetType() !=
'VJOURNAL' ) {
1265 $components_prefix[] = $comp;
1266 }
else if ($comp->GetProperty(
'RECURRENCE-ID') ===
null) {
1268 $components_base_events[] = $comp;
1271 $components_override_events[] = $comp;
1275 $components = array_merge($components_prefix, $components_base_events, $components_override_events);
1277 foreach( $components AS $k => $comp ) {
1278 if ( $comp->GetType() !=
'VEVENT' && $comp->GetType() !=
'VTODO' && $comp->GetType() !=
'VJOURNAL' ) {
1281 if ( !isset($dtstart) ) {
1282 $dtstart_prop = $comp->GetProperty($dtstart_type);
1283 if ( !isset($dtstart_prop) && $comp->GetType() !=
'VTODO' ) {
1284 $dtstart_type =
'DUE';
1285 $dtstart_prop = $comp->GetProperty($dtstart_type);
1287 if ( !isset($dtstart_prop) )
continue;
1289 if ( $return_floating_times ) $dtstart->setAsFloat();
1290 if ( DEBUG_RRULE ) printf(
"Component is: %s (floating: %s)\n", $comp->GetType(), ($return_floating_times?
"yes":
"no") );
1291 $is_date = $dtstart->isDate();
1292 $instances[$dtstart->FloatOrUTC($return_floating_times)] = $comp;
1293 $rrule = $comp->GetProperty(
'RRULE');
1294 $has_repeats = isset($rrule);
1296 $p = $comp->GetProperty(
'RECURRENCE-ID');
1297 if ( isset($p) && $p->Value() !=
'' ) {
1298 $range = $p->GetParameterValue(
'RANGE');
1300 if ( $is_date ) $recur_utc->setAsDate();
1301 $recur_utc = $recur_utc->FloatOrUTC($return_floating_times);
1302 if ( isset($range) && $range ==
'THISANDFUTURE' ) {
1303 foreach( $instances AS $k => $v ) {
1304 if ( DEBUG_RRULE ) printf(
"Removing overridden instance at: $k\n" );
1305 if ( $k >= $recur_utc ) unset($instances[$k]);
1309 unset($instances[$recur_utc]);
1314 else if ( DEBUG_RRULE ) {
1315 $p = $comp->GetProperty(
'SUMMARY');
1316 $summary = ( isset($p) ? $p->Value() :
'not set');
1317 $p = $comp->GetProperty(
'UID');
1318 $uid = ( isset($p) ? $p->Value() :
'not set');
1319 printf(
"Processing event '%s' with UID '%s' starting on %s\n",
1320 $summary, $uid, $dtstart->FloatOrUTC($return_floating_times) );
1321 print(
"Instances at start");
1322 foreach( $instances AS $k => $v ) {
1327 $instances += rrule_expand($dtstart,
'RRULE', $comp, $range_end,
null, $return_floating_times, $fallback_tzid);
1328 if ( DEBUG_RRULE ) {
1329 print(
"After rrule_expand");
1330 foreach( $instances AS $k => $v ) {
1335 $instances += rdate_expand($dtstart,
'RDATE', $comp, $range_end,
null, $return_floating_times);
1336 if ( DEBUG_RRULE ) {
1337 print(
"After rdate_expand");
1338 foreach( $instances AS $k => $v ) {
1343 foreach ( rdate_expand($dtstart,
'EXDATE', $comp, $range_end,
null, $return_floating_times) AS $k => $v ) {
1344 unset($instances[$k]);
1346 if ( DEBUG_RRULE ) {
1347 print(
"After exdate_expand");
1348 foreach( $instances AS $k => $v ) {
1355 $last_duration =
null;
1356 $early_start =
null;
1357 $new_components = array();
1358 $start_utc = $range_start->FloatOrUTC($return_floating_times);
1359 $end_utc = $range_end->FloatOrUTC($return_floating_times);
1360 foreach( $instances AS $utc => $comp ) {
1361 if ( $utc > $end_utc ) {
1362 if ( DEBUG_RRULE ) printf(
"We're done: $utc is out of the range.\n");
1366 $end_type = ($comp->GetType() ==
'VTODO' ?
'DUE' :
'DTEND');
1367 $duration = $comp->GetProperty(
'DURATION');
1368 if ( !isset($duration) || $duration->Value() ==
'' ) {
1369 $instance_start = $comp->GetProperty($dtstart_type);
1371 if ( $return_floating_times ) $dtsrt->setAsFloat();
1372 $instance_end = $comp->GetProperty($end_type);
1373 if ( isset($instance_end) ) {
1378 if ( $instance_start->GetParameterValue(
'VALUE') ==
'DATE' ) {
1390 if ( $utc < $start_utc ) {
1391 if ( isset($early_start) && isset($last_duration) && $duration->equals($last_duration) ) {
1392 if ( $utc < $early_start ) {
1393 if ( DEBUG_RRULE ) printf(
"Next please: $utc is before $early_start and before $start_utc.\n");
1399 $latest_start = clone($range_start);
1400 $latest_start->modify(
'-'.$duration);
1401 $early_start = $latest_start->FloatOrUTC($return_floating_times);
1402 $last_duration = $duration;
1403 if ( $utc < $early_start ) {
1404 if ( DEBUG_RRULE ) printf(
"Another please: $utc is before $early_start and before $start_utc.\n");
1409 $component = clone($comp);
1410 $component->ClearProperties( $clear_instance_props );
1411 $component->AddProperty($dtstart_type, $utc, ($is_date ? array(
'VALUE' =>
'DATE') :
null) );
1412 $component->AddProperty(
'DURATION', $duration );
1413 if ( $has_repeats && $dtstart->FloatOrUTC($return_floating_times) != $utc )
1414 $component->AddProperty(
'RECURRENCE-ID', $utc, ($is_date ? array(
'VALUE' =>
'DATE') :
null) );
1415 $new_components[$utc] = $component;
1419 foreach( $components AS $k => $comp ) {
1420 $p = $comp->GetProperty(
'RECURRENCE-ID');
1421 if ( isset($p) && $p->Value() !=
'') {
1422 $recurrence_id = $p->Value();
1425 $dtstart_prop = $comp->GetProperty(
'DTSTART');
1426 if ( !isset($dtstart_prop) && $comp->GetType() !=
'VTODO' ) {
1427 $dtstart_prop = $comp->GetProperty(
'DUE');
1430 if ( !isset($new_components[$recurrence_id]) && !isset($dtstart_prop) )
continue;
1432 $is_date = $dtstart_rrdt->isDate();
1433 if ( $return_floating_times ) $dtstart_rrdt->setAsFloat();
1434 $dtstart = $dtstart_rrdt->FloatOrUTC($return_floating_times);
1435 if ( !isset($new_components[$recurrence_id]) && $dtstart > $end_utc )
continue;
1437 $end_type = ($comp->GetType() ==
'VTODO' ?
'DUE' :
'DTEND');
1438 $duration = $comp->GetProperty(
'DURATION');
1440 if ( !isset($duration) || $duration->Value() ==
'' ) {
1441 $instance_end = $comp->GetProperty($end_type);
1442 if ( isset($instance_end) ) {
1444 if ( $return_floating_times ) $dtend_rrdt->setAsFloat();
1445 $dtend = $dtend_rrdt->FloatOrUTC($return_floating_times);
1450 $dtend = $dtstart + ($is_date ? $dtstart + 86400 : 0 );
1455 $dtend = $dtstart + $duration->asSeconds();
1458 if ( !isset($new_components[$recurrence_id]) && $dtend < $start_utc )
continue;
1460 if ( DEBUG_RRULE ) printf(
"Replacing overridden instance at %s\n", $recurrence_id);
1461 $new_components[$recurrence_id] = $comp;
1465 $vResource->SetComponents($new_components);
1478function getComponentRange(vComponent $comp, $fallback_tzid =
null) {
1479 $dtstart_prop = $comp->GetProperty(
'DTSTART');
1480 $duration_prop = $comp->GetProperty(
'DURATION');
1481 if ( isset($duration_prop) ) {
1482 if ( !isset($dtstart_prop) )
throw new Exception(
'Invalid '.$comp->GetType().
' containing DURATION without DTSTART', 0);
1483 $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1484 $dtend = clone($dtstart);
1488 $completed_prop =
null;
1489 switch ( $comp->GetType() ) {
1491 if ( !isset($dtstart_prop) )
throw new Exception(
'Invalid VEVENT without DTSTART', 0);
1492 $dtend_prop = $comp->GetProperty(
'DTEND');
1495 $completed_prop = $comp->GetProperty(
'COMPLETED');
1496 $dtend_prop = $comp->GetProperty(
'DUE');
1499 if ( !isset($dtstart_prop) )
1500 $dtstart_prop = $comp->GetProperty(
'DTSTAMP');
1501 $dtend_prop = $dtstart_prop;
1504 throw new Exception(
'getComponentRange cannot handle "'.$comp->GetType().
'" components', 0);
1507 if ( isset($dtstart_prop) )
1508 $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1512 if ( isset($dtend_prop) )
1513 $dtend = RepeatRuleDateTime::withFallbackTzid($dtend_prop, $fallback_tzid);
1517 if ( isset($completed_prop) ) {
1518 $completed = RepeatRuleDateTime::withFallbackTzid($completed_prop, $fallback_tzid);
1519 if ( !isset($dtstart) || (isset($dtstart) && $completed < $dtstart) ) $dtstart = $completed;
1520 if ( !isset($dtend) || (isset($dtend) && $completed > $dtend) ) $dtend = $completed;
1535function getVCalendarRange( $vResource, $fallback_tzid =
null ) {
1536 $components = $vResource->GetComponents();
1540 $earliest_start =
null;
1542 $has_repeats =
false;
1543 foreach( $components AS $k => $comp ) {
1544 if ( $comp->GetType() ==
'VTIMEZONE' )
continue;
1545 $range = getComponentRange($comp, $fallback_tzid);
1546 $dtstart = $range->from;
1547 if ( !isset($dtstart) )
continue;
1548 $duration = $range->getDuration();
1550 $rrule = $comp->GetProperty(
'RRULE');
1551 $limited_occurrences =
true;
1552 if ( isset($rrule) ) {
1554 $limited_occurrences = $rule->hasLimitedOccurrences();
1557 if ( $limited_occurrences ) {
1558 $instances = array();
1559 $instances[$dtstart->FloatOrUTC()] = $dtstart;
1560 if ( !isset($range_end) ) {
1562 $range_end->modify(
'+150 years');
1564 $instances += rrule_expand($dtstart,
'RRULE', $comp, $range_end,
null,
false, $fallback_tzid);
1565 $instances += rdate_expand($dtstart,
'RDATE', $comp, $range_end);
1566 foreach ( rdate_expand($dtstart,
'EXDATE', $comp, $range_end) AS $k => $v ) {
1567 unset($instances[$k]);
1569 if ( count($instances) < 1 ) {
1570 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1574 $instances = array_keys($instances);
1578 $last->modify($duration);
1579 if ( empty($earliest_start) || $first < $earliest_start ) $earliest_start = $first;
1580 if ( empty($latest_end) || $last > $latest_end ) $latest_end = $last;
1583 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
__construct( $date1, $date2)
overlaps(RepeatRuleDateRange $other)
RFC5545($return_floating_times=false)
static daysInMonth( $year, $month)
FloatOrUTC($return_floating_times=false)
static rrule_expand_limit( $freq)
next($return_floating_times=false)
expand_byday_in_week( $day_in_week)
__construct( $basedate, $rrule, $is_date=null, $return_floating_times=false)
__construct( $in_duration)
static fromTwoDates( $d1, $d2)