aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/vobject/lib/TimezoneGuesser/FindFromTimezoneIdentifier.php
blob: d8c6d5d3b16f1029049bb6e61ef738d4ab67bd54 (plain) (blame)
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
<?php

declare(strict_types=1);

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;
use Exception;

/**
 * Some clients add 'X-LIC-LOCATION' with the olson name.
 */
class FindFromTimezoneIdentifier implements TimezoneFinder
{
    public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
    {
        // First we will just see if the tzid is a support timezone identifier.
        //
        // The only exception is if the timezone starts with (. This is to
        // handle cases where certain microsoft products generate timezone
        // identifiers that for instance look like:
        //
        // (GMT+01.00) Sarajevo/Warsaw/Zagreb
        //
        // Since PHP 5.5.10, the first bit will be used as the timezone and
        // this method will return just GMT+01:00. This is wrong, because it
        // doesn't take DST into account
        if (!isset($tzid[0])) {
            return null;
        }
        if ('(' === $tzid[0]) {
            return null;
        }
        // PHP has a bug that logs PHP warnings even it shouldn't:
        // https://bugs.php.net/bug.php?id=67881
        //
        // That's why we're checking if we'll be able to successfully instantiate
        // \DateTimeZone() before doing so. Otherwise we could simply instantiate
        // and catch the exception.
        $tzIdentifiers = DateTimeZone::listIdentifiers();

        try {
            if (
                (in_array($tzid, $tzIdentifiers)) ||
                (preg_match('/^GMT(\+|-)([0-9]{4})$/', $tzid, $matches)) ||
                (in_array($tzid, $this->getIdentifiersBC()))
            ) {
                return new DateTimeZone($tzid);
            }
        } catch (Exception $e) {
        }

        return null;
    }

    /**
     * This method returns an array of timezone identifiers, that are supported
     * by DateTimeZone(), but not returned by DateTimeZone::listIdentifiers().
     *
     * We're not using DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC) because:
     * - It's not supported by some PHP versions as well as HHVM.
     * - It also returns identifiers, that are invalid values for new DateTimeZone() on some PHP versions.
     * (See timezonedata/php-bc.php and timezonedata php-workaround.php)
     *
     * @return array
     */
    private function getIdentifiersBC()
    {
        return include __DIR__.'/../timezonedata/php-bc.php';
    }
}