-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathTimeZoneRegion.php
More file actions
123 lines (106 loc) · 3.42 KB
/
Copy pathTimeZoneRegion.php
File metadata and controls
123 lines (106 loc) · 3.42 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Brick\DateTime;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\Parser\DateTimeParser;
use Brick\DateTime\Parser\DateTimeParseResult;
use Brick\DateTime\Parser\IsoParsers;
use DateTime;
use DateTimeZone;
use Exception;
/**
* A geographical region where the same time-zone rules apply, such as `Europe/London`.
*/
final class TimeZoneRegion extends TimeZone
{
/**
* Private constructor. Use a factory method to obtain an instance.
*/
private function __construct(
private readonly DateTimeZone $zone,
) {
}
/**
* @param string $id The region id.
*
* @throws DateTimeException If the region id is invalid.
*/
public static function of(string $id): TimeZoneRegion
{
if ($id === '' || $id === 'Z' || $id === 'z' || $id[0] === '+' || $id[0] === '-') {
// DateTimeZone would accept offsets, but TimeZoneRegion targets regions only.
throw DateTimeException::unknownTimeZoneRegion($id);
}
try {
return new TimeZoneRegion(new DateTimeZone($id));
} catch (Exception) {
throw DateTimeException::unknownTimeZoneRegion($id);
}
}
/**
* @throws DateTimeException If the region is not valid.
* @throws DateTimeParseException If required fields are missing from the result.
*/
public static function from(DateTimeParseResult $result): TimeZoneRegion
{
$region = $result->getField(Field\TimeZoneRegion::NAME);
return TimeZoneRegion::of($region);
}
public static function utc() : TimeZoneRegion
{
return TimeZoneRegion::of('UTC');
}
/**
* Returns all the available time-zone identifiers.
*
* @param bool $includeObsolete Whether to include obsolete time-zone identifiers. Defaults to false.
*
* @return string[] An array of time-zone identifiers.
*/
public static function getAllIdentifiers(bool $includeObsolete = false): array
{
return DateTimeZone::listIdentifiers(
$includeObsolete
? DateTimeZone::ALL_WITH_BC
: DateTimeZone::ALL,
);
}
/**
* Returns the time-zone identifiers for the given country.
*
* If the country code is not known, an empty array is returned.
*
* @param string $countryCode The ISO 3166-1 two-letter country code.
*
* @return string[] An array of time-zone identifiers.
*/
public static function getIdentifiersForCountry(string $countryCode): array
{
return DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $countryCode);
}
/**
* Parses a region id, such as 'Europe/London'.
*
* @throws DateTimeParseException
*/
public static function parse(string $text, ?DateTimeParser $parser = null): TimeZoneRegion
{
if ($parser === null) {
$parser = IsoParsers::timeZoneRegion();
}
return TimeZoneRegion::from($parser->parse($text));
}
public function getId(): string
{
return $this->zone->getName();
}
public function getOffset(Instant $pointInTime): int
{
$dateTime = new DateTime('@' . $pointInTime->getEpochSecond(), new DateTimeZone('UTC'));
return $this->zone->getOffset($dateTime);
}
public function toNativeDateTimeZone(): DateTimeZone
{
return clone $this->zone;
}
}