Skip to content

Commit

Permalink
Fix formatting UTC date time (#482)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markuspoerschke authored Jan 17, 2023
1 parent caf1d48 commit 37bda3f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Presentation/Component/Property/Value/DateTimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Presentation/Factory/DateTimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
];
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Presentation/Factory/DateTimeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 37bda3f

Please sign in to comment.