new getAllOccurrences() method which also set cache, now use cache in findNext()

master
Tanguy Pruvot 2010-10-30 21:31:49 +02:00
rodzic ca0dcc1af0
commit 642115b1cc
1 zmienionych plików z 31 dodań i 6 usunięć

Wyświetl plik

@ -80,6 +80,24 @@ class SG_iCal_Freq {
} }
} }
/**
* Returns all timestamps array(), build the cache if not made before
* @return array
*/
public function getAllOccurrences() {
if (empty($this->cache)) {
//build cache
$i=0; $this->cache[0] = $this->start;
$next = $this->findNext($this->start);
while ($next) {
$i++; $this->cache[$i] = $next;
$next = $this->findNext($next);
}
}
return $this->cache;
}
/** /**
* Returns the previous (most recent) occurrence of the rule from the * Returns the previous (most recent) occurrence of the rule from the
* given offset * given offset
@ -88,12 +106,11 @@ class SG_iCal_Freq {
*/ */
public function previousOccurrence( $offset ) { public function previousOccurrence( $offset ) {
if (!empty($this->cache)) { if (!empty($this->cache)) {
reset($this->cache); $t2=$this->cache[0];
while( ($t2 = next($this->cache)) < $offset) { foreach($this->cache as $ts) {
if( $t2 == false ){ if ($ts >= $offset)
break; return $t2;
} $t2 = $ts;
$ts = $t2;
} }
} else { } else {
$ts = $this->start; $ts = $this->start;
@ -168,6 +185,14 @@ class SG_iCal_Freq {
* @return int * @return int
*/ */
public function findNext($offset) { public function findNext($offset) {
if (!empty($this->cache)) {
foreach($this->cache as $ts) {
if ($ts > $offset)
return $ts;
}
}
$debug = false; $debug = false;
//make sure the offset is valid //make sure the offset is valid