Skip to content

Commit

Permalink
Can handle exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Jan 12, 2015
1 parent a25d6f3 commit f72c10b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
33 changes: 32 additions & 1 deletion src/JMBTechnologyLimited/RRuleUnravel/ICalData.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ICalData {

protected $interval = 1;

protected $excluded = array();

function __construct(\DateTime $start = null, \DateTime $end = null, $data = null, $timezone = null)
{
Expand Down Expand Up @@ -143,6 +144,30 @@ function setRRuleByArray($data)

}


public function setExDateByString($exdtval, $exdtparam = "") {

$this->excluded = array();

$timezone = new \DateTimeZone("UTC");

foreach(explode(";", $exdtparam) as $exdtparamBit) {
if (strpos( $exdtparamBit, "=") !== false) {
list($k, $v) = explode("=", $exdtparamBit, 2);
if ($k == "TZID") {
$timezone = new \DateTimeZone($v);
}
}
}

foreach(explode(",",$exdtval) as $exdtvalBit) {
if ($exdtvalBit) {
$this->excluded[] = new \DateTime($exdtvalBit, $timezone);
}
}

}

/**
* @param \DateTime $end
*/
Expand Down Expand Up @@ -194,7 +219,13 @@ public function getTimezone()
return $this->timezone;
}


/**
* @return array
*/
public function getExcluded()
{
return $this->excluded;
}

/**
* @return mixed
Expand Down
20 changes: 15 additions & 5 deletions src/JMBTechnologyLimited/RRuleUnravel/Unraveler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function stepBySimplePeriod($intervalString) {

if ($add)
{
$this->results[] = new UnravelerResult(clone $start, clone $end);
$this->addResult($start, $end);
$this->icalDataUnravelling->decreaseCount();
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public function stepByDayMonthlyPeriod() {
}

if ($this->includeOriginalEvent) {
$this->results[] = new UnravelerResult(clone $start, clone $end);
$this->addResult($start, $end);
}

$process = true;
Expand Down Expand Up @@ -168,7 +168,7 @@ public function stepByDayMonthlyPeriod() {

if ($add && $process) {

$this->results[] = new UnravelerResult($monthCalendar->getStart($i), $monthCalendar->getEnd($i));
$this->addResult($monthCalendar->getStart($i), $monthCalendar->getEnd($i));
$this->icalDataUnravelling->decreaseCount();


Expand Down Expand Up @@ -209,8 +209,18 @@ public function stepByDayMonthlyPeriod() {

}



protected function addResult(\DateTime $start, \DateTime $end) {
foreach($this->icalDataUnravelling->getICalData()->getExcluded() as $excluded) {
if ($start->getTimezone()->getName() != $excluded->getTimezone()->getName()) {
// TODOD
} else {
if ($excluded->format("c") == $start->format("c")) {
return;
}
}
}
$this->results[] = new UnravelerResult(clone $start, clone $end);
}

/**
* @param boolean $includeOriginalEvent
Expand Down
46 changes: 46 additions & 0 deletions tests/JMBTechnologyLimited/RRuleUnravel/WeeklyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,52 @@ function testTwoWeeksPeriod() {

}



function testTwoWeeksPeriodWithOneException() {
$icaldata = new ICalData(
new \DateTime("2015-02-12 09:00:00", new \DateTimeZone("UTC")),
new \DateTime("2015-02-12 10:00:00", new \DateTimeZone("UTC")),
"FREQ=WEEKLY;INTERVAL=2;BYDAY=TH",
"Europe/London");
$icaldata->setExDateByString("20150226T090000","TZID=Europe/London");
$unraveler = new Unraveler($icaldata);
$unraveler->setIncludeOriginalEvent(false);
$unraveler->process();
$results = $unraveler->getResults();

$this->assertTrue(count($results) > 5);

$this->assertEquals("2015-03-12T09:00:00+00:00", $results[0]->getStartInUTC()->format("c"));
$this->assertEquals("2015-03-12T10:00:00+00:00", $results[0]->getEndInUTC()->format("c"));

$this->assertEquals("2015-03-12T09:00:00+00:00", $results[0]->getStart()->format("c"));
$this->assertEquals("2015-03-12T10:00:00+00:00", $results[0]->getEnd()->format("c"));

$this->assertEquals("2015-03-26T09:00:00+00:00", $results[1]->getStartInUTC()->format("c"));
$this->assertEquals("2015-03-26T10:00:00+00:00", $results[1]->getEndInUTC()->format("c"));

$this->assertEquals("2015-03-26T09:00:00+00:00", $results[1]->getStart()->format("c"));
$this->assertEquals("2015-03-26T10:00:00+00:00", $results[1]->getEnd()->format("c"));

// BST date shift

$this->assertEquals("2015-04-09T08:00:00+00:00", $results[2]->getStartInUTC()->format("c"));
$this->assertEquals("2015-04-09T09:00:00+00:00", $results[2]->getEndInUTC()->format("c"));

$this->assertEquals("2015-04-09T09:00:00+01:00", $results[2]->getStart()->format("c"));
$this->assertEquals("2015-04-09T10:00:00+01:00", $results[2]->getEnd()->format("c"));


$this->assertEquals("2015-04-23T08:00:00+00:00", $results[3]->getStartInUTC()->format("c"));
$this->assertEquals("2015-04-23T09:00:00+00:00", $results[3]->getEndInUTC()->format("c"));

$this->assertEquals("2015-04-23T09:00:00+01:00", $results[3]->getStart()->format("c"));
$this->assertEquals("2015-04-23T10:00:00+01:00", $results[3]->getEnd()->format("c"));


}

}


Expand Down

0 comments on commit f72c10b

Please sign in to comment.