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