Browse Source

Merge pull request #41300 from nextcloud/enh/add-timezone-generator

Add timezone getter to ITimeFactory
tags/v29.0.0beta1
Anna 3 months ago
parent
commit
8822b16d37
No account linked to committer's email address

+ 7
- 0
lib/private/AppFramework/Utility/TimeFactory.php View File



return $clone; return $clone;
} }

public function getTimeZone(?string $timezone = null): \DateTimeZone {
if ($timezone !== null) {
return new \DateTimeZone($timezone);
}
return $this->timezone;
}
} }

+ 8
- 0
lib/public/AppFramework/Utility/ITimeFactory.php View File

* @since 26.0.0 * @since 26.0.0
*/ */
public function withTimeZone(\DateTimeZone $timezone): static; 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
- 0
tests/lib/AppFramework/Utility/TimeFactoryTest.php View File

$now = $withTimeZone->now(); $now = $withTimeZone->now();
self::assertSame('Europe/Berlin', $now->getTimezone()->getName()); 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…
Cancel
Save