Skip to content

Commit e291cce

Browse files
author
Jason Oster
committed
Merge version-2-4
1 parent 62ceba3 commit e291cce

File tree

1 file changed

+142
-118
lines changed

1 file changed

+142
-118
lines changed

functions/parse/recur_functions.php

+142-118
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,50 @@
88
We will use two kinds of functions - those that restrict the date to allowed values and those that expand allowed values
99
*/
1010

11-
function add_recur($times,$freq=''){
11+
function add_recur($times, $freq = '') {
1212
global $recur_data;
1313
global $count, $mArray_begin, $mArray_end, $except_dates, $start_date, $start_date_unixtime,$end_range_unixtime,$until_unixtime, $day_offset, $current_view;
1414
if (!is_array($times)) $times = array($times);
1515
/* BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY, BYDAY, BYHOUR, BYMINUTE, BYSECOND and BYSETPOS */
1616
#dump_times($times);
17-
if ($current_view != 'search'){
18-
$times = restrict_bymonth($times,$freq);
19-
# $times = restrict_byweekno($times,$freq);
20-
$times = restrict_byyearday($times,$freq);
21-
$times = restrict_bymonthday($times,$freq);
22-
$times = restrict_byday($times,$freq);
23-
if($start_date_unixtime > $mArray_begin) $times[] = $start_date_unixtime;
24-
$times = restrict_bysetpos($times,$freq);#echo "restrict_bysetpos";
17+
if ($current_view != 'search') {
18+
$times = restrict_bymonth($times, $freq);
19+
# $times = restrict_byweekno($times, $freq);
20+
$times = restrict_byyearday($times, $freq);
21+
$times = restrict_bymonthday($times, $freq);
22+
$times = restrict_byday($times, $freq);
23+
if ($start_date_unixtime > $mArray_begin) $times[] = $start_date_unixtime;
24+
$times = restrict_bysetpos($times, $freq);
2525
$times = array_unique($times);
2626
sort($times);
2727
}
28-
$until_date = date("Ymd",$end_range_unixtime);
29-
foreach ($times as $time){
30-
#echo "time:". date("Ymd",$time)." $until_date<br>\n";
28+
foreach ($times as $time) {
3129
if (!isset($time) || $time == '') continue;
32-
$date = date("Ymd",$time);
33-
$time = strtotime("$date 12:00:00");
30+
31+
// Why are we arbitrarily setting the time to noon?
32+
$date = date('Ymd', $time);
33+
$time = strtotime($date . ' 12:00:00');
34+
$until_date = date('Ymd', $until_unixtime);
35+
$until_unixtime = strtotime($until_date . ' 12:00:00');
36+
3437
# day offset fixes shifts across day boundaries due to time diffs.
3538
# These are already fixed for the initial instance, but need to be fixed for recurrences
36-
if (date("Ymd", $time) != $start_date) $time = $time + $day_offset * (24*60*60);
37-
if(isset($time)
38-
&& $time != ''
39-
&& !in_array($time, $recur_data)
40-
&& !in_array($date, $except_dates)
41-
&& $time <= $until_unixtime
42-
&& $time >= $start_date_unixtime
43-
&& $date <= $until_date
44-
){
45-
$count--; #echo "\n.$count\n";
46-
if($time >= $mArray_begin
47-
&& $time <= $mArray_end
48-
&& $count >= 0
49-
) $recur_data[] = $time;
39+
if (date('Ymd', $time) != $start_date) $time = $time + ($day_offset * 24 * 60 * 60);
40+
if ((isset($time)) &&
41+
(!in_array($time, $recur_data)) &&
42+
(!in_array($date, $except_dates)) &&
43+
($time <= $until_unixtime) &&
44+
($time >= $start_date_unixtime) &&
45+
($date <= $until_date)) {
46+
$count--;
47+
if (($time >= $mArray_begin) &&
48+
($time <= $mArray_end)
49+
($count >= 0)) {
50+
$recur_data[] = $time;
51+
}
52+
}
53+
else if (in_array($date, $except_dates)) {
54+
$count--;
5055
}
5156
else if (in_array($date, $except_dates)) {
5257
$count--;
@@ -55,74 +60,81 @@ function add_recur($times,$freq=''){
5560
#dump_times($recur_data);
5661
return;
5762
}
58-
function expand_bymonth($time){
63+
64+
function expand_bymonth($time) {
5965
global $bymonth, $byweekno, $bymonthday, $year, $start_unixtime, $freq_type;
60-
if(!empty($byweekno)) return $time;
61-
if(empty($bymonth)) $bymonth = array(date("m", $start_unixtime));
62-
$d = date("d",$start_unixtime);
66+
if (!empty($byweekno)) return $time;
67+
if (empty($bymonth)) $bymonth = array(date('m', $start_unixtime));
68+
$d = date('d', $start_unixtime);
6369
if (!empty($bymonthday)) $d = 1;
64-
foreach ($bymonth as $m){
65-
$time = mktime(12,0,0,$m,$d,$year); #echo "exm:".date("Ymd",$time)."\n";
70+
foreach ($bymonth as $m) {
71+
$time = mktime(12, 0, 0, $m, $d, $year);
6672
$times[] = $time;
6773
}
6874
return $times;
6975
}
70-
function expand_byweekno($times){
76+
77+
function expand_byweekno($times) {
7178
global $byweekno, $year, $freq_type, $wkst, $wkst3char;
7279
# byweekno is only used when repeat is yearly
7380
# when it's set, the input times are irrelevant
7481
# it also needs a byday.
7582
if ($freq_type != 'year') return $times;
7683
if (empty($byweekno)) return $times;
77-
$total_weeks = date("W",mktime(12,0,0,12,24,$year) ) +1;
78-
$w1_start = strtotime("this $wkst3char", mktime(12,0,0,1,1,$year) );
79-
foreach($byweekno as $weekno){
80-
if($weekno < 0) $weekno = $weekno + $total_weeks;
81-
#echo "\n $wkst3char w1st:".date("Ymd", $w1_start)." ".date("Ymd", mktime(12,0,0,1,1,$year))." weekno:$weekno";
82-
$new_times[] = strtotime("+".(($weekno-1)*7)."days",$w1_start);
84+
$total_weeks = date('W', mktime(12, 0, 0, 12, 24, $year)) + 1;
85+
$w1_start = strtotime('this ' . $wkst3char, mktime(12, 0, 0, 1, 1, $year));
86+
foreach ($byweekno as $weekno) {
87+
if ($weekno < 0) {
88+
$weekno = $weekno + $total_weeks;
89+
}
90+
$new_times[] = strtotime('+' . (($weekno - 1) * 7) . ' days', $w1_start);
8391
}
8492
#dump_times($new_times);
8593
return $new_times;
8694
}
8795

88-
function expand_byyearday($times){
96+
function expand_byyearday($times) {
8997
global $byyearday, $year;
9098
if (empty($byyearday)) return $times;
9199
$py = $year-1;
92100
$ny = $year+1;
93101
$new_times = array();
94-
foreach($times as $time){
95-
foreach($byyearday as $yearday){
96-
if($yearday > 0){ $day = strtotime("+$yearday days Dec 31, $py");#echo "\n".date("Ymd",$day)." = +$yearday days Dec 31, $py";
97-
}else $day = strtotime("Jan 1 $ny $yearday days");
98-
if(date("Y",$day == $year)) $new_times[] = $day;
102+
foreach ($times as $time){
103+
foreach ($byyearday as $yearday) {
104+
if ($yearday > 0) {
105+
$day = strtotime('+' . $yearday . ' days Dec 31, ' . $py);
106+
}
107+
else {
108+
$day = strtotime('Jan 1 ' . $ny . ' ' . $yearday . ' days');
109+
}
110+
if (date('Y', $day == $year)) $new_times[] = $day;
99111
}
100112
}
101113
# dump_times($new_times);
102114
return $new_times;
103115
}
104116

105-
function expand_bymonthday($times){
117+
function expand_bymonthday($times) {
106118
global $bymonthday, $year;
107119
if (empty($bymonthday)) return $times;
108-
foreach($times as $time){
109-
$month = date('m',$time);
110-
foreach($bymonthday as $monthday){
111-
if($monthday < 0) $monthday = date("t",$time) + $monthday +1;
112-
$new_times[] = mktime(12,0,0,$month,$monthday,$year);
113-
#echo "monthday:$monthday\n";
120+
foreach ($times as $time) {
121+
$month = date('m', $time);
122+
foreach ($bymonthday as $monthday) {
123+
if ($monthday < 0) {
124+
$monthday = (date('t', $time) + $monthday + 1);
125+
}
126+
$new_times[] = mktime(12, 0, 0, $month, $monthday, $year);
114127
}
115128
}
116129
return $new_times;
117130
}
118131

119-
function expand_byday($time){
132+
function expand_byday($time) {
120133
global $freq_type, $byday, $bymonth,$byweekno, $wkst3char, $year, $month, $start_unixtime, $summary;
121134
if (empty($byday)) return array($time);
122135
$times = array();
123-
$the_sunday = dateOfWeek(date("Ymd",$time), $wkst3char);
124-
#echo "<pre>$summary $freq_type ".print_r($byday,true)."$wkst3char $the_sunday</pre>";
125-
foreach($byday as $key=>$day) {
136+
$the_sunday = dateOfWeek(date('Ymd', $time), $wkst3char);
137+
foreach ($byday as $key => $day) {
126138
/* set $byday_arr
127139
[0] => byday string, e.g. 4TH
128140
[1] => sign/modifier
@@ -131,28 +143,30 @@ function expand_byday($time){
131143
*/
132144
ereg ('([-\+]{0,1})?([0-9]+)?([A-Z]{2})', $day, $byday_arr);
133145
$on_day = two2threeCharDays($byday_arr[3]);
134-
switch($freq_type){
146+
switch ($freq_type) {
135147
case 'week':
136148
#need to find the first day of the appropriate week.
137-
$next_date_time = strtotime("this $on_day",strtotime($the_sunday)) + (12 * 60 * 60);
149+
$next_date_time = strtotime('this ' . $on_day, strtotime($the_sunday)) + (12 * 60 * 60);
138150
$times[] = $next_date_time;
139151
break;
140152
case 'month':
141-
$time = mktime(12,0,0,$month,1,$year);
153+
$time = mktime(12, 0, 0, $month, 1, $year);
154+
// FIXME: Is this supposed to fall through?
142155
case 'year':
143-
if(empty($byweekno)){
144-
$week_arr = array(1,2,3,4,5);
156+
if (empty($byweekno)) {
157+
$week_arr = array(1, 2, 3, 4, 5);
145158
if(isset($byday_arr[2]) && $byday_arr[2] !='') $week_arr = array($byday_arr[2]);
146-
$month_start = strtotime(date("Ym01",$time)) - (24 * 60 * 60);
147-
$month_end = strtotime(date("Ymt",$time))+ (36 * 60 * 60);
148-
if($freq_type == 'year' && empty($bymonth)){
149-
$month_start = mktime(12,0,0,1,0,$year);
150-
$month_end = mktime(12,0,0,1,1,$year+1);
159+
$month_start = strtotime(date('Ym01', $time)) - (24 * 60 * 60);
160+
$month_end = strtotime(date('Ymt', $time)) + (36 * 60 * 60);
161+
if ($freq_type == 'year' && empty($bymonth)) {
162+
$month_start = mktime(12, 0, 0, 1, 0, $year);
163+
$month_end = mktime(12, 0, 0, 1, 1, $year + 1);
151164
}
152-
$month_start_day = strtolower(date("D", $month_start));
153-
foreach($week_arr as $week){
154-
#echo "<pre>$summary ".$byday_arr[1].$week.$on_day." st:".date("Ymd",$month_start)." t:".date("Ymd",$time)."\n";
155-
if($byday_arr[1] == '-') $next_date_time = strtotime($byday_arr[1].$week.$on_day,$month_end);
165+
$month_start_day = strtolower(date('D', $month_start));
166+
foreach ($week_arr as $week) {
167+
if ($byday_arr[1] == '-') {
168+
$next_date_time = strtotime($byday_arr[1] . $week . $on_day, $month_end);
169+
}
156170
else {
157171
# we need this special offset in case the event day coincides with the month start day
158172
# eg: month starts on a Sunday, our event is the Nth Sunday of the month... without this
@@ -161,76 +175,82 @@ function expand_byday($time){
161175
$next_date_time = strtotime($byday_arr[1] . $week . $on_day, ($month_start + $special_offset));
162176
}
163177
# check that we're still in the same month
164-
if (date("m",$next_date_time) == date("m",$time) ) $times[] = $next_date_time;
178+
if (date('m', $next_date_time) == date('m', $time)) $times[] = $next_date_time;
165179
}
166-
}else{
180+
}
181+
else {
167182
# byweekno should act like freq_type = week
168-
$next_date_time = strtotime("this $on_day",strtotime($the_sunday)) + (12 * 60 * 60);
183+
$next_date_time = strtotime('this ' . $on_day, strtotime($the_sunday)) + (12 * 60 * 60);
169184
$times[] = $next_date_time;
170185
}
171186
break;
172187
default:
173-
$month_start = strtotime(date("Ym01",$time));
174-
$next_date_time = strtotime($byday_arr[1].$byday_arr[2].$on_day, $month_start);
188+
$month_start = strtotime(date('Ym01', $time));
189+
$next_date_time = strtotime($byday_arr[1] . $byday_arr[2] . $on_day, $month_start);
190+
break;
175191
}
176192
}
177-
#echo "exp byday";dump_times($times);
178193
return $times;
179194
}
180195

181-
182-
function restrict_bymonth($times,$freq=''){
196+
function restrict_bymonth($times, $freq = '') {
183197
global $bymonth, $byyearday;
184198
if (empty($bymonth) || !empty($byyearday)) return $times;
185-
$new_times=array();
186-
foreach ($times as $time){
187-
if(in_array(date("m", $time), $bymonth)) $new_times[] = $time;
199+
$new_times = array();
200+
foreach ($times as $time) {
201+
if (in_array(date('m', $time), $bymonth)) $new_times[] = $time;
188202
}
189203
return $new_times;
190204
}
191-
function restrict_byweekno($times,$freq=''){
205+
206+
function restrict_byweekno($times, $freq = '') {
192207
global $byweekno;
193-
if(empty($byweekno)) return $times;
194-
$new_times=array();
195-
foreach ($times as $time) if(in_array(date("W", $time), $byweekno)) $new_times[] = $time;
208+
if (empty($byweekno)) return $times;
209+
$new_times = array();
210+
foreach ($times as $time) {
211+
if (in_array(date('W', $time), $byweekno)) $new_times[] = $time;
212+
}
196213
return $new_times;
197214

198215
}
199-
function restrict_byyearday($times,$freq=''){
216+
217+
function restrict_byyearday($times, $freq = '') {
200218
global $byyearday;
201-
if(empty($byyearday)) return $times;
202-
$new_times=array();
203-
foreach ($times as $time){
204-
foreach ($byyearday as $yearday){
205-
if($yearday < 0){
206-
$yearday = 365 + $yearday +1;
207-
if(date("L",$time)) $yearday += 1;
219+
if (empty($byyearday)) return $times;
220+
$new_times = array();
221+
foreach ($times as $time) {
222+
foreach ($byyearday as $yearday) {
223+
if ($yearday < 0) {
224+
$yearday = 365 + $yearday + 1;
225+
if (date('L', $time)) $yearday += 1;
208226
}
209227
$yearday_arr[] = $yearday;
210228
}
211-
# date(z,$time) gives 0 for Jan 1
212-
if(in_array((date("z", $time)+1), $yearday_arr)) $new_times[] = $time;
229+
# date('z', $time) gives 0 for Jan 1
230+
if (in_array((date('z', $time) + 1), $yearday_arr)) $new_times[] = $time;
213231
}
214232
return $new_times;
215233
}
216234

217-
function restrict_bymonthday($times,$freq=''){
235+
function restrict_bymonthday($times, $freq = '') {
218236
global $bymonthday;
219-
if(empty($bymonthday)) return $times;
220-
$new_times=array();
221-
foreach ($times as $time){
222-
foreach ($bymonthday as $monthday){
223-
if($monthday < 0) $monthday = date("t",$time) + $monthday +1;
237+
if (empty($bymonthday)) return $times;
238+
$new_times = array();
239+
foreach ($times as $time) {
240+
foreach ($bymonthday as $monthday) {
241+
if ($monthday < 0) {
242+
$monthday = date('t', $time) + $monthday + 1;
243+
}
224244
$monthday_arr[] = $monthday;
225245
}
226-
if(in_array(date("j", $time), $monthday_arr)) $new_times[] = $time;
246+
if (in_array(date('j', $time), $monthday_arr)) $new_times[] = $time;
227247
}
228248
return $new_times;
229249
}
230-
function restrict_byday($times,$freq=''){
250+
function restrict_byday($times, $freq = '') {
231251
global $byday;
232-
if(empty($byday)) return $times;
233-
foreach($byday as $key=>$day) {
252+
if (empty($byday)) return $times;
253+
foreach ($byday as $key => $day) {
234254
/* set $byday_arr
235255
[0] => byday string, e.g. 4TH
236256
[1] => sign/modifier
@@ -240,27 +260,31 @@ function restrict_byday($times,$freq=''){
240260
ereg ('([-\+]{0,1})?([0-9]{1})?([A-Z]{2})', $day, $byday_arr);
241261
$byday3[] = two2threeCharDays($byday_arr[3]);
242262
}
243-
$new_times=array();
244-
foreach ($times as $time) if(in_array(strtolower(date("D", $time)), $byday3)) $new_times[] = $time;
263+
$new_times = array();
264+
foreach ($times as $time) {
265+
if (in_array(strtolower(date('D', $time)), $byday3)) $new_times[] = $time;
266+
}
245267
return $new_times;
246268
}
247269

248-
function restrict_bysetpos($times,$freq=''){
270+
function restrict_bysetpos($times, $freq = '') {
249271
global $bysetpos;
250-
if(empty($bysetpos)) return $times;
272+
if (empty($bysetpos)) return $times;
251273
sort($times);
252-
$new_times=array();
253-
foreach($bysetpos as $setpos){
254-
$new_times[] = implode('',array_slice($times, $setpos, 1));
274+
$new_times = array();
275+
foreach ($bysetpos as $setpos) {
276+
$new_times[] = implode('', array_slice($times, $setpos, 1));
255277
}
256278
return $new_times;
257279
}
258280

259281
# for diagnostics
260-
function dump_times($times){
282+
function dump_times($times) {
261283
global $summary;
262-
echo "<pre>$summary times:";
284+
echo '<pre>' . $summary . ' times:';
263285
#var_dump($times);
264-
foreach($times as $time) echo "\ndate:".date("Y-m-d H:i:s",$time);
265-
echo "</pre>";
286+
foreach ($times as $time) {
287+
echo "\ndate:" . date('Y-m-d H:i:s', $time);
288+
}
289+
echo '</pre>';
266290
}

0 commit comments

Comments
 (0)