]> git.proxmox.com Git - pve-common.git/blob - src/PVE/CalendarEvent.pm
f6a11c4423e6395c09a39f90735a7b2e97350ba7
[pve-common.git] / src / PVE / CalendarEvent.pm
1 package PVE::CalendarEvent;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use Time::Local;
7 use PVE::JSONSchema;
8
9 # Note: This class implements a parser/utils for systemd like calender exents
10 # Date specification is currently not implemented
11
12 my $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
22 PVE::JSONSchema::register_format('pve-calendar-event', \&pve_verify_calendar_event);
23 sub 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
34 # The parser.
35 # returns a $calspec hash which can be passed to compute_next_event()
36 sub 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);
58 $res_hash->{$start} = 1;
59 }
60 }
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++) {
66 $res_hash->{$i} = 1;
67 }
68 } else {
69 die "unable to parse calendar event '$p'\n";
70 }
71 };
72
73 my $h = undef;
74 my $m = undef;
75
76 my $matchall_minutes = 0;
77 my $matchall_hours = 0;
78 my $minutes_hash = {};
79 my $hours_hash = {};
80
81 my $dowsel = join('|', keys %$dow_names);
82
83 my $dow_hash;
84
85 my $parse_dowspec = sub {
86 my ($p) = @_;
87
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;
96 }
97 } else {
98 die "unable to parse weekday specification '$p'\n";
99 }
100 };
101
102 my @parts = split(/\s+/, $event);
103
104 if ($parts[0] =~ m/$dowsel/i) {
105 my $dow_spec = shift @parts;
106 foreach my $p (split(',', $dow_spec)) {
107 $parse_dowspec->($p);
108 }
109 } else {
110 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
111 }
112
113 if (scalar(@parts) && $parts[0] =~ m/\-/) {
114 my $date_spec = shift @parts;
115 die "date specification not implemented";
116 }
117
118 my $time_spec = shift(@parts) // "00:00";
119 my $chars = '[0-9*/.,]';
120
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
126 $matchall_hours = 1;
127 foreach my $p (split(',', $time_spec)) {
128 $parse_single_timespec->($p, 60, \$matchall_minutes, $minutes_hash);
129 }
130
131 } else {
132 die "unable to parse calendar event\n";
133 }
134
135 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
136
137 if ($matchall_hours) {
138 $h = '*';
139 } else {
140 $h = [ sort keys %$hours_hash ];
141 }
142
143 if ($matchall_minutes) {
144 $m = '*';
145 } else {
146 $m = [ sort keys %$minutes_hash ];
147 }
148
149 return { h => $h, m => $m, dow => [ sort keys %$dow_hash ]};
150 }
151
152 sub compute_next_event {
153 my ($calspec, $last, $utc) = @_;
154
155 my $hspec = $calspec->{h};
156 my $mspec = $calspec->{m};
157 my $dowspec = $calspec->{dow};
158
159 $last += 60; # at least one minute later
160
161 while (1) {
162
163 my ($min, $hour, $mday, $mon, $year, $wday);
164 my $startofday;
165
166 if ($utc) {
167 (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
168 $startofday = timegm(0, 0, 0, $mday, $mon, $year);
169 } else {
170 (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
171 $startofday = timelocal(0, 0, 0, $mday, $mon, $year);
172 }
173
174 $last = $startofday + $hour*3600 + $min*60;
175
176 my $check_dow = sub {
177 foreach my $d (@$dowspec) {
178 return $last if $d == $wday;
179 if ($d > $wday) {
180 return $startofday + ($d-$wday)*86400;
181 }
182 }
183 return $startofday + (7-$wday)*86400; # start of next week
184 };
185
186 if ((my $next = $check_dow->()) != $last) {
187 $last = $next;
188 next; # repeat
189 }
190
191 my $check_hour = sub {
192 return $last if $hspec eq '*';
193 foreach my $h (@$hspec) {
194 return $last if $h == $hour;
195 if ($h > $hour) {
196 return $startofday + $h*3600;
197 }
198 }
199 return $startofday + 24*3600; # test next day
200 };
201
202 if ((my $next = $check_hour->()) != $last) {
203 $last = $next;
204 next; # repeat
205 }
206
207 my $check_minute = sub {
208 return $last if $mspec eq '*';
209 foreach my $m (@$mspec) {
210 return $last if $m == $min;
211 if ($m > $min) {
212 return $startofday +$hour*3600 + $m*60;
213 }
214 }
215 return $startofday + ($hour + 1)*3600; # test next hour
216 };
217
218 if ((my $next = $check_minute->()) != $last) {
219 $last = $next;
220 next; # repeat
221 } else {
222 return $last;
223 }
224 }
225
226 die "unable to compute next calendar event\n";
227 }
228
229 1;