]> git.proxmox.com Git - pve-common.git/blame - src/PVE/CalendarEvent.pm
calendarevent: check range for start also without repetition
[pve-common.git] / src / PVE / CalendarEvent.pm
CommitLineData
2244b271
DM
1package PVE::CalendarEvent;
2
3use strict;
4use warnings;
5use Data::Dumper;
6use Time::Local;
55c6e2cd 7use PVE::JSONSchema;
2244b271
DM
8
9# Note: This class implements a parser/utils for systemd like calender exents
10# Date specification is currently not implemented
11
12my $dow_names = {
13 sun => 0,
14 mon => 1,
15 tue => 2,
16 wed => 3,
17 thu => 4,
18 fri => 5,
19 sat => 6,
20};
21
55c6e2cd
DM
22PVE::JSONSchema::register_format('pve-calendar-event', \&pve_verify_calendar_event);
23sub pve_verify_calendar_event {
24 my ($text, $noerr) = @_;
25
26 eval { parse_calendar_event($text); };
27 if (my $err = $@) {
28 return undef if $noerr;
29 die "invalid calendar event '$text'\n";
30 }
31 return $text;
32}
33
2244b271
DM
34# The parser.
35# returns a $calspec hash which can be passed to compute_next_event()
36sub parse_calendar_event {
37 my ($event) = @_;
38
39 my $parse_single_timespec = sub {
40 my ($p, $max, $matchall_ref, $res_hash) = @_;
41
42 if ($p =~ m/^((?:\*|[0-9]+))(?:\/([1-9][0-9]*))?$/) {
43 my ($start, $repetition) = ($1, $2);
44 if (defined($repetition)) {
45 $repetition = int($repetition);
46 $start = $start eq '*' ? 0 : int($start);
47 die "value '$start' out of range\n" if $start >= $max;
48 die "repetition '$repetition' out of range\n" if $repetition >= $max;
49 while ($start < $max) {
50 $res_hash->{$start} = 1;
51 $start += $repetition;
52 }
53 } else {
54 if ($start eq '*') {
55 $$matchall_ref = 1;
56 } else {
57 $start = int($start);
057c619a 58 die "value '$start' out of range\n" if $start >= $max;
2244b271
DM
59 $res_hash->{$start} = 1;
60 }
61 }
62 } elsif ($p =~ m/^([0-9]+)\.\.([1-9][0-9]*)$/) {
63 my ($start, $end) = (int($1), int($2));
64 die "range start '$start' out of range\n" if $start >= $max;
65 die "range end '$end' out of range\n" if $end >= $max || $end < $start;
66 for (my $i = $start; $i <= $end; $i++) {
67 $res_hash->{$i} = 1;
68 }
69 } else {
70 die "unable to parse calendar event '$p'\n";
71 }
72 };
73
74 my $h = undef;
75 my $m = undef;
76
77 my $matchall_minutes = 0;
78 my $matchall_hours = 0;
79 my $minutes_hash = {};
80 my $hours_hash = {};
81
82 my $dowsel = join('|', keys %$dow_names);
83
84 my $dow_hash;
85
86 my $parse_dowspec = sub {
87 my ($p) = @_;
88
89 if ($p =~ m/^($dowsel)$/i) {
90 $dow_hash->{$dow_names->{lc($1)}} = 1;
91 } elsif ($p =~ m/^($dowsel)\.\.($dowsel)$/i) {
92 my $start = $dow_names->{lc($1)};
93 my $end = $dow_names->{lc($2)} || 7;
94 die "wrong order in range '$p'\n" if $end < $start;
95 for (my $i = $start; $i <= $end; $i++) {
96 $dow_hash->{($i % 7)} = 1;
97 }
98 } else {
99 die "unable to parse weekday specification '$p'\n";
100 }
101 };
102
103 my @parts = split(/\s+/, $event);
104
105 if ($parts[0] =~ m/$dowsel/i) {
106 my $dow_spec = shift @parts;
107 foreach my $p (split(',', $dow_spec)) {
108 $parse_dowspec->($p);
109 }
110 } else {
111 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
112 }
113
114 if (scalar(@parts) && $parts[0] =~ m/\-/) {
115 my $date_spec = shift @parts;
116 die "date specification not implemented";
117 }
118
119 my $time_spec = shift(@parts) // "00:00";
120 my $chars = '[0-9*/.,]';
121
122 if ($time_spec =~ m/^($chars+):($chars+)$/) {
123 my ($p1, $p2) = ($1, $2);
124 $parse_single_timespec->($p1, 24, \$matchall_hours, $hours_hash);
125 $parse_single_timespec->($p2, 60, \$matchall_minutes, $minutes_hash);
126 } elsif ($time_spec =~ m/^($chars)+$/) { # minutes only
127 $matchall_hours = 1;
128 foreach my $p (split(',', $time_spec)) {
129 $parse_single_timespec->($p, 60, \$matchall_minutes, $minutes_hash);
130 }
131
132 } else {
133 die "unable to parse calendar event\n";
134 }
135
136 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
137
138 if ($matchall_hours) {
139 $h = '*';
140 } else {
a4200306 141 $h = [ sort { $a <=> $b } keys %$hours_hash ];
2244b271
DM
142 }
143
144 if ($matchall_minutes) {
145 $m = '*';
146 } else {
a4200306 147 $m = [ sort { $a <=> $b } keys %$minutes_hash ];
2244b271
DM
148 }
149
150 return { h => $h, m => $m, dow => [ sort keys %$dow_hash ]};
151}
152
153sub compute_next_event {
154 my ($calspec, $last, $utc) = @_;
155
156 my $hspec = $calspec->{h};
157 my $mspec = $calspec->{m};
158 my $dowspec = $calspec->{dow};
159
160 $last += 60; # at least one minute later
161
162 while (1) {
163
164 my ($min, $hour, $mday, $mon, $year, $wday);
165 my $startofday;
166
167 if ($utc) {
168 (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
169 $startofday = timegm(0, 0, 0, $mday, $mon, $year);
170 } else {
171 (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
172 $startofday = timelocal(0, 0, 0, $mday, $mon, $year);
173 }
174
175 $last = $startofday + $hour*3600 + $min*60;
176
177 my $check_dow = sub {
178 foreach my $d (@$dowspec) {
179 return $last if $d == $wday;
180 if ($d > $wday) {
181 return $startofday + ($d-$wday)*86400;
182 }
183 }
184 return $startofday + (7-$wday)*86400; # start of next week
185 };
186
187 if ((my $next = $check_dow->()) != $last) {
188 $last = $next;
189 next; # repeat
190 }
191
192 my $check_hour = sub {
193 return $last if $hspec eq '*';
194 foreach my $h (@$hspec) {
195 return $last if $h == $hour;
196 if ($h > $hour) {
197 return $startofday + $h*3600;
198 }
199 }
200 return $startofday + 24*3600; # test next day
201 };
202
203 if ((my $next = $check_hour->()) != $last) {
204 $last = $next;
205 next; # repeat
206 }
207
208 my $check_minute = sub {
209 return $last if $mspec eq '*';
210 foreach my $m (@$mspec) {
211 return $last if $m == $min;
212 if ($m > $min) {
213 return $startofday +$hour*3600 + $m*60;
214 }
215 }
216 return $startofday + ($hour + 1)*3600; # test next hour
217 };
218
219 if ((my $next = $check_minute->()) != $last) {
220 $last = $next;
221 next; # repeat
222 } else {
223 return $last;
224 }
225 }
226
227 die "unable to compute next calendar event\n";
228}
229
2301;