Completed
Push — develop ( 268fc1...a06731 )
by Adrien
23:59
created

TimeZone::getTimezoneTransitions()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 10
cp 0
crap 30
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 * @category   PhpSpreadsheet
23
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 */
26
class TimeZone
27
{
28
    /*
29
     * Default Timezone used for date/time conversions
30
     *
31
     * @private
32
     * @var    string
33
     */
34
    protected static $timezone = 'UTC';
35
36
    /**
37
     * Validate a Timezone name
38
     *
39
     * @param     string        $timezone            Time zone (e.g. 'Europe/London')
40
     * @return     bool                        Success or failure
41
     */
42 2
    private static function validateTimeZone($timezone)
43
    {
44 2
        if (in_array($timezone, \DateTimeZone::listIdentifiers())) {
45 1
            return true;
46
        }
47
48 1
        return false;
49
    }
50
51
    /**
52
     * Set the Default Timezone used for date/time conversions
53
     *
54
     * @param     string        $timezone            Time zone (e.g. 'Europe/London')
55
     * @return     bool                        Success or failure
56
     */
57 2
    public static function setTimeZone($timezone)
58
    {
59 2
        if (self::validateTimezone($timezone)) {
60 1
            self::$timezone = $timezone;
61
62 1
            return true;
63
        }
64
65 1
        return false;
66
    }
67
68
    /**
69
     * Return the Default Timezone used for date/time conversions
70
     *
71
     * @return     string        Timezone (e.g. 'Europe/London')
72
     */
73
    public static function getTimeZone()
74
    {
75
        return self::$timezone;
76
    }
77
78
    /**
79
     *    Return the Timezone offset used for date/time conversions to/from UST
80
     *    This requires both the timezone and the calculated date/time to allow for local DST
81
     *
82
     *    @param    string             $timezone         The timezone for finding the adjustment to UST
83
     *    @param    int            $timestamp        PHP date/time value
84
     *    @throws   \PhpOffice\PhpSpreadsheet\Exception
85
     *    @return   int            Number of seconds for timezone adjustment
86
     */
87
    public static function getTimeZoneAdjustment($timezone, $timestamp)
88
    {
89
        if ($timezone !== null) {
90
            if (!self::validateTimezone($timezone)) {
91
                throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid timezone ' . $timezone);
92
            }
93
        } else {
94
            $timezone = self::$timezone;
95
        }
96
97
        if ($timezone == 'UST') {
98
            return 0;
99
        }
100
101
        $objTimezone = new \DateTimeZone($timezone);
102
        $transitions = $objTimezone->getTransitions($timestamp, $timestamp);
103
104
        return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
105
    }
106
}
107