CarbonTimeZone.php
5.45 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Carbon;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
class CarbonTimeZone extends DateTimeZone
{
public function __construct($timezone = null)
{
parent::__construct(static::getDateTimeZoneNameFromMixed($timezone));
}
protected static function getDateTimeZoneNameFromMixed($timezone)
{
if (is_null($timezone)) {
$timezone = date_default_timezone_get();
} elseif (is_numeric($timezone)) {
if ($timezone <= -100 || $timezone >= 100) {
throw new InvalidArgumentException('Absolute timezone offset cannot be greater than 100.');
}
$timezone = ($timezone >= 0 ? '+' : '').$timezone.':00';
}
return $timezone;
}
protected static function getDateTimeZoneFromName(&$name)
{
return @timezone_open($name = (string) static::getDateTimeZoneNameFromMixed($name));
}
/**
* Create a CarbonTimeZone from mixed input.
*
* @param DateTimeZone|string|int|null $object original value to get CarbonTimeZone from it.
* @param DateTimeZone|string|int|null $objectDump dump of the object for error messages.
*
* @return false|static
*/
public static function instance($object = null, $objectDump = null)
{
$tz = $object;
if ($tz instanceof static) {
return $tz;
}
if ($tz === null) {
return new static();
}
if (!$tz instanceof DateTimeZone) {
$tz = static::getDateTimeZoneFromName($object);
}
if ($tz === false) {
if (Carbon::isStrictModeEnabled()) {
throw new InvalidArgumentException('Unknown or bad timezone ('.($objectDump ?: $object).')');
}
return false;
}
return new static($tz->getName());
}
/**
* Returns abbreviated name of the current timezone according to DST setting.
*
* @param bool $dst
*
* @return string
*/
public function getAbbreviatedName($dst = false)
{
$name = $this->getName();
foreach ($this->listAbbreviations() as $abbreviation => $zones) {
foreach ($zones as $zone) {
if ($zone['timezone_id'] === $name && $zone['dst'] == $dst) {
return $abbreviation;
}
}
}
return 'unknown';
}
/**
* @alias getAbbreviatedName
*
* Returns abbreviated name of the current timezone according to DST setting.
*
* @param bool $dst
*
* @return string
*/
public function getAbbr($dst = false)
{
return $this->getAbbreviatedName($dst);
}
/**
* Get the offset as string "sHH:MM" (such as "+00:00" or "-12:30").
*
* @param DateTimeInterface|null $date
*
* @return string
*/
public function toOffsetName(DateTimeInterface $date = null)
{
$minutes = floor($this->getOffset($date ?: Carbon::now($this)) / 60);
$hours = floor($minutes / 60);
$minutes = str_pad(abs($minutes) % 60, 2, '0', STR_PAD_LEFT);
return ($hours < 0 ? '-' : '+').str_pad(abs($hours), 2, '0', STR_PAD_LEFT).":$minutes";
}
/**
* Returns a new CarbonTimeZone object using the offset string instead of region string.
*
* @param DateTimeInterface|null $date
*
* @return CarbonTimeZone
*/
public function toOffsetTimeZone(DateTimeInterface $date = null)
{
return new static($this->toOffsetName($date));
}
/**
* Returns the first region string (such as "America/Toronto") that matches the current timezone.
*
* @see timezone_name_from_abbr native PHP function.
*
* @param DateTimeInterface|null $date
* @param int $isDst
*
* @return string
*/
public function toRegionName(DateTimeInterface $date = null, $isDst = 1)
{
$name = $this->getName();
$firstChar = substr($name, 0, 1);
if ($firstChar !== '+' && $firstChar !== '-') {
return $name;
}
return @timezone_name_from_abbr(null, $this->getOffset($date ?: Carbon::now($this)), $isDst);
}
/**
* Returns a new CarbonTimeZone object using the region string instead of offset string.
*
* @param DateTimeInterface|null $date
*
* @return CarbonTimeZone|false
*/
public function toRegionTimeZone(DateTimeInterface $date = null)
{
$tz = $this->toRegionName($date);
if ($tz === false) {
if (Carbon::isStrictModeEnabled()) {
throw new InvalidArgumentException('Unknown timezone for offset '.$this->getOffset($date ?: Carbon::now($this)).' seconds.');
}
return false;
}
return new static($tz);
}
/**
* Cast to string (get timezone name).
*
* @return string
*/
public function __toString()
{
return $this->getName();
}
/**
* Create a CarbonTimeZone from mixed input.
*
* @param DateTimeZone|string|int|null $object
*
* @return false|static
*/
public static function create($object = null)
{
return static::instance($object);
}
}