1 package PVE
::CalendarEvent
;
9 # Note: This class implements a parser/utils for systemd like calender exents
10 # Date specification is currently not implemented
22 PVE
::JSONSchema
::register_format
('pve-calendar-event', \
&pve_verify_calendar_event
);
23 sub pve_verify_calendar_event
{
24 my ($text, $noerr) = @_;
26 eval { parse_calendar_event
($text); };
28 return undef if $noerr;
29 die "invalid calendar event '$text'\n";
35 # returns a $calspec hash which can be passed to compute_next_event()
36 sub parse_calendar_event
{
39 my $parse_single_timespec = sub {
40 my ($p, $max, $matchall_ref, $res_hash) = @_;
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;
58 die "value '$start' out of range\n" if $start >= $max;
59 $res_hash->{$start} = 1;
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++) {
70 die "unable to parse calendar event '$p'\n";
77 my $matchall_minutes = 0;
78 my $matchall_hours = 0;
79 my $minutes_hash = {};
82 my $dowsel = join('|', keys %$dow_names);
86 my $parse_dowspec = sub {
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;
99 die "unable to parse weekday specification '$p'\n";
103 my @parts = split(/\s+/, $event);
105 if ($parts[0] =~ m/$dowsel/i) {
106 my $dow_spec = shift @parts;
107 foreach my $p (split(',', $dow_spec)) {
108 $parse_dowspec->($p);
111 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
114 if (scalar(@parts) && $parts[0] =~ m/\-/) {
115 my $date_spec = shift @parts;
116 die "date specification not implemented";
119 my $time_spec = shift(@parts) // "00:00";
120 my $chars = '[0-9*/.,]';
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
128 foreach my $p (split(',', $time_spec)) {
129 $parse_single_timespec->($p, 60, \
$matchall_minutes, $minutes_hash);
133 die "unable to parse calendar event\n";
136 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
138 if ($matchall_hours) {
141 $h = [ sort { $a <=> $b } keys %$hours_hash ];
144 if ($matchall_minutes) {
147 $m = [ sort { $a <=> $b } keys %$minutes_hash ];
150 return { h
=> $h, m
=> $m, dow
=> [ sort keys %$dow_hash ]};
153 sub compute_next_event
{
154 my ($calspec, $last, $utc) = @_;
156 my $hspec = $calspec->{h
};
157 my $mspec = $calspec->{m
};
158 my $dowspec = $calspec->{dow
};
160 $last += 60; # at least one minute later
164 my ($min, $hour, $mday, $mon, $year, $wday);
168 (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
169 $startofday = timegm
(0, 0, 0, $mday, $mon, $year);
171 (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
172 $startofday = timelocal
(0, 0, 0, $mday, $mon, $year);
175 $last = $startofday + $hour*3600 + $min*60;
177 my $check_dow = sub {
178 foreach my $d (@$dowspec) {
179 return $last if $d == $wday;
181 return $startofday + ($d-$wday)*86400;
184 return $startofday + (7-$wday)*86400; # start of next week
187 if ((my $next = $check_dow->()) != $last) {
192 my $check_hour = sub {
193 return $last if $hspec eq '*';
194 foreach my $h (@$hspec) {
195 return $last if $h == $hour;
197 return $startofday + $h*3600;
200 return $startofday + 24*3600; # test next day
203 if ((my $next = $check_hour->()) != $last) {
208 my $check_minute = sub {
209 return $last if $mspec eq '*';
210 foreach my $m (@$mspec) {
211 return $last if $m == $min;
213 return $startofday +$hour*3600 + $m*60;
216 return $startofday + ($hour + 1)*3600; # test next hour
219 if ((my $next = $check_minute->()) != $last) {
227 die "unable to compute next calendar event\n";