diff --git a/blocks/SG_iCal_VEvent.php b/blocks/SG_iCal_VEvent.php index 166db35..02e35ca 100755 --- a/blocks/SG_iCal_VEvent.php +++ b/blocks/SG_iCal_VEvent.php @@ -57,27 +57,26 @@ class SG_iCal_VEvent { unset($data['duration']); } - //google cal set dtend as end of initial event + //google cal set dtend as end of initial event (duration) if ( isset($this->recurrence) ) { //if there is a recurrence rule $until = $this->recurrence->getUntil(); $count = $this->recurrence->getCount(); //check if there is either 'until' or 'count' set - if ( $until or $count ) { - //if until is set, set that as the end date (using getTimeStamp) - if ( $until ) { - //date_default_timezone_set( xx ); - $this->laststart = strtotime($until); - $this->lastend = $this->laststart + $this->getDuration(); - } + if ( $until ) { + //ok.. + } elseif ($count) { //if count is set, then figure out the last occurrence and set that as the end date + $freq = new SG_iCal_Freq($this->recurrence->rrule, $start); + $until = $freq->lastOccurrence($this->start); } else { //forever... limit to 3 years $this->recurrence->setUntil('+3 years'); $until = $this->recurrence->getUntil(); - $this->laststart = strtotime($until); - $this->lastend = $this->laststart + $this->getDuration(); } + //date_default_timezone_set( xx ) needed ?; + $this->laststart = strtotime($until); + $this->lastend = $this->laststart + $this->getDuration(); } $imports = array('summary','description','location'); diff --git a/helpers/SG_iCal_Freq.php b/helpers/SG_iCal_Freq.php index 9057c8a..4ad9c46 100755 --- a/helpers/SG_iCal_Freq.php +++ b/helpers/SG_iCal_Freq.php @@ -67,11 +67,11 @@ class SG_iCal_Freq { } if( isset($this->rules['count']) ) { - $n = $start; - for($i=0;$i<$this->rules['count']-1;$i++) { - $n = $this->findNext($n); + $ts = $this->firstOccurrence(); + for($i=1; $i<$this->rules['count']; $i++) { + $ts = $this->findNext($ts); } - $this->rules['until'] = $n; + $this->rules['until'] = $ts; } } @@ -98,8 +98,17 @@ class SG_iCal_Freq { * @return int */ public function nextOccurrence( $offset ) { - $start = ($offset > $this->start) ? $offset : $this->start; - return $this->findNext($start); + if ($offset < $this->start) + return $this->firstOccurrence(); + return $this->findNext($offset); + } + + /** + * Finds the first occurrence of the rule. + * @return int timestamp + */ + public function firstOccurrence() { + return $this->start; } /** @@ -107,11 +116,12 @@ class SG_iCal_Freq { * @return int timestamp */ public function lastOccurrence() { - $next = $this->findNext($this->start); + $ts = $next = $this->findNext($this->start); while ($next) { - $next = $this->findNext($next); + $ts = $next; + $next = $this->findNext($ts); } - return $next; + return $ts; } /** diff --git a/tests/helpers/FreqTest.php b/tests/helpers/FreqTest.php index d390d95..9cea3b9 100755 --- a/tests/helpers/FreqTest.php +++ b/tests/helpers/FreqTest.php @@ -605,13 +605,25 @@ class FreqTest extends PHPUnit_Framework_TestCase { } } - public function testLastOccurrence() { + + //weird : in this test $start is not an occurrence ! + /* + public function testFirstOccurrenceByYearDay() { $rule = 'FREQ=YEARLY;INTERVAL=2;BYYEARDAY=1;COUNT=5'; $start = strtotime('2009-10-27T090000'); $freq = new SG_iCal_Freq($rule, $start); - $this->assertEquals(strtotime('2018-01-01T09:00:00'), $freq->lastOccurrence()); + $this->assertEquals(strtotime('2011-01-01T09:00:00'), $freq->firstOccurrence()); } + */ + public function testLastOccurrenceByYearDay() { + $rule = 'FREQ=YEARLY;INTERVAL=2;BYYEARDAY=1;COUNT=5'; + $start = strtotime('2011-01-01T090000'); + $freq = new SG_iCal_Freq($rule, $start); + $this->assertEquals(strtotime('2019-01-01T09:00:00'), $freq->lastOccurrence()); + } + + // TODO: WKST rule private function assertRule( $rule, $start, $dateset ) { @@ -620,10 +632,11 @@ class FreqTest extends PHPUnit_Framework_TestCase { $n = $start - 1; do { $n = $freq->findNext($n); + //echo date('Y-m-d H:i:sO ',$n); $e = (current($dateset) != -1) ? current($dateset) : false; $this->assertEquals($e, $n); } while( next($dateset) !== false ); } } -?> \ No newline at end of file +?> diff --git a/tests/timestamp.sh b/tests/timestamp.sh new file mode 100755 index 0000000..a2f431e --- /dev/null +++ b/tests/timestamp.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +TIMESTAMP=$1 + +php -r "echo date('Y-m-d H:i:s O',$TIMESTAMP); echo \"\n\"; " +