Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/Recur/EventIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Sabre\VObject\Component;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\Property\ICalendar\DateTime;
use Sabre\VObject\Settings;

/**
Expand Down Expand Up @@ -150,8 +151,12 @@ public function __construct($input, ?string $uid = null, ?\DateTimeZone $timeZon
$this->eventDuration =
$this->masterEvent->DTEND->getDateTime($this->timeZone)->getTimeStamp() -
$this->startDate->getTimeStamp();
$this->exactDuration =
$this->masterEvent->DTSTART->hasTime()
&& !$this->masterEvent->DTSTART->isFloating();
} elseif (isset($this->masterEvent->DURATION)) {
$duration = $this->masterEvent->DURATION->getDateInterval();
$this->nominalDuration = $duration;
$end = clone $this->startDate;
$end = $end->add($duration);
$this->eventDuration = $end->getTimeStamp() - $this->startDate->getTimeStamp();
Expand All @@ -163,8 +168,24 @@ public function __construct($input, ?string $uid = null, ?\DateTimeZone $timeZon

if (isset($this->masterEvent->RDATE)) {
$rdateValues = [];
$rdateDateTimes = [];
$hasPeriods = false;
foreach ($this->masterEvent->RDATE as $rdate) {
$rdateValues = array_merge($rdateValues, $rdate->getParts());
if ($rdate instanceof DateTime) {
// getDateTimes() reads the TZID of each RDATE property.
$rdateDateTimes = array_merge(
$rdateDateTimes,
$rdate->getDateTimes($this->startDate->getTimezone())
);
} else {
$hasPeriods = true;
}
}
if (!$hasPeriods) {
// RDATE properties in different timezones can be out of order.
sort($rdateDateTimes);
$rdateValues = $rdateDateTimes;
}
$this->recurIterator = new RDateIterator(
$rdateValues,
Expand Down Expand Up @@ -233,6 +254,15 @@ public function getDtEnd(): ?\DateTimeImmutable
}
$end = clone $this->currentDate;

// RFC 5545, section 3.8.5.3: with DTEND, all instances have the same
// exact duration. With DURATION, they have the same nominal duration.
if ($this->nominalDuration) {
return $end->add($this->nominalDuration);
}
if ($this->exactDuration) {
return $end->setTimestamp($end->getTimestamp() + $this->eventDuration);
}

return $end->modify('+'.$this->eventDuration.' seconds');
}

Expand Down Expand Up @@ -423,6 +453,17 @@ public function isInfinite(): bool
*/
protected $eventDuration;

/**
* The DURATION of the master event, if it has one.
*/
protected ?\DateInterval $nominalDuration = null;

/**
* True if the master event has a DTEND and a DTSTART with a time in a
* timezone. Its duration in seconds then applies to all instances.
*/
protected bool $exactDuration = false;

