]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CalendarEvent.pm
1 package PVE
::CalendarEvent
;
8 # Note: This class implements a parser/utils for systemd like calender exents
9 # Date specification is currently not implemented
22 # returns a $calspec hash which can be passed to compute_next_event()
23 sub parse_calendar_event
{
26 my $parse_single_timespec = sub {
27 my ($p, $max, $matchall_ref, $res_hash) = @_;
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;
45 $res_hash->{$start} = 1;
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++) {
56 die "unable to parse calendar event '$p'\n";
63 my $matchall_minutes = 0;
64 my $matchall_hours = 0;
65 my $minutes_hash = {};
68 my $dowsel = join('|', keys %$dow_names);
72 my $parse_dowspec = sub {
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;
85 die "unable to parse weekday specification '$p'\n";
89 my @parts = split(/\s+/, $event);
91 if ($parts[0] =~ m/$dowsel/i) {
92 my $dow_spec = shift @parts;
93 foreach my $p (split(',', $dow_spec)) {
97 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
100 if (scalar(@parts) && $parts[0] =~ m/\-/) {
101 my $date_spec = shift @parts;
102 die "date specification not implemented";
105 my $time_spec = shift(@parts) // "00:00";
106 my $chars = '[0-9*/.,]';
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
114 foreach my $p (split(',', $time_spec)) {
115 $parse_single_timespec->($p, 60, \
$matchall_minutes, $minutes_hash);
119 die "unable to parse calendar event\n";
122 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
124 if ($matchall_hours) {
127 $h = [ sort keys %$hours_hash ];
130 if ($matchall_minutes) {
133 $m = [ sort keys %$minutes_hash ];
136 return { h
=> $h, m
=> $m, dow
=> [ sort keys %$dow_hash ]};
139 sub compute_next_event
{
140 my ($calspec, $last, $utc) = @_;
142 my $hspec = $calspec->{h
};
143 my $mspec = $calspec->{m
};
144 my $dowspec = $calspec->{dow
};
146 $last += 60; # at least one minute later
150 my ($min, $hour, $mday, $mon, $year, $wday);
154 (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
155 $startofday = timegm
(0, 0, 0, $mday, $mon, $year);
157 (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
158 $startofday = timelocal
(0, 0, 0, $mday, $mon, $year);
161 $last = $startofday + $hour*3600 + $min*60;
163 my $check_dow = sub {
164 foreach my $d (@$dowspec) {
165 return $last if $d == $wday;
167 return $startofday + ($d-$wday)*86400;
170 return $startofday + (7-$wday)*86400; # start of next week
173 if ((my $next = $check_dow->()) != $last) {
178 my $check_hour = sub {
179 return $last if $hspec eq '*';
180 foreach my $h (@$hspec) {
181 return $last if $h == $hour;
183 return $startofday + $h*3600;
186 return $startofday + 24*3600; # test next day
189 if ((my $next = $check_hour->()) != $last) {
194 my $check_minute = sub {
195 return $last if $mspec eq '*';
196 foreach my $m (@$mspec) {
197 return $last if $m == $min;
199 return $startofday +$hour*3600 + $m*60;
202 return $startofday + ($hour + 1)*3600; # test next hour
205 if ((my $next = $check_minute->()) != $last) {
213 die "unable to compute next calendar event\n";