Skip to content

Commit

Permalink
Merge pull request #184 from atymic/fix/all-day
Browse files Browse the repository at this point in the history
fix: all day event issues
  • Loading branch information
alies-dev authored Jan 26, 2024
2 parents b5c8abc + e84231e commit 99c131f
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor
coverage
.phpunit.result.cache
.php-cs-fixer.cache
.phpunit.cache
4 changes: 2 additions & 2 deletions src/Generators/Ics.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function generate(Link $link): string
$dateTimeFormat = $link->allDay ? $this->dateFormat : $this->dateTimeFormat;

if ($link->allDay) {
$url[] = 'DTSTAMP:'.gmdate($dateTimeFormat, $link->from->getTimestamp());
$url[] = 'DTSTART:'.gmdate($dateTimeFormat, $link->from->getTimestamp());
$url[] = 'DTSTAMP:'.$link->from->format($dateTimeFormat);
$url[] = 'DTSTART:'.$link->from->format($dateTimeFormat);
$url[] = 'DURATION:P'.(max(1, $link->from->diff($link->to)->days)).'D';
} else {
$url[] = 'DTSTAMP:'.gmdate($dateTimeFormat, $link->from->getTimestamp());
Expand Down
13 changes: 13 additions & 0 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function __construct(string $title, \DateTimeInterface $from, \DateTimeIn
*/
public static function create(string $title, \DateTimeInterface $from, \DateTimeInterface $to, bool $allDay = false)
{
// When creating all day events, we need to be in the UTC timezone as all day events are "floating" based on the user's timezone
if ($allDay) {
$startDate = new \DateTime($from->format('Y-m-d'), new \DateTimeZone('UTC'));
$numberOfDays = $from->diff($to)->days + 1;

return self::createAllDay($title, $startDate, $numberOfDays);
}

return new static($title, $from, $to, $allDay);
}

Expand All @@ -74,6 +82,11 @@ public static function create(string $title, \DateTimeInterface $from, \DateTime
*/
public static function createAllDay(string $title, \DateTimeInterface $fromDate, int $numberOfDays = 1): self
{
// In cases where the from date is not UTC, make sure it's UTC, size all day events are floating and non UTC dates cause bugs in the generators
if ($fromDate->getTimezone() !== new \DateTimeZone('UTC')) {
$fromDate = \DateTime::createFromFormat('Y-m-d', $fromDate->format('Y-m-d'));
}

$from = (clone $fromDate)->modify('midnight');
$to = (clone $from)->modify("+$numberOfDays days");

Expand Down
16 changes: 16 additions & 0 deletions tests/Generators/GoogleGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ protected function linkMethodName(): string
{
return 'google';
}

/** @test */
public function it_correctly_generates_all_day_events_by_days(): void
{
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createAllDayEventMultipleDaysWithTimezoneLink())
);
}

/** @test */
public function it_correctly_generates_all_day_events_by_dates(): void
{
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createEventMultipleDaysViaStartEndWithTimezoneLink())
);
}
}
16 changes: 16 additions & 0 deletions tests/Generators/IcsGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,20 @@ public function it_has_a_product_dtstamp(): void
$this->generator(['DTSTAMP' => '20180201T090000Z'])->generate($this->createShortEventLink())
);
}

/** @test */
public function it_correctly_generates_all_day_events_by_days(): void
{
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createAllDayEventMultipleDaysWithTimezoneLink())
);
}

/** @test */
public function it_correctly_generates_all_day_events_by_dates(): void
{
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createEventMultipleDaysViaStartEndWithTimezoneLink())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://calendar.google.com/calendar/render?action=TEMPLATE&dates=20240125/20240131&ctz=UTC&text=All+day+bugs&details=Testing+all+day
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://calendar.google.com/calendar/render?action=TEMPLATE&dates=20240125/20240130&ctz=UTC&text=All+day+bugs&details=Testing+all+day
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:Spatie calendar-links
BEGIN:VEVENT
UID:8fe3eececcd6b020db3b47ef55e7cf89
SUMMARY:All day bugs
DTSTAMP:20240125
DTSTART:20240125
DURATION:P6D
DESCRIPTION:Testing all day
END:VEVENT
END:VCALENDAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:Spatie calendar-links
BEGIN:VEVENT
UID:b4be522f87b9894dadd2b9cd5479136b
SUMMARY:All day bugs
DTSTAMP:20240125
DTSTART:20240125
DURATION:P5D
DESCRIPTION:Testing all day
END:VEVENT
END:VCALENDAR
27 changes: 27 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ protected function createMultipleDaysAllDayEventLink(bool $immutable = false): L
)->description($description)->address('Party Lane 1A, 1337 Funtown');
}

protected function createAllDayEventMultipleDaysWithTimezoneLink(bool $immutable = false): Link
{
$description = 'Testing all day';

$dateTimeClass = $immutable ? DateTimeImmutable::class : DateTime::class;

return Link::createAllDay(
'All day bugs',
$dateTimeClass::createFromFormat('Y-m-d', '2024-01-25', new DateTimeZone('Pacific/Wake'))->setTime(0,0),
5
)->description($description);
}

protected function createEventMultipleDaysViaStartEndWithTimezoneLink(bool $immutable = false): Link
{
$description = 'Testing all day';

$dateTimeClass = $immutable ? DateTimeImmutable::class : DateTime::class;

return Link::create(
'All day bugs',
$dateTimeClass::createFromFormat('Y-m-d', '2024-01-25', new DateTimeZone('Pacific/Wake'))->setTime(0,0),
$dateTimeClass::createFromFormat('Y-m-d', '2024-01-30', new DateTimeZone('Pacific/Wake'))->setTime(0,0),
true,
)->description($description);
}

protected function createDescriptionIsHTMLcodeEventLink(bool $immutable = false): Link
{
$description = 'With balloons, clowns and stuff
Expand Down

0 comments on commit 99c131f

Please sign in to comment.