/**
* A reference to the main (master) event.
*/
Expand Down
18 changes: 11 additions & 7 deletions lib/Recur/RDateIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class RDateIterator implements \Iterator
/**
* Creates the Iterator.
*
* @param string|array $rrule
* The values are strings, parsed in the timezone of the start date, or
* DateTimeInterface objects.
*
* @param string|array<string|\DateTimeInterface> $rrule
*/
public function __construct($rrule, /**
* The reference start date/time for the rrule.
Expand Down Expand Up @@ -91,11 +94,10 @@ public function next(): void
return;
}

$this->currentDate =
DateTimeParser::parse(
$this->dates[$this->counter - 1],
$this->startDate->getTimezone()
);
$date = $this->dates[$this->counter - 1];
$this->currentDate = $date instanceof \DateTimeInterface
? \DateTimeImmutable::createFromInterface($date)->setTimezone($this->startDate->getTimezone())
: DateTimeParser::parse($date, $this->startDate->getTimezone());
}

/* End of Iterator implementation }}} */
Expand Down Expand Up @@ -140,7 +142,7 @@ public function fastForward(\DateTimeInterface $dt): void
* This method receives a string from an RRULE property, and populates this
* class with all the values.
*
* @param string|array $rdate
* @param string|array<string|\DateTimeInterface> $rdate
*/
protected function parseRDate($rdate): void
{
Expand All @@ -153,6 +155,8 @@ protected function parseRDate($rdate): void

/**
* Array with the RRULE dates.
*
* @var array<string|\DateTimeInterface>
*/
protected array $dates = [];
}
133 changes: 133 additions & 0 deletions tests/VObject/Recur/EventIterator/DurationAcrossDstTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Sabre\VObject\Recur\EventIterator;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Reader;
use Sabre\VObject\Recur\EventIterator;

/**
* The end of an instance that crosses a DST transition.
*
* RFC 5545, section 3.8.5.3: with DTEND, all instances have the same exact
* duration. With DURATION, they have the same nominal duration.
*
* @see https://github.com/sabre-io/vobject/issues/818
*/
class DurationAcrossDstTest extends TestCase
{
/**
* @return array<string, array{0: string, 1: string[], 2?: string}>
*/
public static function endProvider(): array
{
return [
// 23 hours: New York moves to summer time on March 14, 2027.
'DTEND, to summer time' => [
"DTSTART;TZID=America/New_York:20270313T120000\nDTEND;TZID=America/New_York:20270314T120000",
['2027-03-14 12:00 EDT', '2027-03-21 11:00 EDT'],
],
// 24 hours, and the second instance crosses the transition.
'DTEND, second instance to summer time' => [
"DTSTART;TZID=America/New_York:20270306T120000\nDTEND;TZID=America/New_York:20270307T120000",
['2027-03-07 12:00 EST', '2027-03-14 13:00 EDT'],
],
// 25 hours: New York moves to winter time on November 7, 2027.
'DTEND, to winter time' => [
"DTSTART;TZID=America/New_York:20271106T120000\nDTEND;TZID=America/New_York:20271107T120000",
['2027-11-07 12:00 EST', '2027-11-14 13:00 EST'],
],
'DURATION, to summer time' => [
"DTSTART;TZID=America/New_York:20270313T120000\nDURATION:P1D",
['2027-03-14 12:00 EDT', '2027-03-21 12:00 EDT'],
],
'DURATION, second instance to summer time' => [
"DTSTART;TZID=America/New_York:20270306T120000\nDURATION:P1D",
['2027-03-07 12:00 EST', '2027-03-14 12:00 EDT'],
],
'DURATION in hours, to summer time' => [
"DTSTART;TZID=America/New_York:20270313T120000\nDURATION:PT24H",
['2027-03-14 13:00 EDT', '2027-03-21 12:00 EDT'],
],
'DURATION, to winter time' => [
"DTSTART;TZID=America/New_York:20271106T120000\nDURATION:P1D",
['2027-11-07 12:00 EST', '2027-11-14 12:00 EST'],
],
'DURATION in hours, to winter time' => [
"DTSTART;TZID=America/New_York:20271106T120000\nDURATION:PT24H",
['2027-11-07 11:00 EST', '2027-11-14 12:00 EST'],
],
// The day is nominal, then the hours are exact: 01:00 EST plus
// 2 hours is 04:00 EDT.
'DURATION in days and hours, to summer time' => [
"DTSTART;TZID=America/New_York:20270313T010000\nDURATION:P1DT2H",
['2027-03-14 04:00 EDT', '2027-03-21 03:00 EDT'],
],
'Floating DURATION in hours, to summer time' => [
"DTSTART:20270306T120000\nDURATION:PT24H",
['2027-03-07 12:00 EST', '2027-03-14 13:00 EDT'],
'America/New_York',
],
'All-day DURATION, to summer time' => [
"DTSTART;VALUE=DATE:20270314\nDURATION:P1D",
['2027-03-15 00:00 EDT', '2027-03-22 00:00 EDT'],
'America/New_York',
],
];
}

/**
* @param string[] $expected
*/
#[DataProvider('endProvider')]
public function testGetDtEnd(string $times, array $expected, ?string $timeZone = null): void
{
$iterator = new EventIterator(
$this->calendar($times),
'test',
$timeZone ? new \DateTimeZone($timeZone) : null
);

$ends = [];
foreach ($iterator as $_) {
$ends[] = $iterator->getDtEnd()?->format('Y-m-d H:i T');
}

self::assertSame($expected, $ends);
}

public function testGetEventObject(): void
{
$iterator = new EventIterator(
$this->calendar("DTSTART;TZID=America/New_York:20270313T120000\nDTEND;TZID=America/New_York:20270314T120000"),
'test'
);

self::assertSame(
'2027-03-14 12:00 EDT',
$iterator->getEventObject()->DTEND?->getDateTime()->format('Y-m-d H:i T')
);
}

private function calendar(string $times): VCalendar
{
$calendar = Reader::read(<<<ICS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Sabre//Sabre VObject//EN
BEGIN:VEVENT
UID:test
DTSTAMP:20260101T000000Z
$times
RRULE:FREQ=WEEKLY;COUNT=2
SUMMARY:Test
END:VEVENT
END:VCALENDAR
ICS);
self::assertInstanceOf(VCalendar::class, $calendar);

return $calendar;
}
}
112 changes: 112 additions & 0 deletions tests/VObject/Recur/EventIterator/RDateTimezoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Sabre\VObject\Recur\EventIterator;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Reader;
use Sabre\VObject\Recur\EventIterator;

/**
* An RDATE with its own TZID.
*
* @see https://github.com/sabre-io/vobject/issues/819
*/
class RDateTimezoneTest extends TestCase
{
/**
* @return array<string, array{string, string[]}>
*/
public static function startProvider(): array
{
return [
// 10:00 in Paris is 09:00 UTC in January.
'TZID' => [
"DTSTART:20270104T100000Z\nRDATE;TZID=Europe/Paris:20270108T100000",
['2027-01-04 10:00', '2027-01-08 09:00'],
],
// 04:00 in New York is 09:00 UTC, 17:00 in Tokyo is 08:00 UTC.
'several TZID' => [
"DTSTART:20270104T100000Z\nRDATE;TZID=America/New_York:20270108T040000\nRDATE;TZID=Asia/Tokyo:20270108T170000",
['2027-01-04 10:00', '2027-01-08 08:00', '2027-01-08 09:00'],
],
'UTC and TZID' => [
"DTSTART;TZID=Europe/Paris:20270104T100000\nRDATE:20270108T090000Z\nRDATE;TZID=Asia/Tokyo:20270108T170000",
['2027-01-04 09:00', '2027-01-08 08:00', '2027-01-08 09:00'],
],
// Without a TZID, the RDATE is in the timezone of DTSTART.
'no TZID' => [
"DTSTART;TZID=Europe/Paris:20270104T100000\nRDATE:20270108T100000",
['2027-01-04 09:00', '2027-01-08 09:00'],
],
'all day' => [
"DTSTART;VALUE=DATE:20270104\nRDATE;VALUE=DATE:20270108",
['2027-01-04 00:00', '2027-01-08 00:00'],
],
];
}

/**
* @param string[] $expected start of each instance, in UTC
*/
#[DataProvider('startProvider')]
public function testStart(string $dates, array $expected): void
{
$iterator = new EventIterator($this->calendar($dates), 'test');

$utc = new \DateTimeZone('UTC');
$starts = [];
foreach ($iterator as $_) {
$starts[] = $iterator->getDtStart()?->setTimezone($utc)->format('Y-m-d H:i');
}

self::assertSame($expected, $starts);
}

public function testStartKeepsTheTimezoneOfDtstart(): void
{
$iterator = new EventIterator(
$this->calendar("DTSTART;TZID=Europe/Paris:20270104T100000\nRDATE;TZID=America/New_York:20270108T040000"),
'test'
);
$iterator->next();

self::assertSame('2027-01-08 10:00 Europe/Paris', $iterator->getDtStart()?->format('Y-m-d H:i e'));
}

/**
* The RDATE starts at 00:30 EST, before the move to summer time. It keeps
* the exact duration of 3 hours.
*/
public function testEndAcrossDst(): void
{
$iterator = new EventIterator(
$this->calendar("DTSTART;TZID=America/New_York:20270306T120000\nDTEND;TZID=America/New_York:20270306T150000\nRDATE:20270314T053000Z"),
'test'
);
$iterator->next();

self::assertSame('2027-03-14 00:30 EST', $iterator->getDtStart()?->format('Y-m-d H:i T'));
self::assertSame('2027-03-14 04:30 EDT', $iterator->getDtEnd()?->format('Y-m-d H:i T'));
}

private function calendar(string $dates): VCalendar
{
$calendar = Reader::read(<<<ICS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Sabre//Sabre VObject//EN
BEGIN:VEVENT
UID:test
DTSTAMP:20260101T000000Z
$dates
SUMMARY:Test
END:VEVENT
END:VCALENDAR
ICS);
self::assertInstanceOf(VCalendar::class, $calendar);

return $calendar;
}
}
19 changes: 19 additions & 0 deletions tests/VObject/Recur/RDateIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ public function testTimezone(): void
self::assertFalse($it->isInfinite());
}

public function testDateTimeValues(): void
{
$tz = new \DateTimeZone('Europe/Berlin');
$it = new RDateIterator(
[new \DateTimeImmutable('2014-09-01 00:00:00', new \DateTimeZone('America/New_York'))],
new \DateTimeImmutable('2014-08-01 00:00:00', $tz)
);

$expected = [
new \DateTimeImmutable('2014-08-01 00:00:00', $tz),
new \DateTimeImmutable('2014-09-01 06:00:00', $tz),
];

self::assertEquals(
$expected,
iterator_to_array($it)
);
}

public function testFastForward(): void
{
$utc = new \DateTimeZone('UTC');
Expand Down