Completed
Pull Request — master (#2)
by Andreas
02:06
created

Timezone::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (c) DateTime-Contributors
5
 *
6
 * Licensed under the MIT License. See LICENSE.md file in the project root
7
 * for full license information.
8
 */
9
10
namespace DateTime\Timezone;
11
12
use Closure;
13
use DateTime\DateTime;
14
use DateTimeZone;
15
16
abstract class Timezone
17
{
18
    const AFRICA = 1 ;
19
    const AMERICA = 2 ;
20
    const ANTARCTICA = 4 ;
21
    const ARCTIC = 8 ;
22
    const ASIA = 16 ;
23
    const ATLANTIC = 32 ;
24
    const AUSTRALIA = 64 ;
25
    const EUROPE = 128 ;
26
    const INDIAN = 256 ;
27
    const PACIFIC = 512 ;
28
    const UTC = 1024 ;
29
    const ALL = 2047 ;
30
    const ALL_WITH_BC = 4095 ;
31
    const PER_COUNTRY = 4096 ;
32
33
    const TYPE_TIMEZONE = 3;
34
    const TYPE_ABBREVIATION = 2;
35
    const TYPE_OFFSET = 1;
36
37
    protected $timezone;
38
39
    private function __construct(DateTimeZone $timezone)
40
    {
41
        $this->timezone = $timezone;
42
    }
43
44
    public function getType() : int
45
    {
46
        if (false !== $this->timezone->getLocation()) {
47
            return self::TYPE_TIMEZONE;
48
        }
49
        if (false !== strpos($this->getName(), ':')) {
50
            return self::TYPE_OFFSET;
51
        }
52
53
        return self::TYPE_ABBREVIATION;
54
    }
55
56
    public function getName() : string
57
    {
58
        return $this->timezone->getName();
59
    }
60
61
    public function getOffset(DateTime $datetime) : int
62
    {
63
        $datetimeThief = function (DateTime $datetime) {
64
            return $datetime->datetime;
0 ignored issues
show
Bug introduced by
The property datetime is declared private in DateTime\DateTime and cannot be accessed from this context.
Loading history...
65
        };
66
        $datetimeThief = Closure::bind($datetimeThief, null, $datetime);
67
        $datetime       = $datetimeThief($datetime);
68
69
        return $this->timezone->getOffset($datetime);
70
    }
71
72
    public function getTransitions(int $timestamp_begin = null, int $timestamp_end = null) : array
73
    {
74
        return $this->timezone->getTransitions($timestamp_begin, $timestamp_end);
75
    }
76
77
    public static function listAbbreviations() : array
78
    {
79
        return DateTimeZone::listAbbreviations();
80
    }
81
82
    public static function listIdentifiers(int $what = self::ALL, string $country = null) : array
83
    {
84
        return DateTimeZone::listIdentifiers($what, $country);
85
    }
86
87
    public static function fromString(string $timezone) : self
88
    {
89
        $timezone = new DateTimeZone($timezone);
90
91
        if (false !== $timezone->getLocation()) {
92
            return new Name($timezone);
93
        }
94
        if (false !== strpos($timezone->getName(), ':')) {
95
            return new Offset($timezone);
96
        }
97
98
        return new Abbreviation($timezone);
99
    }
100
}
101