1 | <?php |
||
16 | class IcalTimezoneFormatter { |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $localTimezones = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $transitions = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $offsets = []; |
||
32 | |||
33 | /** |
||
34 | * @since 3.0 |
||
35 | */ |
||
36 | 8 | public function __construct() { |
|
39 | |||
40 | /** |
||
41 | * Set a list of local timezones. |
||
42 | * |
||
43 | * @since 3.0 |
||
44 | * |
||
45 | * @param array|string $localTimezones |
||
46 | */ |
||
47 | 7 | public function setLocalTimezones( $localTimezones ) { |
|
61 | |||
62 | /** |
||
63 | * Calculate transitions for each timezone. |
||
64 | * |
||
65 | * @since 3.0 |
||
66 | * |
||
67 | * @param integer $from Timestamp from which transitions are generated. |
||
68 | * @param integer $to Timestamp until which transitions are generated. |
||
69 | * |
||
70 | * @return boolean |
||
71 | */ |
||
72 | 7 | public function calcTransitions( $from = null, $to = null ) { |
|
73 | |||
74 | 7 | if ( $this->localTimezones === [] ) { |
|
75 | 2 | return false; |
|
76 | } |
||
77 | |||
78 | 5 | if ( $from === null || $to === null ) { |
|
79 | return false; |
||
80 | } |
||
81 | |||
82 | 5 | foreach ( $this->localTimezones as $timezone ) { |
|
83 | try { |
||
84 | 5 | $dateTimezone = new DateTimeZone( $timezone ); |
|
85 | } |
||
86 | 1 | catch ( Exception $e ) { |
|
87 | 1 | continue; |
|
88 | } |
||
89 | |||
90 | 5 | $transitions = $dateTimezone->getTransitions( $from, $to ); |
|
91 | |||
92 | 5 | if ( $transitions === false ) { |
|
93 | continue; |
||
94 | } |
||
95 | |||
96 | 5 | $min = 0; |
|
97 | 5 | $max = 1; |
|
98 | |||
99 | 5 | foreach ( $transitions as $i => $transition ) { |
|
100 | 5 | if ( $transition['ts'] < $from ) { |
|
101 | $min = $i; |
||
102 | continue; |
||
103 | } |
||
104 | |||
105 | 5 | if ( $transition['ts'] > $to ) { |
|
106 | $max = $i; |
||
107 | break; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | 5 | $this->offsets[$timezone] = $transitions[max( $min - 1, 0 )]['offset']; |
|
112 | 5 | $this->transitions[$timezone] = array_slice( $transitions, $min, $max - $min ); |
|
113 | } |
||
114 | |||
115 | 5 | return true; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Generate the transitions for a given range, for each timezones, in the |
||
120 | * iCalendar format. |
||
121 | * |
||
122 | * @since 3.0 |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 7 | public function getTransitions() { |
|
172 | |||
173 | /** |
||
174 | * Format an integer offset to '+hhii', where hh are the hours, and ii the |
||
175 | * minutes |
||
176 | * |
||
177 | * @param int $offset |
||
178 | */ |
||
179 | 5 | private function formatTimezoneOffset( $offset ) { |
|
182 | |||
183 | } |
||
184 |
This checks looks for cases where a variable has been assigned to itself.
This assignement can be removed without consequences.