diff --git a/lib/Recur/EventIterator.php b/lib/Recur/EventIterator.php index 5db409350..67e54fa39 100644 --- a/lib/Recur/EventIterator.php +++ b/lib/Recur/EventIterator.php @@ -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; /** @@ -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(); @@ -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, @@ -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'); } @@ -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. */ diff --git a/lib/Recur/RDateIterator.php b/lib/Recur/RDateIterator.php index b9bd0beff..f5185e692 100644 --- a/lib/Recur/RDateIterator.php +++ b/lib/Recur/RDateIterator.php @@ -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 $rrule */ public function __construct($rrule, /** * The reference start date/time for the rrule. @@ -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 }}} */ @@ -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 $rdate */ protected function parseRDate($rdate): void { @@ -153,6 +155,8 @@ protected function parseRDate($rdate): void /** * Array with the RRULE dates. + * + * @var array */ protected array $dates = []; } diff --git a/tests/VObject/Recur/EventIterator/DurationAcrossDstTest.php b/tests/VObject/Recur/EventIterator/DurationAcrossDstTest.php new file mode 100644 index 000000000..6c6988c04 --- /dev/null +++ b/tests/VObject/Recur/EventIterator/DurationAcrossDstTest.php @@ -0,0 +1,133 @@ + + */ + 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(<< + */ + 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(<<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');