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 $res_hash->{$start} = 1;
61 } elsif ($p =~ m/^([0-9]+)\.\.([1-9][0-9]*)$/) {
62 my ($start, $end) = (int($1), int($2));
63 die "range start '$start' out of range\n" if $start >= $max;
64 die "range end '$end' out of range\n" if $end >= $max || $end < $start;
65 for (my $i = $start; $i <= $end; $i++) {
69 die "unable to parse calendar event '$p'\n";
76 my $matchall_minutes = 0;
77 my $matchall_hours = 0;
78 my $minutes_hash = {};
81 my $dowsel = join('|', keys %$dow_names);
85 my $parse_dowspec = sub {
88 if ($p =~ m/^($dowsel)$/i) {
89 $dow_hash->{$dow_names->{lc($1)}} = 1;
90 } elsif ($p =~ m/^($dowsel)\.\.($dowsel)$/i) {
91 my $start = $dow_names->{lc($1)};
92 my $end = $dow_names->{lc($2)} || 7;
93 die "wrong order in range '$p'\n" if $end < $start;
94 for (my $i = $start; $i <= $end; $i++) {
95 $dow_hash->{($i % 7)} = 1;
98 die "unable to parse weekday specification '$p'\n";
102 my @parts = split(/\s+/, $event);
104 if ($parts[0] =~ m/$dowsel/i) {
105 my $dow_spec = shift @parts;
106 foreach my $p (split(',', $dow_spec)) {
107 $parse_dowspec->($p);
110 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
113 if (scalar(@parts) && $parts[0] =~ m/\-/) {
114 my $date_spec = shift @parts;
115 die "date specification not implemented";
118 my $time_spec = shift(@parts) // "00:00";
119 my $chars = '[0-9*/.,]';
121 if ($time_spec =~ m/^($chars+):($chars+)$/) {
122 my ($p1, $p2) = ($1, $2);
123 $parse_single_timespec->($p1, 24, \
$matchall_hours, $hours_hash);
124 $parse_single_timespec->($p2, 60, \
$matchall_minutes, $minutes_hash);
125 } elsif ($time_spec =~ m/^($chars)+$/) { # minutes only
127 foreach my $p (split(',', $time_spec)) {
128 $parse_single_timespec->($p, 60, \
$matchall_minutes, $minutes_hash);
132 die "unable to parse calendar event\n";
135 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
137 if ($matchall_hours) {
140 $h = [ sort { $a <=> $b } keys %$hours_hash ];
143 if ($matchall_minutes) {
146 $m = [ sort { $a <=> $b } keys %$minutes_hash ];
149 return { h
=> $h, m
=> $m, dow
=> [ sort keys %$dow_hash ]};
152 sub compute_next_event
{
153 my ($calspec, $last, $utc) = @_;
155 my $hspec = $calspec->{h
};
156 my $mspec = $calspec->{m
};
157 my $dowspec = $calspec->{dow
};
159 $last += 60; # at least one minute later
163 my ($min, $hour, $mday, $mon, $year, $wday);
167 (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
168 $startofday = timegm
(0, 0, 0, $mday, $mon, $year);
170 (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
171 $startofday = timelocal
(0, 0, 0, $mday, $mon, $year);
174 $last = $startofday + $hour*3600 + $min*60;
176 my $check_dow = sub {
177 foreach my $d (@$dowspec) {
178 return $last if $d == $wday;
180 return $startofday + ($d-$wday)*86400;
183 return $startofday + (7-$wday)*86400; # start of next week
186 if ((my $next = $check_dow->()) != $last) {
191 my $check_hour = sub {
192 return $last if $hspec eq '*';
193 foreach my $h (@$hspec) {
194 return $last if $h == $hour;
196 return $startofday + $h*3600;
199 return $startofday + 24*3600; # test next day
202 if ((my $next = $check_hour->()) != $last) {
207 my $check_minute = sub {
208 return $last if $mspec eq '*';
209 foreach my $m (@$mspec) {
210 return $last if $m == $min;
212 return $startofday +$hour*3600 + $m*60;
215 return $startofday + ($hour + 1)*3600; # test next hour
218 if ((my $next = $check_minute->()) != $last) {
226 die "unable to compute next calendar event\n";