FindFromOffset.php
1 year ago
FindFromTimezoneIdentifier.php
1 year ago
FindFromTimezoneMap.php
1 year ago
GuessFromLicEntry.php
1 year ago
GuessFromMsTzId.php
1 year ago
TimezoneFinder.php
1 year ago
TimezoneGuesser.php
1 year ago
FindFromTimezoneMap.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Sabre\VObject\TimezoneGuesser; |
| 6 | |
| 7 | use DateTimeZone; |
| 8 | |
| 9 | /** |
| 10 | * Some clients add 'X-LIC-LOCATION' with the olson name. |
| 11 | */ |
| 12 | class FindFromTimezoneMap implements TimezoneFinder |
| 13 | { |
| 14 | private $map = []; |
| 15 | |
| 16 | private $patterns = [ |
| 17 | '/^\((UTC|GMT)(\+|\-)[\d]{2}\:[\d]{2}\) (.*)/', |
| 18 | '/^\((UTC|GMT)(\+|\-)[\d]{2}\.[\d]{2}\) (.*)/', |
| 19 | ]; |
| 20 | |
| 21 | public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone |
| 22 | { |
| 23 | // Next, we check if the tzid is somewhere in our tzid map. |
| 24 | if ($this->hasTzInMap($tzid)) { |
| 25 | return new DateTimeZone($this->getTzFromMap($tzid)); |
| 26 | } |
| 27 | |
| 28 | // Some Microsoft products prefix the offset first, so let's strip that off |
| 29 | // and see if it is our tzid map. We don't want to check for this first just |
| 30 | // in case there are overrides in our tzid map. |
| 31 | foreach ($this->patterns as $pattern) { |
| 32 | if (!preg_match($pattern, $tzid, $matches)) { |
| 33 | continue; |
| 34 | } |
| 35 | $tzidAlternate = $matches[3]; |
| 36 | if ($this->hasTzInMap($tzidAlternate)) { |
| 37 | return new DateTimeZone($this->getTzFromMap($tzidAlternate)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * This method returns an array of timezone identifiers, that are supported |
| 46 | * by DateTimeZone(), but not returned by DateTimeZone::listIdentifiers(). |
| 47 | * |
| 48 | * We're not using DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC) because: |
| 49 | * - It's not supported by some PHP versions as well as HHVM. |
| 50 | * - It also returns identifiers, that are invalid values for new DateTimeZone() on some PHP versions. |
| 51 | * (See timezonedata/php-bc.php and timezonedata php-workaround.php) |
| 52 | * |
| 53 | * @return array |
| 54 | */ |
| 55 | private function getTzMaps() |
| 56 | { |
| 57 | if ([] === $this->map) { |
| 58 | $this->map = array_merge( |
| 59 | include __DIR__.'/../timezonedata/windowszones.php', |
| 60 | include __DIR__.'/../timezonedata/lotuszones.php', |
| 61 | include __DIR__.'/../timezonedata/exchangezones.php', |
| 62 | include __DIR__.'/../timezonedata/php-workaround.php' |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | return $this->map; |
| 67 | } |
| 68 | |
| 69 | private function getTzFromMap(string $tzid): string |
| 70 | { |
| 71 | return $this->getTzMaps()[$tzid]; |
| 72 | } |
| 73 | |
| 74 | private function hasTzInMap(string $tzid): bool |
| 75 | { |
| 76 | return isset($this->getTzMaps()[$tzid]); |
| 77 | } |
| 78 | } |
| 79 |