/
Rule.php
281 lines (275 loc) · 11.2 KB
/
Rule.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Class representing a set of "Rule" timezone database entries of the
* same name.
*
* Copyright 2011-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @package Timezone
*/
class Horde_Timezone_Rule
{
/**
* A ruleset name.
*
* @var string
*/
protected $_name;
/**
* All Rule lines for this ruleset.
*
* @var array
*/
protected $_rules = array();
/**
* List to map weekday descriptions used in the timezone database.
*
* @var array
*/
protected $_weekdays = array('Mon' => Horde_Date::DATE_MONDAY,
'Tue' => Horde_Date::DATE_TUESDAY,
'Wed' => Horde_Date::DATE_WEDNESDAY,
'Thu' => Horde_Date::DATE_THURSDAY,
'Fri' => Horde_Date::DATE_FRIDAY,
'Sat' => Horde_Date::DATE_SATURDAY,
'Sun' => Horde_Date::DATE_SUNDAY);
/**
* Constructor.
*
* @param string $name A ruleset name.
*/
public function __construct($name)
{
$this->_name = $name;
}
/**
* Adds a Rule line to this ruleset.
*
* @param array $rule A parsed Rule line.
*/
public function add($rule)
{
$this->_rules[] = $rule;
}
/**
* Adds rules from this ruleset to a VTIMEZONE component.
*
* @param Horde_Icalendar_Vtimezone $tz A VTIMEZONE component.
* @param string $tzid The timezone ID of the component.
* @param string $name A timezone name abbreviation.
* May contain a placeholder that is
* replaced the Rules' "Letter(s)"
* entry.
* @param array $startOffset An offset hash describing the
* base offset of a timezone.
* @param Horde_Date $start Start of the period to add rules
* for.
* @param Horde_Date $end End of the period to add rules
* for.
*/
public function addRules(Horde_Icalendar_Vtimezone $tz, $tzid, $name,
$startOffset,
Horde_Date $start, Horde_Date $end = null)
{
$offset = $startOffset;
foreach ($this->_rules as $rule) {
$year = $rule[3];
if ($year[0] == 'o') {
// TO is "only"
$rule[3] = $rule[2];
}
if ($rule[3][0] != 'm' && $rule[3] < $start->year) {
// TO is not maximum and is before the searched period
continue;
}
if ($end &&
$rule[2][0] != 'm' && $rule[2] > $end->year) {
// FROM is not "minimum" and is after the searched period
break;
}
if ($rule[2][0] != 'm' && $rule[2] < $start->year) {
$rule[2] = $start->year;
}
if ($rule[8] == 0) {
$component = new Horde_Icalendar_Standard();
$component->setAttribute('TZOFFSETFROM', $offset);
$component->setAttribute('TZOFFSETTO', $startOffset);
$offset = $startOffset;
} else {
$component = new Horde_Icalendar_Daylight();
$component->setAttribute('TZOFFSETFROM', $offset);
$offset = $this->_getOffset($startOffset, $rule[8]);
$component->setAttribute('TZOFFSETTO', $offset);
}
$month = Horde_Timezone::getMonth($rule[5]);
// Retrieve time of rule start.
preg_match('/(\d+)(?::(\d+))?(?::(\d+))?(w|s|u)?/', $rule[7], $match);
if (!isset($match[2])) {
$match[2] = 0;
}
if ($rule[2] == $rule[3] && preg_match('/^\d+$/', $rule[6])) {
// Rule lasts only for a single year and starts on a specific
// date.
$rdate = new Horde_Date(
array('year' => $rule[2],
'month' => Horde_Timezone::getMonth($rule[5]),
'mday' => $rule[6],
'hour' => $match[1],
'min' => $match[2],
'sec' => 0));
$component->setAttribute('DTSTART', $rdate);
} elseif (substr($rule[6], 0, 4) == 'last') {
// Rule starts on the last of a certain weekday of the month.
$weekday = $this->_weekdays[substr($rule[6], 4, 3)];
$last = new Horde_Date(array(
'year' => $rule[2],
'month' => $month,
'mday' => Horde_Date_Utils::daysInMonth($month, $rule[2]),
'hour' => $match[1],
'min' => $match[2],
'sec' => 0
));
while ($last->dayOfWeek() != $weekday) {
$last->mday--;
}
$component->setAttribute('DTSTART', $last);
if ($rule[3][0] == 'm') {
$until = '';
} else {
$last = new Horde_Date(
array('year' => $rule[3],
'month' => $month,
'mday' => Horde_Date_Utils::daysInMonth($month, $rule[2]),
'hour' => $match[1],
'min' => $match[2],
'sec' => 0),
$tzid);
while ($last->dayOfWeek() != $weekday) {
$last->mday--;
}
$last->setTimezone('UTC');
$until = ';UNTIL=' . $last->format('Ymd\THIs') . 'Z';
}
$component->setAttribute(
'RRULE',
'FREQ=YEARLY;BYDAY=-1'
. Horde_String::upper(substr($rule[6], 4, 2))
. ';BYMONTH=' . $month . $until);
} elseif (strpos($rule[6], '>=')) {
// Rule starts on a certain weekday after a certain day of
// month.
list($weekday, $day) = explode('>=', $rule[6]);
$weekdayInt = $this->_weekdays[substr($weekday, 0, 3)];
$first = new Horde_Date(array(
'year' => $rule[2],
'month' => $month,
'mday' => $day,
'hour' => $match[1],
'min' => $match[2],
'sec' => 0
));
while ($first->dayOfWeek() != $weekdayInt) {
$first->mday++;
}
$component->setAttribute('DTSTART', $first);
if ($rule[3][0] == 'm') {
$until = '';
} else {
$last = new Horde_Date(
array('year' => $rule[3],
'month' => $month,
'mday' => $day,
'hour' => $match[1],
'min' => $match[2],
'sec' => 0),
$tzid);
while ($last->dayOfWeek() != $weekday) {
$last->mday++;
}
$last->setTimezone('UTC');
$until = ';UNTIL=' . $last->format('Ymd\THIs') . 'Z';
}
for ($days = array(), $i = $day, $lastDay = min(Horde_Date_Utils::daysInMonth($month, $rule[2]), $i + 6);
$day > 1 && $i <= $lastDay;
$i++) {
$days[] = $i;
}
$component->setAttribute(
'RRULE',
'FREQ=YEARLY;BYMONTH=' . $month
. ($days ? (';BYMONTHDAY=' . implode(',', $days)) : '')
. ';BYDAY=1' . Horde_String::upper(substr($weekday, 0, 2))
. $until);
} elseif (strpos($rule[6], '<=')) {
// Rule starts on a certain weekday before a certain day of
// month.
list($weekday, $day) = explode('>=', $rule[6]);
$weekdayInt = $this->_weekdays[substr($weekday, 0, 3)];
$last = new Horde_Date(array(
'year' => $rule[2],
'month' => $month,
'mday' => $day,
'hour' => $match[1],
'min' => $match[2],
'sec' => 0
));
while ($last->dayOfWeek() != $weekdayInt) {
$last->mday--;
}
$component->setAttribute('DTSTART', $last);
if ($rule[3][0] == 'm') {
$until = '';
} else {
$last = new Horde_Date(
array('year' => $rule[3],
'month' => $month,
'mday' => $day,
'hour' => $match[1],
'min' => $match[2],
'sec' => 0),
$tzid);
while ($last->dayOfWeek() != $weekday) {
$last->mday--;
}
$last->setTimezone('UTC');
$until = ';UNTIL=' . $last->format('Ymd\THIs') . 'Z';
}
for ($days = array(), $i = 1; $i <= $day; $i++) {
$days[] = $i;
}
$component->setAttribute(
'RRULE',
'FREQ=YEARLY;BYMONTH=' . $month
. ';BYMONTHDAY=' . implode(',', $days)
. ';BYDAY=-1' . Horde_String::upper(substr($weekday, 0, 2))
. $until);
}
$component->setAttribute('TZNAME', sprintf($name, $rule[9]));
$tz->addComponent($component);
}
}
/**
* Calculates the new offset of a timezone.
*
* @param array $start A hash describing the original timezone offset.
* @param string $new A string describing the offset to be added to (or
* subtracted from) the original offset.
*
* @return array A hash describing the new timezone offset.
*/
protected function _getOffset($start, $new)
{
$start = ($start['ahead'] ? 1 : -1) * (60 * $start['hour'] + $start['minute']);
preg_match('/(-)?(\d+):(\d+)/', $new, $match);
$start += ($match[1] == '-' ? -1 : 1) * (60 * $match[2] + $match[3]);
$result = array('ahead' => $start > 0);
$start = abs($start);
$result['hour'] = floor($start / 60);
$result['minute'] = $start % 60;
return $result;
}
}