-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhpTimeService.php
48 lines (42 loc) · 1.11 KB
/
PhpTimeService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace DobroSite\TimeService;
/**
* Служба времени, использующая встроенные средства PHP.
*
* @since 1.0
*/
final class PhpTimeService implements TimeService
{
private \DateTimeZone $timezone;
/**
* @param string $timezone Используемый часовой пояс. Если не задан, будет взят из настроек PHP.
*
* @since 1.0
*/
public function __construct(string $timezone = '')
{
if ($timezone === '') {
$timezone = date_default_timezone_get();
}
$this->timezone = new \DateTimeZone($timezone);
}
/**
* Возвращает текущее время.
*
* @since 1.0
*/
public function getCurrentTime(): \DateTimeImmutable
{
return new \DateTimeImmutable('now', $this->timezone);
}
/**
* Возвращает часовой пояс.
*
* @since 1.0
*/
public function getTimeZone(): \DateTimeZone
{
return $this->getCurrentTime()->getTimezone();
}
}