Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting UTC date time #482

Merged
merged 2 commits into from
Jan 17, 2023
Merged
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
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());
}
}