|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SRF\iCalendar; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeZone; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Create the iCalendar's vTimezone component |
|
10
|
|
|
* |
|
11
|
|
|
* @license GNU GPL v2+ |
|
12
|
|
|
* @since 3.0 |
|
13
|
|
|
* |
|
14
|
|
|
* @author HgO |
|
15
|
|
|
*/ |
|
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
|
|
|
public function __construct() { |
|
|
|
|
|
|
37
|
|
|
$this->localTimezones = [ $GLOBALS['wgLocaltimezone'] ]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set a list of local timezones. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 3.0 |
|
44
|
|
|
* |
|
45
|
|
|
* @param array|string $localTimezones |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setLocalTimezones( $localTimezones ) { |
|
48
|
|
|
|
|
49
|
|
|
if ( is_array( $localTimezones ) ) { |
|
50
|
|
|
$localTimezones = $localTimezones; |
|
|
|
|
|
|
51
|
|
|
} elseif ( strpos( $localTimezones, ',' ) !== false ) { |
|
52
|
|
|
$localTimezones = explode( ',', $localTimezones ); |
|
53
|
|
|
} elseif ( $localTimezones !== '' ) { |
|
54
|
|
|
$localTimezones = [ $localTimezones ]; |
|
55
|
|
|
} else { |
|
56
|
|
|
$localTimezones = []; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->localTimezones = $localTimezones; |
|
60
|
|
|
} |
|
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
|
|
|
public function calcTransitions( $from = null, $to = null ) { |
|
73
|
|
|
|
|
74
|
|
|
if ( $this->localTimezones === [] ) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ( $from === null || $to === null ){ |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
foreach ( $this->localTimezones as $timezone ) { |
|
83
|
|
|
try { |
|
84
|
|
|
$dateTimezone = new DateTimeZone( $timezone ); |
|
85
|
|
|
} catch( Exception $e ) { |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$transitions = $dateTimezone->getTransitions(); |
|
90
|
|
|
|
|
91
|
|
|
if ( $transitions === false ) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$min = 0; |
|
96
|
|
|
$max = 1; |
|
97
|
|
|
|
|
98
|
|
|
foreach ( $transitions as $i => $transition ) { |
|
99
|
|
|
if ( $transition['ts'] < $from ) { |
|
100
|
|
|
$min = $i; |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ( $transition['ts'] > $to ) { |
|
105
|
|
|
$max = $i; |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->offsets[$timezone] = $transitions[max( $min-1, 0 )]['offset']; |
|
111
|
|
|
$this->transitions[$timezone] = array_slice( $transitions, $min, $max - $min ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Generate the transitions for a given range, for each timezones, in the |
|
119
|
|
|
* iCalendar format. |
|
120
|
|
|
* |
|
121
|
|
|
* @since 3.0 |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getTransitions() { |
|
126
|
|
|
|
|
127
|
|
|
$result = ''; |
|
128
|
|
|
|
|
129
|
|
|
if ( $this->transitions === null || $this->transitions === [] ) { |
|
130
|
|
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach ( $this->transitions as $timezone => $transitions ) { |
|
134
|
|
|
// cf. http://www.kanzaki.com/docs/ical/vtimezone.html |
|
135
|
|
|
$result .= "BEGIN:VTIMEZONE\r\n"; |
|
136
|
|
|
$result .= "TZID:$timezone\r\n"; |
|
137
|
|
|
|
|
138
|
|
|
$tzfrom = $this->offsets[$timezone] / 3600; |
|
139
|
|
|
foreach ( $transitions as $transition ) { |
|
140
|
|
|
$dst = ( $transition['isdst'] ) ? "DAYLIGHT" : "STANDARD"; |
|
141
|
|
|
$result .= "BEGIN:$dst\r\n"; |
|
142
|
|
|
|
|
143
|
|
|
$start_date = date( 'Ymd\THis', $transition['ts'] ); |
|
144
|
|
|
$result .= "DTSTART:$start_date\r\n"; |
|
145
|
|
|
|
|
146
|
|
|
$offset = $transition['offset'] / 3600; |
|
147
|
|
|
|
|
148
|
|
|
$offset_from = $this->formatTimezoneOffset( $tzfrom ); |
|
149
|
|
|
$result .= "TZOFFSETFROM:$offset_from\r\n"; |
|
150
|
|
|
|
|
151
|
|
|
$offset_to = $this->formatTimezoneOffset( $offset ); |
|
152
|
|
|
$result .= "TZOFFSETTO:$offset_to\r\n"; |
|
153
|
|
|
|
|
154
|
|
|
if ( !empty( $transition['abbr'] ) ) |
|
155
|
|
|
$result .= "TZNAME:{$transition['abbr']}\r\n"; |
|
156
|
|
|
|
|
157
|
|
|
$result .= "END:$dst\r\n"; |
|
158
|
|
|
|
|
159
|
|
|
$tzfrom = $offset; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$result .= "END:VTIMEZONE\r\n"; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// Clear the calculation |
|
166
|
|
|
$this->transitions = []; |
|
167
|
|
|
|
|
168
|
|
|
return $result; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Format an integer offset to '+hhii', where hh are the hours, and ii the |
|
173
|
|
|
* minutes |
|
174
|
|
|
* |
|
175
|
|
|
* @param int $offset |
|
176
|
|
|
*/ |
|
177
|
|
|
private function formatTimezoneOffset( $offset ) { |
|
178
|
|
|
return sprintf('%s%02d%02d', $offset >= 0 ? '+' : '', floor($offset), ($offset - floor($offset)) * 60); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: