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
FindFromTimezoneIdentifier.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Sabre\VObject\TimezoneGuesser; |
| 6 | |
| 7 | use DateTimeZone; |
| 8 | use Exception; |
| 9 | |
| 10 | /** |
| 11 | * Some clients add 'X-LIC-LOCATION' with the olson name. |
| 12 | */ |
| 13 | class FindFromTimezoneIdentifier implements TimezoneFinder |
| 14 | { |
| 15 | public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone |
| 16 | { |
| 17 | // First we will just see if the tzid is a support timezone identifier. |
| 18 | // |
| 19 | // The only exception is if the timezone starts with (. This is to |
| 20 | // handle cases where certain microsoft products generate timezone |
| 21 | // identifiers that for instance look like: |
| 22 | // |
| 23 | // (GMT+01.00) Sarajevo/Warsaw/Zagreb |
| 24 | // |
| 25 | // Since PHP 5.5.10, the first bit will be used as the timezone and |
| 26 | // this method will return just GMT+01:00. This is wrong, because it |
| 27 | // doesn't take DST into account |
| 28 | if (!isset($tzid[0])) { |
| 29 | return null; |
| 30 | } |
| 31 | if ('(' === $tzid[0]) { |
| 32 | return null; |
| 33 | } |
| 34 | // PHP has a bug that logs PHP warnings even it shouldn't: |
| 35 | // https://bugs.php.net/bug.php?id=67881 |
| 36 | // |
| 37 | // That's why we're checking if we'll be able to successfully instantiate |
| 38 | // \DateTimeZone() before doing so. Otherwise we could simply instantiate |
| 39 | // and catch the exception. |
| 40 | $tzIdentifiers = DateTimeZone::listIdentifiers(); |
| 41 | |
| 42 | try { |
| 43 | if ( |
| 44 | (in_array($tzid, $tzIdentifiers)) || |
| 45 | (preg_match('/^GMT(\+|-)([0-9]{4})$/', $tzid, $matches)) || |
| 46 | (in_array($tzid, $this->getIdentifiersBC())) |
| 47 | ) { |
| 48 | return new DateTimeZone($tzid); |
| 49 | } |
| 50 | } catch (Exception $e) { |
| 51 | } |
| 52 | |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * This method returns an array of timezone identifiers, that are supported |
| 58 | * by DateTimeZone(), but not returned by DateTimeZone::listIdentifiers(). |
| 59 | * |
| 60 | * We're not using DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC) because: |
| 61 | * - It's not supported by some PHP versions as well as HHVM. |
| 62 | * - It also returns identifiers, that are invalid values for new DateTimeZone() on some PHP versions. |
| 63 | * (See timezonedata/php-bc.php and timezonedata php-workaround.php) |
| 64 | * |
| 65 | * @return array |
| 66 | */ |
| 67 | private function getIdentifiersBC() |
| 68 | { |
| 69 | return include __DIR__.'/../timezonedata/php-bc.php'; |
| 70 | } |
| 71 | } |
| 72 |