Skip to content

Commit

Permalink
Understand UNTIL clause
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Jan 15, 2015
1 parent 8f94fa0 commit 00a206b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/JMBTechnologyLimited/RRuleUnravel/ICalData.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class ICalData {

protected $excluded = array();

/** @var \DateTime */
protected $until;

function __construct(\DateTime $start = null, \DateTime $end = null, $data = null, $timezone = null)
{
$this->start = $start;
Expand Down Expand Up @@ -140,6 +143,10 @@ function setRRuleByArray($data)
{
$this->bymonthday = $value;
}
else if ($key == 'UNTIL')
{
$this->until = new \DateTime($value, $this->timezone);
}
}

}
Expand Down Expand Up @@ -424,6 +431,24 @@ public function getInterval()



/**
* @return boolean
*/
public function hasUntil()
{
return (boolean)$this->until;
}


/**
* @return \DateTime
*/
public function getUntil()
{
return $this->until;
}


}


20 changes: 17 additions & 3 deletions src/JMBTechnologyLimited/RRuleUnravel/Unraveler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ protected function stepBySimplePeriod($intervalString) {
$add = false;
// can also stop processing now
$process = false;
} else if ($this->icalDataUnravelling->getICalData()->hasUntil() && $start > $this->icalDataUnravelling->getICalData()->getUntil()) {
// it's inclusive, so we add this one but no mare
$process = false;
}

if ($add)
Expand All @@ -90,8 +93,10 @@ protected function stepBySimplePeriod($intervalString) {
}

// This is a temporary stop for rules with no count, so they stop sometime.
// Also > 2100 just as a safety to stop run away loops!
// Need to do better!
if (count($this->results) > 100)

if (count($this->results) > 100 || $start->format("Y") > 2100)
{
$process = false;
}
Expand Down Expand Up @@ -176,10 +181,15 @@ public function stepByDayMonthlyPeriod() {
$add = false;
// can also stop processing now
$process = false;
// TODO add below, with tests!
//} else if ($this->icalDataUnravelling->getICalData()->hasUntil() && $start > $this->icalDataUnravelling->getICalData()->getUntil()) {
// it's inclusive, so we add this one but no mare
// $process = false;
}
// This is a temporary stop for rules with no count, so they stop sometime.
// Need to do better!
if (count($this->results) > 100)
// Also > 2100 just as a safety to stop run away loops!
if (count($this->results) > 100 || $start->format("Y") > 2100)
{
$process = false;
}
Expand Down Expand Up @@ -210,7 +220,11 @@ public function stepByDayMonthlyPeriod() {
}

protected function addResult(\DateTime $start, \DateTime $end) {


if ($this->icalDataUnravelling->getICalData()->hasUntil() && $start > $this->icalDataUnravelling->getICalData()->getUntil()) {
return;
}

foreach($this->icalDataUnravelling->getICalData()->getExcluded() as $excluded) {
if ($start->getTimezone()->getName() != $excluded['datetime']->getTimezone()->getName()) {
// TODOD
Expand Down
32 changes: 32 additions & 0 deletions tests/JMBTechnologyLimited/RRuleUnravel/WeeklyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,38 @@ function testTwoWeeksPeriodWithOneException() {


}
function testWeeklyUntil() {
$icaldata = new ICalData(
new \DateTime("2015-02-02 09:00:00", new \DateTimeZone("UTC")),
new \DateTime("2015-02-02 10:00:00", new \DateTimeZone("UTC")),
"FREQ=WEEKLY;UNTIL=20150216T090000Z;BYDAY=MO",
"Europe/London");
$unraveler = new Unraveler($icaldata);
$unraveler->setIncludeOriginalEvent(true);
$unraveler->process();
$results = $unraveler->getResults();

$this->assertTrue(count($results) == 3);

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

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

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

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

$this->assertEquals("2015-02-16T09:00:00+00:00", $results[2]->getStartInUTC()->format("c"));
$this->assertEquals("2015-02-16T10:00:00+00:00", $results[2]->getEndInUTC()->format("c"));

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

}

}

Expand Down

0 comments on commit 00a206b

Please sign in to comment.