Skip to content

Commit

Permalink
Merge pull request #41300 from nextcloud/enh/add-timezone-generator
Browse files Browse the repository at this point in the history
Add timezone getter to ITimeFactory
  • Loading branch information
miaulalala authored Feb 13, 2024
2 parents 25344c2 + 6434ce9 commit 8822b16
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/private/AppFramework/Utility/TimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ public function withTimeZone(\DateTimeZone $timezone): static {

return $clone;
}

public function getTimeZone(?string $timezone = null): \DateTimeZone {
if ($timezone !== null) {
return new \DateTimeZone($timezone);
}
return $this->timezone;
}
}
8 changes: 8 additions & 0 deletions lib/public/AppFramework/Utility/ITimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null
* @since 26.0.0
*/
public function withTimeZone(\DateTimeZone $timezone): static;

/**
* @param string|null $timezone
* @return \DateTimeZone Requested timezone if provided, UTC otherwise
* @throws \Exception
* @since 29.0.0
*/
public function getTimeZone(?string $timezone = null): \DateTimeZone;
}
17 changes: 17 additions & 0 deletions tests/lib/AppFramework/Utility/TimeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,21 @@ public function testNowWithTimeZone(): void {
$now = $withTimeZone->now();
self::assertSame('Europe/Berlin', $now->getTimezone()->getName());
}

public function testGetTimeZone(): void {
$expected = new \DateTimeZone('Europe/Berlin');
$actual = $this->timeFactory->getTimeZone('Europe/Berlin');
self::assertEquals($expected, $actual);
}

public function testGetTimeZoneUTC(): void {
$expected = new \DateTimeZone('UTC');
$actual = $this->timeFactory->getTimeZone();
self::assertEquals($expected, $actual);
}

public function testGetTimeZoneInvalid(): void {
$this->expectException(\Exception::class);
$this->timeFactory->getTimeZone('blubblub');
}
}

0 comments on commit 8822b16

Please sign in to comment.