12 if ( !class_exists(
'DateTime') )
return;
19 function 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;
34 define(
'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 >= 86400 ) {
156 $this->days = floor($in_duration / 86400);
157 $in_duration -= $this->days * 86400;
158 if ( $in_duration == 0 && ($this->days / 7) == floor($this->days / 7) ) {
159 $this->as_text .= ($this->days/7).
'W';
160 return $this->as_text;
162 $this->as_text .= $this->days.
'D';
164 if ( $in_duration > 0 ) {
165 $secs = $in_duration;
166 $this->as_text .=
'T';
167 $hours = floor($in_duration / 3600);
168 if ( $hours > 0 ) $this->as_text .= $hours .
'H';
169 $minutes = floor(($in_duration % 3600) / 60);
170 if ( $minutes > 0 ) $this->as_text .= $minutes .
'M';
171 $seconds = $in_duration % 60;
172 if ( $seconds > 0 ) $this->as_text .= $seconds .
'S';
175 return $this->as_text;
199 $diff = $d2->epoch() - $d1->epoch();
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 ) printf(
"Date%s property%s: %s%s\n", ($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 ) printf(
"Date%s property%s: %s%s\n", ($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 ) printf(
"Floating Date value: %s\n", $date );
281 elseif ( $dtz === null || $dtz ==
'' ) {
282 $dtz = self::$UTCzone;
283 if ( preg_match(
'/(\d{8}(T\d{6})?)(Z?)/', $date, $matches) ) {
285 $this->tzid = ( $matches[3] ==
'Z' ?
'UTC' : null );
287 $this->is_date = (strlen($date) == 8 );
288 if ( DEBUG_RRULE ) printf(
"Date%s value with timezone: %s in %s\n", ($this->is_date?
"":
"Time"), $date, $this->tzid );
290 elseif ( is_string($dtz) ) {
292 $this->tzid = $dtz->tzid();
293 $type = gettype($date);
294 if ( DEBUG_RRULE ) printf(
"Date%s $type with timezone: %s in %s\n", ($this->is_date?
"":
"Time"), $date, $this->tzid );
297 $this->tzid = $dtz->getName();
298 $type = gettype($date);
299 if ( DEBUG_RRULE ) printf(
"Date%s $type with timezone: %s in %s\n", ($this->is_date?
"":
"Time"), $date, $this->tzid );
302 parent::__construct($date, $dtz);
303 if ( isset($is_date) ) $this->is_date = $is_date;
308 public static function withFallbackTzid( $date, $fallback_tzid ) {
311 if ($date->GetParameterValue(
'VALUE') ==
'DATE' && isset($fallback_tzid)) {
313 }
else if ($date->GetParameterValue(
'TZID') === null && isset($fallback_tzid)) {
321 public function __toString() {
322 return (
string)parent::format(self::$Format) .
' ' . parent::getTimeZone()->getName();
326 public function AsDate() {
327 return $this->format(
'Ymd');
331 public function setAsFloat() {
336 public function isFloating() {
337 return !isset($this->tzid);
340 public function isDate() {
341 return $this->is_date;
345 public function setAsDate() {
346 $this->is_date =
true;
350 public function modify( $interval ) {
352 if ( preg_match(
'{^(-)?P(([0-9-]+)W)?(([0-9-]+)D)?T?(([0-9-]+)H)?(([0-9-]+)M)?(([0-9-]+)S)?$}', $interval, $matches) ) {
353 $minus = (isset($matches[1])?$matches[1]:
'');
355 if ( isset($matches[2]) && $matches[2] !=
'' ) $interval .= $minus . $matches[3] .
' weeks ';
356 if ( isset($matches[4]) && $matches[4] !=
'' ) $interval .= $minus . $matches[5] .
' days ';
357 if ( isset($matches[6]) && $matches[6] !=
'' ) $interval .= $minus . $matches[7] .
' hours ';
358 if ( isset($matches[8]) && $matches[8] !=
'' ) $interval .= $minus . $matches[9] .
' minutes ';
359 if (isset($matches[10]) &&$matches[10] !=
'' ) $interval .= $minus . $matches[11] .
' seconds ';
363 if ( !isset($interval) || $interval ==
'' ) $interval =
'1 day';
364 if ( parent::format(
'd') > 28 && strstr($interval,
'month') !== false ) {
365 $this->setDate(null,null,28);
367 parent::modify($interval);
368 return $this->__toString();
379 public function UTC($fmt =
'Ymd\THis\Z' ) {
381 if ( $this->tzid !=
'UTC' ) {
382 if ( isset($this->tzid)) {
383 $dtz = parent::getTimezone();
386 $dtz =
new DateTimeZone(date_default_timezone_get());
388 $offset = 0 - $dtz->getOffset($gmt);
389 $gmt->modify( $offset .
' seconds' );
391 return $gmt->format($fmt);
408 if ( !$return_floating_times && isset($this->tzid) && $this->tzid !=
'UTC' ) {
409 $dtz = parent::getTimezone();
410 $offset = 0 - $dtz->getOffset($gmt);
411 $gmt->modify( $offset .
' seconds' );
413 if ( $this->is_date )
return $gmt->format(
'Ymd');
414 if ( $return_floating_times )
return $gmt->format(
'Ymd\THis');
415 return $gmt->format(
'Ymd\THis') . (!$return_floating_times && isset($this->tzid) ?
'Z' :
'');
422 public function RFC5545($return_floating_times =
false) {
424 if ( isset($this->tzid) && $this->tzid !=
'UTC' ) {
425 $result =
';TZID='.$this->tzid;
427 if ( $this->is_date ) {
428 $result .=
';VALUE=DATE:' . $this->format(
'Ymd');
431 $result .=
':' . $this->format(
'Ymd\THis');
432 if ( !$return_floating_times && isset($this->tzid) && $this->tzid ==
'UTC' ) {
440 public function setTimeZone( $tz ) {
441 if ( is_string($tz) ) {
443 $this->tzid = $tz->tzid();
445 parent::setTimeZone( $tz );
450 public function getTimeZone() {
461 if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) )
return 1;
472 if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
return 30;
473 else if ($month != 2)
return 31;
478 function setDate( $year=null, $month=null, $day=null ) {
479 if ( !isset($year) ) $year = parent::format(
'Y');
480 if ( !isset($month) ) $month = parent::format(
'm');
481 if ( !isset($day) ) $day = parent::format(
'd');
485 parent::setDate( $year , $month , $day );
489 function setYearDay( $yearday ) {
490 if ( $yearday > 0 ) {
491 $current_yearday = parent::format(
'z') + 1;
494 $current_yearday = (parent::format(
'z') - (365 + parent::format(
'L')));
496 $diff = $yearday - $current_yearday;
497 if ( $diff < 0 ) $this->modify(
'-P'.-$diff.
'D');
498 else if ( $diff > 0 ) $this->modify(
'P'.$diff.
'D');
505 return parent::format(
'Y');
509 return parent::format(
'm');
513 return parent::format(
'd');
517 return parent::format(
'H');
521 return parent::format(
'i');
525 return parent::format(
's');
529 return parent::format(
'U');
554 if ( $date1 != null && $date2 != null && $date1 > $date2 ) {
555 $this->from = $date2;
556 $this->until = $date1;
559 $this->from = $date1;
560 $this->until = $date2;
570 if ( ($this->until == null && $this->from == null) || ($other->until == null && $other->from == null ) )
return true;
571 if ( $this->until == null && $other->until == null )
return true;
572 if ( $this->from == null && $other->from == null )
return true;
574 if ( $this->until == null )
return ($other->until > $this->from);
575 if ( $this->from == null )
return ($other->from < $this->until);
576 if ( $other->until == null )
return ($this->until > $other->from);
577 if ( $other->from == null )
return ($this->from < $other->until);
579 return !( $this->until < $other->from || $this->from > $other->until );
589 if ( !isset($this->from) )
return null;
590 if ( $this->from->isDate() && !isset($this->until) )
592 else if ( !isset($this->until) )
595 $duration = ( $this->until->epoch() - $this->from->epoch() );
629 private $current_base;
630 private $original_rule;
633 public function __construct( $basedate, $rrule, $is_date=null, $return_floating_times=
false ) {
634 if ( $return_floating_times ) $basedate->setAsFloat();
635 $this->base = (is_object($basedate) ? $basedate :
new RepeatRuleDateTime($basedate) );
636 $this->original_rule = $rrule;
639 printf(
"Constructing RRULE based on: '%s', rrule: '%s' (we float: %s)\n", $basedate, $rrule, ($return_floating_times?
"yes":
"no") );
642 if ( preg_match(
'{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
644 if ( preg_match(
'{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
646 if ( preg_match(
'{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
647 if ( preg_match(
'{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
649 if ( preg_match(
'{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
651 if ( preg_match(
'{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
652 $this->byday = explode(
',',$m[1]);
654 if ( preg_match(
'{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(
',',$m[1]);
655 if ( preg_match(
'{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(
',',$m[1]);
656 if ( preg_match(
'{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(
',',$m[1]);
657 if ( preg_match(
'{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(
',',$m[1]);
658 if ( preg_match(
'{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(
',',$m[1]);
660 if ( preg_match(
'{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(
',',$m[1]);
661 if ( preg_match(
'{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(
',',$m[1]);
662 if ( preg_match(
'{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(
',',$m[1]);
664 if ( !isset($this->interval) ) $this->interval = 1;
665 switch( $this->freq ) {
666 case 'SECONDLY': $this->freq_name =
'second';
break;
667 case 'MINUTELY': $this->freq_name =
'minute';
break;
668 case 'HOURLY': $this->freq_name =
'hour';
break;
669 case 'DAILY': $this->freq_name =
'day';
break;
670 case 'WEEKLY': $this->freq_name =
'week';
break;
671 case 'MONTHLY': $this->freq_name =
'month';
break;
672 case 'YEARLY': $this->freq_name =
'year';
break;
676 $this->frequency_string = sprintf(
'+%d %s', $this->interval, $this->freq_name );
677 if ( DEBUG_RRULE ) printf(
"Frequency modify string is: '%s', base is: '%s'\n", $this->frequency_string, $this->base->format(
'c') );
678 $this->Start($return_floating_times);
687 return ( isset($this->count) || isset($this->until) );
691 public function set_timezone( $tzstring ) {
692 $this->base->setTimezone(
new DateTimeZone($tzstring));
696 public function Start($return_floating_times=
false) {
697 $this->instances = array();
698 $this->GetMoreInstances($return_floating_times);
700 $this->finished =
false;
704 public function rewind() {
705 $this->position = -1;
714 public function next($return_floating_times=
false) {
716 return $this->current($return_floating_times);
720 public function current($return_floating_times=
false) {
721 if ( !$this->valid() )
return null;
722 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
723 if ( !$this->valid() )
return null;
724 if ( DEBUG_RRULE ) printf(
"Returning date from position %d: %s (%s)\n", $this->position,
725 $this->instances[$this->position]->format(
'c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
726 return $this->instances[$this->position];
730 public function key($return_floating_times=
false) {
731 if ( !$this->valid() )
return null;
732 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
733 if ( !isset($this->keys[$this->position]) ) {
734 $this->keys[$this->position] = $this->instances[$this->position];
736 return $this->keys[$this->position];
740 public function valid() {
741 if ( isset($this->instances[$this->position]) || !$this->finished )
return true;
756 return array(
'bymonth' =>
'expand',
'byweekno' =>
'expand',
'byyearday' =>
'expand',
'bymonthday' =>
'expand',
757 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
759 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'expand',
760 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
762 return array(
'bymonth' =>
'limit',
763 'byday' =>
'expand',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
765 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
766 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
768 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
769 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
771 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
772 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'expand' );
774 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
775 'byday' =>
'limit',
'byhour' =>
'limit',
'byminute' =>
'limit',
'bysecond' =>
'limit' );
777 dbg_error_log(
'ERROR',
'Invalid frequency code "%s" - pretending it is "DAILY"', $freq);
778 return array(
'bymonth' =>
'limit',
'bymonthday' =>
'limit',
779 'byday' =>
'limit',
'byhour' =>
'expand',
'byminute' =>
'expand',
'bysecond' =>
'expand' );
782 private function GetMoreInstances($return_floating_times=
false) {
783 if ( $this->finished )
return;
787 if ( $return_floating_times ) $this->base->setAsFloat();
788 while( !$this->finished && !$got_more && $loops++ < $loop_limit ) {
789 if ( !isset($this->current_base) ) {
790 $this->current_base = clone($this->base);
793 $this->current_base->modify( $this->frequency_string );
795 if ( $return_floating_times ) $this->current_base->setAsFloat();
796 if ( DEBUG_RRULE ) printf(
"Getting more instances from: '%s' - %d\n", $this->current_base->format(
'c'), count($this->instances) );
797 $this->current_set = array( clone($this->current_base) );
798 foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
799 if ( isset($this->{$bytype}) ) {
800 $this->{$action.
'_'.$bytype}();
801 if ( !isset($this->current_set[0]) )
break;
805 sort($this->current_set);
806 if ( isset($this->bysetpos) ) $this->limit_bysetpos();
808 $position = count($this->instances) - 1;
809 if ( DEBUG_RRULE ) printf(
"Inserting %d from current_set into position %d\n", count($this->current_set), $position + 1 );
810 foreach( $this->current_set AS $k => $instance ) {
811 if ( $instance < $this->base )
continue;
812 if ( isset($this->until) && $instance > $this->until ) {
813 $this->finished =
true;
816 if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
819 $this->instances[$position] = $instance;
820 if ( DEBUG_RRULE ) printf(
"Added date %s into position %d in current set\n", $instance->format(
'c'), $position );
821 if ( isset($this->count) && ($position + 1) >= $this->count ) {
822 $this->finished =
true;
831 public static function rrule_day_number( $day ) {
845 static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
846 $date_parts = explode(
',',$date->format(
'Y,m,d,H,i,s'));
848 if ( isset($y) || isset($mo) || isset($d) ) {
849 if ( isset($y) ) $date_parts[0] = $y;
850 if ( isset($mo) ) $date_parts[1] = $mo;
851 if ( isset($d) ) $date_parts[2] = $d;
852 $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
854 if ( isset($h) || isset($mi) || isset($s) ) {
855 if ( isset($h) ) $date_parts[3] = $h;
856 if ( isset($mi) ) $date_parts[4] = $mi;
857 if ( isset($s) ) $date_parts[5] = $s;
858 $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
864 private function expand_bymonth() {
865 $instances = $this->current_set;
866 $this->current_set = array();
867 foreach( $instances AS $k => $instance ) {
868 foreach( $this->bymonth AS $k => $month ) {
869 $expanded = $this->date_mask( clone($instance), null, $month, null, null, null, null);
870 if ( DEBUG_RRULE ) printf(
"Expanded BYMONTH $month into date %s\n", $expanded->format(
'c') );
871 $this->current_set[] = $expanded;
876 private function expand_bymonthday() {
877 $instances = $this->current_set;
878 $this->current_set = array();
879 foreach( $instances AS $k => $instance ) {
880 foreach( $this->bymonthday AS $k => $monthday ) {
881 $expanded = $this->date_mask( clone($instance), null, null, $monthday, null, null, null);
882 if ( DEBUG_RRULE ) printf(
"Expanded BYMONTHDAY $monthday into date %s from %s\n", $expanded->format(
'c'), $instance->format(
'c') );
883 $this->current_set[] = $expanded;
888 private function expand_byyearday() {
889 $instances = $this->current_set;
890 $this->current_set = array();
892 foreach( $instances AS $k => $instance ) {
893 foreach( $this->byyearday AS $k => $yearday ) {
894 $on_yearday = clone($instance);
895 $on_yearday->setYearDay($yearday);
896 if ( isset($days_set[$on_yearday->UTC()]) )
continue;
897 $this->current_set[] = $on_yearday;
898 $days_set[$on_yearday->UTC()] =
true;
910 $dow_of_instance = $day_in_week->format(
'w');
911 foreach( $this->byday AS $k => $weekday ) {
912 $dow = self::rrule_day_number($weekday);
913 $offset = $dow - $dow_of_instance;
914 if ( $offset < 0 ) $offset += 7;
915 $expanded = clone($day_in_week);
916 $expanded->modify( sprintf(
'+%d day', $offset) );
917 $this->current_set[] = $expanded;
918 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(W) $weekday into date %s\n", $expanded->format(
'c') );
923 private function expand_byday_in_month( $day_in_month ) {
925 $first_of_month = $this->date_mask( clone($day_in_month), null, null, 1, null, null, null);
926 $dow_of_first = $first_of_month->format(
'w');
927 $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format(
'm'), $first_of_month->format(
'Y'));
928 foreach( $this->byday AS $k => $weekday ) {
929 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
930 $dow = self::rrule_day_number($matches[3]);
931 $first_dom = 1 + $dow - $dow_of_first;
if ( $first_dom < 1 ) $first_dom +=7;
932 $whichweek = intval($matches[2]);
933 if ( DEBUG_RRULE ) printf(
"Expanding BYDAY(M) $weekday in month of %s\n", $first_of_month->format(
'c') );
934 if ( $whichweek > 0 ) {
936 $monthday = $first_dom;
937 if ( $matches[1] ==
'-' ) {
939 while( $monthday > $days_in_month ) $monthday -= 7;
940 $monthday -= (7 * $whichweek);
943 $monthday += (7 * $whichweek);
945 if ( $monthday > 0 && $monthday <= $days_in_month ) {
946 $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
947 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format(
'c') );
948 $this->current_set[] = $expanded;
952 for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
953 $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
954 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format(
'c') );
955 $this->current_set[] = $expanded;
963 private function expand_byday_in_year( $day_in_year ) {
965 $first_of_year = $this->date_mask( clone($day_in_year), null, 1, 1, null, null, null);
966 $dow_of_first = $first_of_year->format(
'w');
967 $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format(
'Y'));
968 foreach( $this->byday AS $k => $weekday ) {
969 if ( preg_match(
'{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
970 $expanded = clone($first_of_year);
971 $dow = self::rrule_day_number($matches[3]);
972 $first_doy = 1 + $dow - $dow_of_first;
if ( $first_doy < 1 ) $first_doy +=7;
973 $whichweek = intval($matches[2]);
974 if ( DEBUG_RRULE ) printf(
"Expanding BYDAY(Y) $weekday from date %s\n", $instance->format(
'c') );
975 if ( $whichweek > 0 ) {
977 $yearday = $first_doy;
978 if ( $matches[1] ==
'-' ) {
980 while( $yearday > $days_in_year ) $yearday -= 7;
981 $yearday -= (7 * $whichweek);
984 $yearday += (7 * $whichweek);
986 if ( $yearday > 0 && $yearday <= $days_in_year ) {
987 $expanded->modify(sprintf(
'+%d day', $yearday - 1));
988 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format(
'c') );
989 $this->current_set[] = $expanded;
993 $expanded->modify(sprintf(
'+%d day', $first_doy - 1));
994 for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
995 if ( DEBUG_RRULE ) printf(
"Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format(
'c') );
996 $this->current_set[] = clone($expanded);
997 $expanded->modify(
'+1 week');
1006 if ( !isset($this->current_set[0]) )
return;
1007 if ( $this->freq ==
'MONTHLY' || $this->freq ==
'YEARLY' ) {
1008 if ( isset($this->bymonthday) || isset($this->byyearday) ) {
1009 $this->limit_byday();
1013 $instances = $this->current_set;
1014 $this->current_set = array();
1015 foreach( $instances AS $k => $instance ) {
1016 if ( $this->freq ==
'MONTHLY' ) {
1017 $this->expand_byday_in_month($instance);
1019 else if ( $this->freq ==
'WEEKLY' ) {
1020 $this->expand_byday_in_week($instance);
1023 if ( isset($this->bymonth) ) {
1024 $this->expand_byday_in_month($instance);
1026 else if ( isset($this->byweekno) ) {
1027 $this->expand_byday_in_week($instance);
1030 $this->expand_byday_in_year($instance);
1037 private function expand_byhour() {
1038 $instances = $this->current_set;
1039 $this->current_set = array();
1040 foreach( $instances AS $k => $instance ) {
1041 foreach( $this->bymonth AS $k => $month ) {
1042 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, $hour, null, null);
1047 private function expand_byminute() {
1048 $instances = $this->current_set;
1049 $this->current_set = array();
1050 foreach( $instances AS $k => $instance ) {
1051 foreach( $this->bymonth AS $k => $month ) {
1052 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, $minute, null);
1057 private function expand_bysecond() {
1058 $instances = $this->current_set;
1059 $this->current_set = array();
1060 foreach( $instances AS $k => $instance ) {
1061 foreach( $this->bymonth AS $k => $second ) {
1062 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, null, $second);
1068 private function limit_generally( $fmt_char, $element_name ) {
1069 $instances = $this->current_set;
1070 $this->current_set = array();
1071 foreach( $instances AS $k => $instance ) {
1072 foreach( $this->{$element_name} AS $k => $element_value ) {
1073 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') );
1074 if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1079 private function limit_byday() {
1081 $instances = $this->current_set;
1082 $this->current_set = array();
1083 foreach( $this->byday AS $k => $weekday ) {
1084 $dow = self::rrule_day_number($weekday);
1085 foreach( $instances AS $k => $instance ) {
1086 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') );
1087 if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1092 private function limit_bymonth() { $this->limit_generally(
'm',
'bymonth' ); }
1093 private function limit_byyearday() { $this->limit_generally(
'z',
'byyearday' ); }
1094 private function limit_bymonthday() { $this->limit_generally(
'd',
'bymonthday' ); }
1095 private function limit_byhour() { $this->limit_generally(
'H',
'byhour' ); }
1096 private function limit_byminute() { $this->limit_generally(
'i',
'byminute' ); }
1097 private function limit_bysecond() { $this->limit_generally(
's',
'bysecond' ); }
1100 private function limit_bysetpos( ) {
1101 $instances = $this->current_set;
1102 $count = count($instances);
1103 $this->current_set = array();
1104 foreach( $this->bysetpos AS $k => $element_value ) {
1105 if ( DEBUG_RRULE ) printf(
"Limiting bysetpos %s of %d instances\n", $element_value, $count );
1106 if ( $element_value > 0 ) {
1107 $this->current_set[] = $instances[$element_value - 1];
1109 else if ( $element_value < 0 ) {
1110 $this->current_set[] = $instances[$count + $element_value];
1120 require_once(
"vComponent.php");
1131 function rdate_expand( $dtstart, $property, $component, $range_end = null, $is_date=null, $return_floating_times=
false ) {
1132 $properties = $component->GetProperties($property);
1133 $expansion = array();
1134 foreach( $properties AS $p ) {
1135 $timezone = $p->GetParameterValue(
'TZID');
1136 $rdate = $p->Value();
1137 $rdates = explode(
',', $rdate );
1138 foreach( $rdates AS $k => $v ) {
1140 if ( $return_floating_times ) $rdate->setAsFloat();
1141 $expansion[$rdate->FloatOrUTC($return_floating_times)] = $component;
1142 if ( $rdate > $range_end )
break;
1159 function rrule_expand( $dtstart, $property, $component, $range_end, $is_date=null, $return_floating_times=
false, $fallback_tzid=null ) {
1161 $expansion = array();
1163 $recur = $component->GetProperty($property);
1164 if ( !isset($recur) )
return $expansion;
1165 $recur = $recur->Value();
1167 $this_start = $component->GetProperty(
'DTSTART');
1168 if ( isset($this_start) ) {
1169 $this_start = RepeatRuleDateTime::withFallbackTzid($this_start, $fallback_tzid);
1172 $this_start = clone($dtstart);
1174 if ( $return_floating_times ) $this_start->setAsFloat();
1177 if ( DEBUG_RRULE ) printf(
"RRULE: %s (floating: %s)\n", $recur, ($return_floating_times?
"yes":
"no") );
1178 $rule =
new RepeatRule( $this_start, $recur, $is_date, $return_floating_times );
1181 if ( !isset($c->rrule_expansion_limit) ) $c->rrule_expansion_limit = 5000;
1182 while( $date = $rule->next($return_floating_times) ) {
1184 $expansion[$date->FloatOrUTC($return_floating_times)] = $component;
1185 if ( $date > $range_end )
break;
1186 if ( $i++ >= $c->rrule_expansion_limit ) {
1187 dbg_error_log(
'ERROR',
"Hit rrule expansion limit of ".$c->rrule_expansion_limit.
" - increase rrule_expansion_limit in config to avoid events missing from freebusy" );
1206 function expand_event_instances( vComponent $vResource, $range_start = null, $range_end = null, $return_floating_times=
false, $fallback_tzid=null ) {
1208 $components = $vResource->GetComponents();
1210 $clear_instance_props = array(
1215 if ( empty( $c->expanded_instances_include_rrule ) ) {
1216 $clear_instance_props += array(
1223 if ( empty($range_start) ) { $range_start =
new RepeatRuleDateTime(); $range_start->modify(
'-6 weeks'); }
1224 if ( empty($range_end) ) {
1225 $range_end = clone($range_start);
1226 $range_end->modify(
'+6 months');
1229 $instances = array();
1233 $has_repeats =
false;
1234 $dtstart_type =
'DTSTART';
1236 $components_prefix = [];
1237 $components_base_events = [];
1238 $components_override_events = [];
1240 foreach ($components AS $k => $comp) {
1241 if ( $comp->GetType() !=
'VEVENT' && $comp->GetType() !=
'VTODO' && $comp->GetType() !=
'VJOURNAL' ) {
1243 $components_prefix[] = $comp;
1244 }
else if ($comp->GetProperty(
'RECURRENCE-ID') === null) {
1246 $components_base_events[] = $comp;
1249 $components_override_events[] = $comp;
1253 $components = array_merge($components_prefix, $components_base_events, $components_override_events);
1255 foreach( $components AS $k => $comp ) {
1256 if ( $comp->GetType() !=
'VEVENT' && $comp->GetType() !=
'VTODO' && $comp->GetType() !=
'VJOURNAL' ) {
1259 if ( !isset($dtstart) ) {
1260 $dtstart_prop = $comp->GetProperty($dtstart_type);
1261 if ( !isset($dtstart_prop) && $comp->GetType() !=
'VTODO' ) {
1262 $dtstart_type =
'DUE';
1263 $dtstart_prop = $comp->GetProperty($dtstart_type);
1265 if ( !isset($dtstart_prop) )
continue;
1267 if ( $return_floating_times ) $dtstart->setAsFloat();
1268 if ( DEBUG_RRULE ) printf(
"Component is: %s (floating: %s)\n", $comp->GetType(), ($return_floating_times?
"yes":
"no") );
1269 $is_date = $dtstart->isDate();
1270 $instances[$dtstart->FloatOrUTC($return_floating_times)] = $comp;
1271 $rrule = $comp->GetProperty(
'RRULE');
1272 $has_repeats = isset($rrule);
1274 $p = $comp->GetProperty(
'RECURRENCE-ID');
1275 if ( isset($p) && $p->Value() !=
'' ) {
1276 $range = $p->GetParameterValue(
'RANGE');
1278 if ( $is_date ) $recur_utc->setAsDate();
1279 $recur_utc = $recur_utc->FloatOrUTC($return_floating_times);
1280 if ( isset($range) && $range ==
'THISANDFUTURE' ) {
1281 foreach( $instances AS $k => $v ) {
1282 if ( DEBUG_RRULE ) printf(
"Removing overridden instance at: $k\n" );
1283 if ( $k >= $recur_utc ) unset($instances[$k]);
1287 unset($instances[$recur_utc]);
1292 else if ( DEBUG_RRULE ) {
1293 $p = $comp->GetProperty(
'SUMMARY');
1294 $summary = ( isset($p) ? $p->Value() :
'not set');
1295 $p = $comp->GetProperty(
'UID');
1296 $uid = ( isset($p) ? $p->Value() :
'not set');
1297 printf(
"Processing event '%s' with UID '%s' starting on %s\n",
1298 $summary, $uid, $dtstart->FloatOrUTC($return_floating_times) );
1299 print(
"Instances at start");
1300 foreach( $instances AS $k => $v ) {
1305 $instances += rrule_expand($dtstart,
'RRULE', $comp, $range_end, null, $return_floating_times, $fallback_tzid);
1306 if ( DEBUG_RRULE ) {
1307 print(
"After rrule_expand");
1308 foreach( $instances AS $k => $v ) {
1313 $instances += rdate_expand($dtstart,
'RDATE', $comp, $range_end, null, $return_floating_times);
1314 if ( DEBUG_RRULE ) {
1315 print(
"After rdate_expand");
1316 foreach( $instances AS $k => $v ) {
1321 foreach ( rdate_expand($dtstart,
'EXDATE', $comp, $range_end, null, $return_floating_times) AS $k => $v ) {
1322 unset($instances[$k]);
1324 if ( DEBUG_RRULE ) {
1325 print(
"After exdate_expand");
1326 foreach( $instances AS $k => $v ) {
1333 $last_duration = null;
1334 $early_start = null;
1335 $new_components = array();
1336 $start_utc = $range_start->FloatOrUTC($return_floating_times);
1337 $end_utc = $range_end->FloatOrUTC($return_floating_times);
1338 foreach( $instances AS $utc => $comp ) {
1339 if ( $utc > $end_utc ) {
1340 if ( DEBUG_RRULE ) printf(
"We're done: $utc is out of the range.\n");
1344 $end_type = ($comp->GetType() ==
'VTODO' ?
'DUE' :
'DTEND');
1345 $duration = $comp->GetProperty(
'DURATION');
1346 if ( !isset($duration) || $duration->Value() ==
'' ) {
1347 $instance_start = $comp->GetProperty($dtstart_type);
1349 if ( $return_floating_times ) $dtsrt->setAsFloat();
1350 $instance_end = $comp->GetProperty($end_type);
1351 if ( isset($instance_end) ) {
1356 if ( $instance_start->GetParameterValue(
'VALUE') ==
'DATE' ) {
1368 if ( $utc < $start_utc ) {
1369 if ( isset($early_start) && isset($last_duration) && $duration->equals($last_duration) ) {
1370 if ( $utc < $early_start ) {
1371 if ( DEBUG_RRULE ) printf(
"Next please: $utc is before $early_start and before $start_utc.\n");
1377 $latest_start = clone($range_start);
1378 $latest_start->modify(
'-'.$duration);
1379 $early_start = $latest_start->FloatOrUTC($return_floating_times);
1380 $last_duration = $duration;
1381 if ( $utc < $early_start ) {
1382 if ( DEBUG_RRULE ) printf(
"Another please: $utc is before $early_start and before $start_utc.\n");
1387 $component = clone($comp);
1388 $component->ClearProperties( $clear_instance_props );
1389 $component->AddProperty($dtstart_type, $utc, ($is_date ? array(
'VALUE' =>
'DATE') : null) );
1390 $component->AddProperty(
'DURATION', $duration );
1391 if ( $has_repeats && $dtstart->FloatOrUTC($return_floating_times) != $utc )
1392 $component->AddProperty(
'RECURRENCE-ID', $utc, ($is_date ? array(
'VALUE' =>
'DATE') : null) );
1393 $new_components[$utc] = $component;
1397 foreach( $components AS $k => $comp ) {
1398 $p = $comp->GetProperty(
'RECURRENCE-ID');
1399 if ( isset($p) && $p->Value() !=
'') {
1400 $recurrence_id = $p->Value();
1403 $dtstart_prop = $comp->GetProperty(
'DTSTART');
1404 if ( !isset($dtstart_prop) && $comp->GetType() !=
'VTODO' ) {
1405 $dtstart_prop = $comp->GetProperty(
'DUE');
1408 if ( !isset($new_components[$recurrence_id]) && !isset($dtstart_prop) )
continue;
1410 $is_date = $dtstart_rrdt->isDate();
1411 if ( $return_floating_times ) $dtstart_rrdt->setAsFloat();
1412 $dtstart = $dtstart_rrdt->FloatOrUTC($return_floating_times);
1413 if ( !isset($new_components[$recurrence_id]) && $dtstart > $end_utc )
continue;
1415 $end_type = ($comp->GetType() ==
'VTODO' ?
'DUE' :
'DTEND');
1416 $duration = $comp->GetProperty(
'DURATION');
1418 if ( !isset($duration) || $duration->Value() ==
'' ) {
1419 $instance_end = $comp->GetProperty($end_type);
1420 if ( isset($instance_end) ) {
1422 if ( $return_floating_times ) $dtend_rrdt->setAsFloat();
1423 $dtend = $dtend_rrdt->FloatOrUTC($return_floating_times);
1428 $dtend = $dtstart + ($is_date ? $dtstart + 86400 : 0 );
1433 $dtend = $dtstart + $duration->
asSeconds();
1436 if ( !isset($new_components[$recurrence_id]) && $dtend < $start_utc )
continue;
1438 if ( DEBUG_RRULE ) printf(
"Replacing overridden instance at %s\n", $recurrence_id);
1439 $new_components[$recurrence_id] = $comp;
1443 $vResource->SetComponents($new_components);
1456 function getComponentRange(vComponent $comp, $fallback_tzid = null) {
1457 $dtstart_prop = $comp->GetProperty(
'DTSTART');
1458 $duration_prop = $comp->GetProperty(
'DURATION');
1459 if ( isset($duration_prop) ) {
1460 if ( !isset($dtstart_prop) )
throw new Exception(
'Invalid '.$comp->GetType().
' containing DURATION without DTSTART', 0);
1461 $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1462 $dtend = clone($dtstart);
1466 $completed_prop = null;
1467 switch ( $comp->GetType() ) {
1469 if ( !isset($dtstart_prop) )
throw new Exception(
'Invalid VEVENT without DTSTART', 0);
1470 $dtend_prop = $comp->GetProperty(
'DTEND');
1473 $completed_prop = $comp->GetProperty(
'COMPLETED');
1474 $dtend_prop = $comp->GetProperty(
'DUE');
1477 if ( !isset($dtstart_prop) )
1478 $dtstart_prop = $comp->GetProperty(
'DTSTAMP');
1479 $dtend_prop = $dtstart_prop;
1482 throw new Exception(
'getComponentRange cannot handle "'.$comp->GetType().
'" components', 0);
1485 if ( isset($dtstart_prop) )
1486 $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1490 if ( isset($dtend_prop) )
1491 $dtend = RepeatRuleDateTime::withFallbackTzid($dtend_prop, $fallback_tzid);
1495 if ( isset($completed_prop) ) {
1496 $completed = RepeatRuleDateTime::withFallbackTzid($completed_prop, $fallback_tzid);
1497 if ( !isset($dtstart) || (isset($dtstart) && $completed < $dtstart) ) $dtstart = $completed;
1498 if ( !isset($dtend) || (isset($dtend) && $completed > $dtend) ) $dtend = $completed;
1513 function getVCalendarRange( $vResource, $fallback_tzid = null ) {
1514 $components = $vResource->GetComponents();
1518 $earliest_start = null;
1520 $has_repeats =
false;
1521 foreach( $components AS $k => $comp ) {
1522 if ( $comp->GetType() ==
'VTIMEZONE' )
continue;
1523 $range = getComponentRange($comp, $fallback_tzid);
1524 $dtstart = $range->from;
1525 if ( !isset($dtstart) )
continue;
1526 $duration = $range->getDuration();
1528 $rrule = $comp->GetProperty(
'RRULE');
1529 $limited_occurrences =
true;
1530 if ( isset($rrule) ) {
1532 $limited_occurrences = $rule->hasLimitedOccurrences();
1535 if ( $limited_occurrences ) {
1536 $instances = array();
1537 $instances[$dtstart->FloatOrUTC()] = $dtstart;
1538 if ( !isset($range_end) ) {
1540 $range_end->modify(
'+150 years');
1542 $instances += rrule_expand($dtstart,
'RRULE', $comp, $range_end, null,
false, $fallback_tzid);
1543 $instances += rdate_expand($dtstart,
'RDATE', $comp, $range_end);
1544 foreach ( rdate_expand($dtstart,
'EXDATE', $comp, $range_end) AS $k => $v ) {
1545 unset($instances[$k]);
1547 if ( count($instances) < 1 ) {
1548 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1552 $instances = array_keys($instances);
1556 $last->modify($duration);
1557 if ( empty($earliest_start) || $first < $earliest_start ) $earliest_start = $first;
1558 if ( empty($latest_end) || $last > $latest_end ) $latest_end = $last;
1561 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
expand_byday_in_week( $day_in_week)
FloatOrUTC($return_floating_times=false)
RFC5545($return_floating_times=false)
static fromTwoDates( $d1, $d2)
__construct( $basedate, $rrule, $is_date=null, $return_floating_times=false)
__construct( $date1, $date2)
__construct( $in_duration)
next($return_floating_times=false)
static daysInMonth( $year, $month)
static rrule_expand_limit( $freq)
overlaps(RepeatRuleDateRange $other)