mirror of
https://github.com/nextcloud/server.git
synced 2026-06-09 08:44:07 -04:00
Merge pull request #41300 from nextcloud/enh/add-timezone-generator
Add timezone getter to ITimeFactory
This commit is contained in:
commit
8822b16d37
3 changed files with 32 additions and 0 deletions
|
|
@ -73,4 +73,11 @@ class TimeFactory implements ITimeFactory {
|
|||
|
||||
return $clone;
|
||||
}
|
||||
|
||||
public function getTimeZone(?string $timezone = null): \DateTimeZone {
|
||||
if ($timezone !== null) {
|
||||
return new \DateTimeZone($timezone);
|
||||
}
|
||||
return $this->timezone;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,4 +58,12 @@ interface ITimeFactory extends ClockInterface {
|
|||
* @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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,4 +46,21 @@ class TimeFactoryTest extends \Test\TestCase {
|
|||
$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');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue