1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,210 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-/** |
|
4 |
- * This file is part of the Carbon package. |
|
5 |
- * |
|
6 |
- * (c) Brian Nesbitt <brian@nesbot.com> |
|
7 |
- * |
|
8 |
- * For the full copyright and license information, please view the LICENSE |
|
9 |
- * file that was distributed with this source code. |
|
10 |
- */ |
|
11 |
-namespace Carbon; |
|
12 |
- |
|
13 |
-use DateTimeInterface; |
|
14 |
-use DateTimeZone; |
|
15 |
-use InvalidArgumentException; |
|
16 |
- |
|
17 |
-class CarbonTimeZone extends DateTimeZone |
|
18 |
-{ |
|
19 |
- public function __construct($timezone = null) |
|
20 |
- { |
|
21 |
- parent::__construct(static::getDateTimeZoneNameFromMixed($timezone)); |
|
22 |
- } |
|
23 |
- |
|
24 |
- protected static function getDateTimeZoneNameFromMixed($timezone) |
|
25 |
- { |
|
26 |
- if (is_null($timezone)) { |
|
27 |
- $timezone = date_default_timezone_get(); |
|
28 |
- } elseif (is_numeric($timezone)) { |
|
29 |
- if ($timezone <= -100 || $timezone >= 100) { |
|
30 |
- throw new InvalidArgumentException('Absolute timezone offset cannot be greater than 100.'); |
|
31 |
- } |
|
32 |
- |
|
33 |
- $timezone = ($timezone >= 0 ? '+' : '').$timezone.':00'; |
|
34 |
- } |
|
35 |
- |
|
36 |
- return $timezone; |
|
37 |
- } |
|
38 |
- |
|
39 |
- protected static function getDateTimeZoneFromName(&$name) |
|
40 |
- { |
|
41 |
- return @timezone_open($name = (string) static::getDateTimeZoneNameFromMixed($name)); |
|
42 |
- } |
|
43 |
- |
|
44 |
- /** |
|
45 |
- * Create a CarbonTimeZone from mixed input. |
|
46 |
- * |
|
47 |
- * @param DateTimeZone|string|int|null $object original value to get CarbonTimeZone from it. |
|
48 |
- * @param DateTimeZone|string|int|null $objectDump dump of the object for error messages. |
|
49 |
- * |
|
50 |
- * @return false|static |
|
51 |
- */ |
|
52 |
- public static function instance($object = null, $objectDump = null) |
|
53 |
- { |
|
54 |
- $tz = $object; |
|
55 |
- |
|
56 |
- if ($tz instanceof static) { |
|
57 |
- return $tz; |
|
58 |
- } |
|
59 |
- |
|
60 |
- if ($tz === null) { |
|
61 |
- return new static(); |
|
62 |
- } |
|
63 |
- |
|
64 |
- if (!$tz instanceof DateTimeZone) { |
|
65 |
- $tz = static::getDateTimeZoneFromName($object); |
|
66 |
- } |
|
67 |
- |
|
68 |
- if ($tz === false) { |
|
69 |
- if (Carbon::isStrictModeEnabled()) { |
|
70 |
- throw new InvalidArgumentException('Unknown or bad timezone ('.($objectDump ?: $object).')'); |
|
71 |
- } |
|
72 |
- |
|
73 |
- return false; |
|
74 |
- } |
|
75 |
- |
|
76 |
- return new static($tz->getName()); |
|
77 |
- } |
|
78 |
- |
|
79 |
- /** |
|
80 |
- * Returns abbreviated name of the current timezone according to DST setting. |
|
81 |
- * |
|
82 |
- * @param bool $dst |
|
83 |
- * |
|
84 |
- * @return string |
|
85 |
- */ |
|
86 |
- public function getAbbreviatedName($dst = false) |
|
87 |
- { |
|
88 |
- $name = $this->getName(); |
|
89 |
- |
|
90 |
- foreach ($this->listAbbreviations() as $abbreviation => $zones) { |
|
91 |
- foreach ($zones as $zone) { |
|
92 |
- if ($zone['timezone_id'] === $name && $zone['dst'] == $dst) { |
|
93 |
- return $abbreviation; |
|
94 |
- } |
|
95 |
- } |
|
96 |
- } |
|
97 |
- |
|
98 |
- return 'unknown'; |
|
99 |
- } |
|
100 |
- |
|
101 |
- /** |
|
102 |
- * @alias getAbbreviatedName |
|
103 |
- * |
|
104 |
- * Returns abbreviated name of the current timezone according to DST setting. |
|
105 |
- * |
|
106 |
- * @param bool $dst |
|
107 |
- * |
|
108 |
- * @return string |
|
109 |
- */ |
|
110 |
- public function getAbbr($dst = false) |
|
111 |
- { |
|
112 |
- return $this->getAbbreviatedName($dst); |
|
113 |
- } |
|
114 |
- |
|
115 |
- /** |
|
116 |
- * Get the offset as string "sHH:MM" (such as "+00:00" or "-12:30"). |
|
117 |
- * |
|
118 |
- * @param DateTimeInterface|null $date |
|
119 |
- * |
|
120 |
- * @return string |
|
121 |
- */ |
|
122 |
- public function toOffsetName(DateTimeInterface $date = null) |
|
123 |
- { |
|
124 |
- $minutes = floor($this->getOffset($date ?: Carbon::now($this)) / 60); |
|
125 |
- |
|
126 |
- $hours = floor($minutes / 60); |
|
127 |
- |
|
128 |
- $minutes = str_pad((string) (abs($minutes) % 60), 2, '0', STR_PAD_LEFT); |
|
129 |
- |
|
130 |
- return ($hours < 0 ? '-' : '+').str_pad((string) abs($hours), 2, '0', STR_PAD_LEFT).":$minutes"; |
|
131 |
- } |
|
132 |
- |
|
133 |
- /** |
|
134 |
- * Returns a new CarbonTimeZone object using the offset string instead of region string. |
|
135 |
- * |
|
136 |
- * @param DateTimeInterface|null $date |
|
137 |
- * |
|
138 |
- * @return CarbonTimeZone |
|
139 |
- */ |
|
140 |
- public function toOffsetTimeZone(DateTimeInterface $date = null) |
|
141 |
- { |
|
142 |
- return new static($this->toOffsetName($date)); |
|
143 |
- } |
|
144 |
- |
|
145 |
- /** |
|
146 |
- * Returns the first region string (such as "America/Toronto") that matches the current timezone. |
|
147 |
- * |
|
148 |
- * @see timezone_name_from_abbr native PHP function. |
|
149 |
- * |
|
150 |
- * @param DateTimeInterface|null $date |
|
151 |
- * @param int $isDst |
|
152 |
- * |
|
153 |
- * @return string |
|
154 |
- */ |
|
155 |
- public function toRegionName(DateTimeInterface $date = null, $isDst = 1) |
|
156 |
- { |
|
157 |
- $name = $this->getName(); |
|
158 |
- $firstChar = substr($name, 0, 1); |
|
159 |
- |
|
160 |
- if ($firstChar !== '+' && $firstChar !== '-') { |
|
161 |
- return $name; |
|
162 |
- } |
|
163 |
- |
|
164 |
- return @timezone_name_from_abbr('', @$this->getOffset($date ?: Carbon::now($this)) ?: 0, $isDst); |
|
165 |
- } |
|
166 |
- |
|
167 |
- /** |
|
168 |
- * Returns a new CarbonTimeZone object using the region string instead of offset string. |
|
169 |
- * |
|
170 |
- * @param DateTimeInterface|null $date |
|
171 |
- * |
|
172 |
- * @return CarbonTimeZone|false |
|
173 |
- */ |
|
174 |
- public function toRegionTimeZone(DateTimeInterface $date = null) |
|
175 |
- { |
|
176 |
- $tz = $this->toRegionName($date); |
|
177 |
- |
|
178 |
- if ($tz === false) { |
|
179 |
- if (Carbon::isStrictModeEnabled()) { |
|
180 |
- throw new InvalidArgumentException('Unknown timezone for offset '.$this->getOffset($date ?: Carbon::now($this)).' seconds.'); |
|
181 |
- } |
|
182 |
- |
|
183 |
- return false; |
|
184 |
- } |
|
185 |
- |
|
186 |
- return new static($tz); |
|
187 |
- } |
|
188 |
- |
|
189 |
- /** |
|
190 |
- * Cast to string (get timezone name). |
|
191 |
- * |
|
192 |
- * @return string |
|
193 |
- */ |
|
194 |
- public function __toString() |
|
195 |
- { |
|
196 |
- return $this->getName(); |
|
197 |
- } |
|
198 |
- |
|
199 |
- /** |
|
200 |
- * Create a CarbonTimeZone from mixed input. |
|
201 |
- * |
|
202 |
- * @param DateTimeZone|string|int|null $object |
|
203 |
- * |
|
204 |
- * @return false|static |
|
205 |
- */ |
|
206 |
- public static function create($object = null) |
|
207 |
- { |
|
208 |
- return static::instance($object); |
|
209 |
- } |
|
210 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,210 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+/** |
|
4 |
+ * This file is part of the Carbon package. |
|
5 |
+ * |
|
6 |
+ * (c) Brian Nesbitt <brian@nesbot.com> |
|
7 |
+ * |
|
8 |
+ * For the full copyright and license information, please view the LICENSE |
|
9 |
+ * file that was distributed with this source code. |
|
10 |
+ */ |
|
11 |
+namespace Carbon; |
|
12 |
+ |
|
13 |
+use DateTimeInterface; |
|
14 |
+use DateTimeZone; |
|
15 |
+use InvalidArgumentException; |
|
16 |
+ |
|
17 |
+class CarbonTimeZone extends DateTimeZone |
|
18 |
+{ |
|
19 |
+ public function __construct($timezone = null) |
|
20 |
+ { |
|
21 |
+ parent::__construct(static::getDateTimeZoneNameFromMixed($timezone)); |
|
22 |
+ } |
|
23 |
+ |
|
24 |
+ protected static function getDateTimeZoneNameFromMixed($timezone) |
|
25 |
+ { |
|
26 |
+ if (is_null($timezone)) { |
|
27 |
+ $timezone = date_default_timezone_get(); |
|
28 |
+ } elseif (is_numeric($timezone)) { |
|
29 |
+ if ($timezone <= -100 || $timezone >= 100) { |
|
30 |
+ throw new InvalidArgumentException('Absolute timezone offset cannot be greater than 100.'); |
|
31 |
+ } |
|
32 |
+ |
|
33 |
+ $timezone = ($timezone >= 0 ? '+' : '').$timezone.':00'; |
|
34 |
+ } |
|
35 |
+ |
|
36 |
+ return $timezone; |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ protected static function getDateTimeZoneFromName(&$name) |
|
40 |
+ { |
|
41 |
+ return @timezone_open($name = (string) static::getDateTimeZoneNameFromMixed($name)); |
|
42 |
+ } |
|
43 |
+ |
|
44 |
+ /** |
|
45 |
+ * Create a CarbonTimeZone from mixed input. |
|
46 |
+ * |
|
47 |
+ * @param DateTimeZone|string|int|null $object original value to get CarbonTimeZone from it. |
|
48 |
+ * @param DateTimeZone|string|int|null $objectDump dump of the object for error messages. |
|
49 |
+ * |
|
50 |
+ * @return false|static |
|
51 |
+ */ |
|
52 |
+ public static function instance($object = null, $objectDump = null) |
|
53 |
+ { |
|
54 |
+ $tz = $object; |
|
55 |
+ |
|
56 |
+ if ($tz instanceof static) { |
|
57 |
+ return $tz; |
|
58 |
+ } |
|
59 |
+ |
|
60 |
+ if ($tz === null) { |
|
61 |
+ return new static(); |
|
62 |
+ } |
|
63 |
+ |
|
64 |
+ if (!$tz instanceof DateTimeZone) { |
|
65 |
+ $tz = static::getDateTimeZoneFromName($object); |
|
66 |
+ } |
|
67 |
+ |
|
68 |
+ if ($tz === false) { |
|
69 |
+ if (Carbon::isStrictModeEnabled()) { |
|
70 |
+ throw new InvalidArgumentException('Unknown or bad timezone ('.($objectDump ?: $object).')'); |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ return false; |
|
74 |
+ } |
|
75 |
+ |
|
76 |
+ return new static($tz->getName()); |
|
77 |
+ } |
|
78 |
+ |
|
79 |
+ /** |
|
80 |
+ * Returns abbreviated name of the current timezone according to DST setting. |
|
81 |
+ * |
|
82 |
+ * @param bool $dst |
|
83 |
+ * |
|
84 |
+ * @return string |
|
85 |
+ */ |
|
86 |
+ public function getAbbreviatedName($dst = false) |
|
87 |
+ { |
|
88 |
+ $name = $this->getName(); |
|
89 |
+ |
|
90 |
+ foreach ($this->listAbbreviations() as $abbreviation => $zones) { |
|
91 |
+ foreach ($zones as $zone) { |
|
92 |
+ if ($zone['timezone_id'] === $name && $zone['dst'] == $dst) { |
|
93 |
+ return $abbreviation; |
|
94 |
+ } |
|
95 |
+ } |
|
96 |
+ } |
|
97 |
+ |
|
98 |
+ return 'unknown'; |
|
99 |
+ } |
|
100 |
+ |
|
101 |
+ /** |
|
102 |
+ * @alias getAbbreviatedName |
|
103 |
+ * |
|
104 |
+ * Returns abbreviated name of the current timezone according to DST setting. |
|
105 |
+ * |
|
106 |
+ * @param bool $dst |
|
107 |
+ * |
|
108 |
+ * @return string |
|
109 |
+ */ |
|
110 |
+ public function getAbbr($dst = false) |
|
111 |
+ { |
|
112 |
+ return $this->getAbbreviatedName($dst); |
|
113 |
+ } |
|
114 |
+ |
|
115 |
+ /** |
|
116 |
+ * Get the offset as string "sHH:MM" (such as "+00:00" or "-12:30"). |
|
117 |
+ * |
|
118 |
+ * @param DateTimeInterface|null $date |
|
119 |
+ * |
|
120 |
+ * @return string |
|
121 |
+ */ |
|
122 |
+ public function toOffsetName(DateTimeInterface $date = null) |
|
123 |
+ { |
|
124 |
+ $minutes = floor($this->getOffset($date ?: Carbon::now($this)) / 60); |
|
125 |
+ |
|
126 |
+ $hours = floor($minutes / 60); |
|
127 |
+ |
|
128 |
+ $minutes = str_pad((string) (abs($minutes) % 60), 2, '0', STR_PAD_LEFT); |
|
129 |
+ |
|
130 |
+ return ($hours < 0 ? '-' : '+').str_pad((string) abs($hours), 2, '0', STR_PAD_LEFT).":$minutes"; |
|
131 |
+ } |
|
132 |
+ |
|
133 |
+ /** |
|
134 |
+ * Returns a new CarbonTimeZone object using the offset string instead of region string. |
|
135 |
+ * |
|
136 |
+ * @param DateTimeInterface|null $date |
|
137 |
+ * |
|
138 |
+ * @return CarbonTimeZone |
|
139 |
+ */ |
|
140 |
+ public function toOffsetTimeZone(DateTimeInterface $date = null) |
|
141 |
+ { |
|
142 |
+ return new static($this->toOffsetName($date)); |
|
143 |
+ } |
|
144 |
+ |
|
145 |
+ /** |
|
146 |
+ * Returns the first region string (such as "America/Toronto") that matches the current timezone. |
|
147 |
+ * |
|
148 |
+ * @see timezone_name_from_abbr native PHP function. |
|
149 |
+ * |
|
150 |
+ * @param DateTimeInterface|null $date |
|
151 |
+ * @param int $isDst |
|
152 |
+ * |
|
153 |
+ * @return string |
|
154 |
+ */ |
|
155 |
+ public function toRegionName(DateTimeInterface $date = null, $isDst = 1) |
|
156 |
+ { |
|
157 |
+ $name = $this->getName(); |
|
158 |
+ $firstChar = substr($name, 0, 1); |
|
159 |
+ |
|
160 |
+ if ($firstChar !== '+' && $firstChar !== '-') { |
|
161 |
+ return $name; |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ return @timezone_name_from_abbr('', @$this->getOffset($date ?: Carbon::now($this)) ?: 0, $isDst); |
|
165 |
+ } |
|
166 |
+ |
|
167 |
+ /** |
|
168 |
+ * Returns a new CarbonTimeZone object using the region string instead of offset string. |
|
169 |
+ * |
|
170 |
+ * @param DateTimeInterface|null $date |
|
171 |
+ * |
|
172 |
+ * @return CarbonTimeZone|false |
|
173 |
+ */ |
|
174 |
+ public function toRegionTimeZone(DateTimeInterface $date = null) |
|
175 |
+ { |
|
176 |
+ $tz = $this->toRegionName($date); |
|
177 |
+ |
|
178 |
+ if ($tz === false) { |
|
179 |
+ if (Carbon::isStrictModeEnabled()) { |
|
180 |
+ throw new InvalidArgumentException('Unknown timezone for offset '.$this->getOffset($date ?: Carbon::now($this)).' seconds.'); |
|
181 |
+ } |
|
182 |
+ |
|
183 |
+ return false; |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ return new static($tz); |
|
187 |
+ } |
|
188 |
+ |
|
189 |
+ /** |
|
190 |
+ * Cast to string (get timezone name). |
|
191 |
+ * |
|
192 |
+ * @return string |
|
193 |
+ */ |
|
194 |
+ public function __toString() |
|
195 |
+ { |
|
196 |
+ return $this->getName(); |
|
197 |
+ } |
|
198 |
+ |
|
199 |
+ /** |
|
200 |
+ * Create a CarbonTimeZone from mixed input. |
|
201 |
+ * |
|
202 |
+ * @param DateTimeZone|string|int|null $object |
|
203 |
+ * |
|
204 |
+ * @return false|static |
|
205 |
+ */ |
|
206 |
+ public static function create($object = null) |
|
207 |
+ { |
|
208 |
+ return static::instance($object); |
|
209 |
+ } |
|
210 |
+} |