From 37bda3f3fd8a02bd96557fdc00dc3d888738c2a6 Mon Sep 17 00:00:00 2001 From: Markus Poerschke Date: Tue, 17 Jan 2023 11:26:56 +0100 Subject: [PATCH] Fix formatting UTC date time (#482) Rather than setting TZID=UTC, date times that are using UTC must end with the Z letter. Wrong: DTSTART;TZID=UTC:20220812T090000 Correct: DTSTART;20220812T090000Z Fixes: #462 Fixes: #263 --- CHANGELOG.md | 4 ++++ .../Component/Property/Value/DateTimeValue.php | 4 ++++ src/Presentation/Factory/DateTimeFactory.php | 6 +++++- tests/Unit/Presentation/Factory/DateTimeFactoryTest.php | 9 +++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4740f6b2..1b79afa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fix formatting date time property with UTC time zone [#482](https://github.com/markuspoerschke/iCal/pull/482) + ## [2.9.0] - 2022-12-23 ### Changed diff --git a/src/Presentation/Component/Property/Value/DateTimeValue.php b/src/Presentation/Component/Property/Value/DateTimeValue.php index 63b3e934..25766839 100644 --- a/src/Presentation/Component/Property/Value/DateTimeValue.php +++ b/src/Presentation/Component/Property/Value/DateTimeValue.php @@ -41,6 +41,10 @@ private function convertTimestampToString(Timestamp $timestamp): string private function convertDateTimeToString(DateTime $dateTime): string { + if ($dateTime->hasDateTimeZone() && $dateTime->getDateTimeZone()->getName() === 'UTC') { + return $dateTime->getDateTime()->format(self::FORMAT_UTC); + } + return $dateTime->getDateTime()->format(self::FORMAT); } diff --git a/src/Presentation/Factory/DateTimeFactory.php b/src/Presentation/Factory/DateTimeFactory.php index efa9a30a..45abf67c 100644 --- a/src/Presentation/Factory/DateTimeFactory.php +++ b/src/Presentation/Factory/DateTimeFactory.php @@ -37,7 +37,11 @@ public function createProperty(string $propertyName, PointInTime $pointInTime): */ private function getParameters(PointInTime $pointInTime): array { - if ($pointInTime instanceof DateTime && $pointInTime->hasDateTimeZone()) { + if ( + $pointInTime instanceof DateTime + && $pointInTime->hasDateTimeZone() + && $pointInTime->getDateTimeZone()->getName() !== 'UTC' + ) { return [ new Property\Parameter('TZID', new TextValue($pointInTime->getDateTimeZone()->getName())), ]; diff --git a/tests/Unit/Presentation/Factory/DateTimeFactoryTest.php b/tests/Unit/Presentation/Factory/DateTimeFactoryTest.php index cdff4001..a3d42978 100644 --- a/tests/Unit/Presentation/Factory/DateTimeFactoryTest.php +++ b/tests/Unit/Presentation/Factory/DateTimeFactoryTest.php @@ -27,4 +27,13 @@ public function testCreateProperty(): void self::assertSame('DTSTART;TZID=Europe/Berlin:20210122T111213', $property->__toString()); } + + public function testUtcTimeZone(): void + { + $dateTime = new DateTime(new PhpDateTimeImmutable('2022-12-25 23:29:00', new DateTimeZone('UTC')), true); + + $property = (new DateTimeFactory())->createProperty('DTSTART', $dateTime); + + self::assertSame('DTSTART:20221225T232900Z', $property->__toString()); + } }