1 package PVE
::CalendarEvent
;
8 use PVE
::Tools
qw(trim);
10 # Note: This class implements a parser/utils for systemd like calendar exents
11 # Date specification is currently not implemented
23 PVE
::JSONSchema
::register_format
('pve-calendar-event', \
&pve_verify_calendar_event
);
24 sub pve_verify_calendar_event
{
25 my ($text, $noerr) = @_;
27 eval { parse_calendar_event
($text); };
29 return undef if $noerr;
30 die "invalid calendar event '$text' - $err\n";
36 # returns a $calspec hash which can be passed to compute_next_event()
37 sub parse_calendar_event
{
40 $event = trim
($event);
43 die "unable to parse calendar event - event is empty\n";
46 my $parse_single_timespec = sub {
47 my ($p, $max, $matchall_ref, $res_hash) = @_;
49 if ($p =~ m/^((?:\*|[0-9]+))(?:\/([1-9][0-9]*))?
$/) {
50 my ($start, $repetition) = ($1, $2);
51 if (defined($repetition)) {
52 $repetition = int($repetition);
53 $start = $start eq '*' ?
0 : int($start);
54 die "value '$start' out of range\n" if $start >= $max;
55 die "repetition '$repetition' out of range\n" if $repetition >= $max;
56 while ($start < $max) {
57 $res_hash->{$start} = 1;
58 $start += $repetition;
65 die "value '$start' out of range\n" if $start >= $max;
66 $res_hash->{$start} = 1;
69 } elsif ($p =~ m/^([0-9]+)\.\.([1-9][0-9]*)$/) {
70 my ($start, $end) = (int($1), int($2));
71 die "range start '$start' out of range\n" if $start >= $max;
72 die "range end '$end' out of range\n" if $end >= $max || $end < $start;
73 for (my $i = $start; $i <= $end; $i++) {
77 die "unable to parse calendar event '$p'\n";
84 my $matchall_minutes = 0;
85 my $matchall_hours = 0;
86 my $minutes_hash = {};
89 my $dowsel = join('|', keys %$dow_names);
93 my $parse_dowspec = sub {
96 if ($p =~ m/^($dowsel)$/i) {
97 $dow_hash->{$dow_names->{lc($1)}} = 1;
98 } elsif ($p =~ m/^($dowsel)\.\.($dowsel)$/i) {
99 my $start = $dow_names->{lc($1)};
100 my $end = $dow_names->{lc($2)} || 7;
101 die "wrong order in range '$p'\n" if $end < $start;
102 for (my $i = $start; $i <= $end; $i++) {
103 $dow_hash->{($i % 7)} = 1;
106 die "unable to parse weekday specification '$p'\n";
110 my @parts = split(/\s+/, $event);
111 my $utc = (@parts && uc($parts[-1]) eq 'UTC');
115 if ($parts[0] =~ m/$dowsel/i) {
116 my $dow_spec = shift @parts;
117 foreach my $p (split(',', $dow_spec)) {
118 $parse_dowspec->($p);
121 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
124 if (scalar(@parts) && $parts[0] =~ m/\-/) {
125 my $date_spec = shift @parts;
126 die "date specification not implemented";
129 my $time_spec = shift(@parts) // "00:00";
130 my $chars = '[0-9*/.,]';
132 if ($time_spec =~ m/^($chars+):($chars+)$/) {
133 my ($p1, $p2) = ($1, $2);
134 foreach my $p (split(',', $p1)) {
135 $parse_single_timespec->($p, 24, \
$matchall_hours, $hours_hash);
137 foreach my $p (split(',', $p2)) {
138 $parse_single_timespec->($p, 60, \
$matchall_minutes, $minutes_hash);
140 } elsif ($time_spec =~ m/^($chars)+$/) { # minutes only
142 foreach my $p (split(',', $time_spec)) {
143 $parse_single_timespec->($p, 60, \
$matchall_minutes, $minutes_hash);
147 die "unable to parse calendar event\n";
150 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
152 if ($matchall_hours) {
155 $h = [ sort { $a <=> $b } keys %$hours_hash ];
158 if ($matchall_minutes) {
161 $m = [ sort { $a <=> $b } keys %$minutes_hash ];
164 return { h
=> $h, m
=> $m, dow
=> [ sort keys %$dow_hash ], utc
=> $utc };
167 sub is_leap_year
($) {
168 return 0 if $_[0] % 4;
169 return 1 if $_[0] % 100;
170 return 0 if $_[0] % 400;
174 # mon = 0.. (Jan = 0)
175 sub days_in_month
($$) {
176 my ($mon, $year) = @_;
177 return 28 + is_leap_year
($year) if $mon == 1;
178 return (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$mon];
182 # mon = 0.. (Jan = 0)
185 my ($sec, $min, $hour, $day, $mon, $year, $wday) = @$time;
204 # Translate to 0..($days_in_mon-1)
207 my $days_in_mon = days_in_month
($mon % 12, $year);
208 last if $day < $days_in_mon;
210 $day -= $days_in_mon;
213 # Translate back to 1..$days_in_mon
222 return [$sec, $min, $hour, $day, $mon, $year, $wday];
225 # helper as we need to keep weekdays in sync
226 sub time_add_days
($$) {
227 my ($time, $inc) = @_;
228 my ($sec, $min, $hour, $day, $mon, $year, $wday) = @$time;
229 return wrap_time
([$sec, $min, $hour, $day + $inc, $mon, $year, $wday + $inc]);
232 sub compute_next_event
{
233 my ($calspec, $last) = @_;
235 my $hspec = $calspec->{h
};
236 my $mspec = $calspec->{m
};
237 my $dowspec = $calspec->{dow
};
238 my $utc = $calspec->{utc
};
240 $last += 60; # at least one minute later
242 my $t = [$utc ?
gmtime($last) : localtime($last)];
243 $t->[0] = 0; # we're not interested in seconds, actually
244 $t->[5] += 1900; # real years for clarity
246 outer
: for (my $i = 0; $i < 1000; ++$i) {
248 foreach my $d (@$dowspec) {
249 goto this_wday
if $d == $wday;
251 $t->[0] = $t->[1] = $t->[2] = 0; # sec = min = hour = 0
252 $t = time_add_days
($t, $d - $wday);
257 $t->[0] = $t->[1] = $t->[2] = 0; # sec = min = hour = 0
258 $t = time_add_days
($t, 7 - $wday);
262 goto this_hour
if $hspec eq '*';
264 foreach my $h (@$hspec) {
265 goto this_hour
if $h == $hour;
267 $t->[0] = $t->[1] = 0; # sec = min = 0
268 $t->[2] = $h; # hour = $h
273 $t->[0] = $t->[1] = $t->[2] = 0; # sec = min = hour = 0
274 $t = time_add_days
($t, 1);
278 goto this_min
if $mspec eq '*';
280 foreach my $m (@$mspec) {
281 goto this_min
if $m == $min;
283 $t->[0] = 0; # sec = 0
284 $t->[1] = $m; # min = $m
289 $t->[0] = $t->[1] = 0; # sec = min = hour = 0
295 return $utc ? timegm
(@$t) : timelocal
(@$t);
298 die "unable to compute next calendar event\n";