]> git.proxmox.com Git - pve-common.git/blob - src/PVE/CalendarEvent.pm
e2bf53ad5b2d1561a8f6e118c3c0238e0140b071
[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 use PVE::Tools qw(trim);
9 use PVE::RS::CalendarEvent;
10
11 # Note: This class implements a parser/utils for systemd like calendar exents
12 # Date specification is currently not implemented
13
14 my $dow_names = {
15 sun => 0,
16 mon => 1,
17 tue => 2,
18 wed => 3,
19 thu => 4,
20 fri => 5,
21 sat => 6,
22 };
23
24 PVE::JSONSchema::register_format('pve-calendar-event', \&pve_verify_calendar_event);
25 sub pve_verify_calendar_event {
26 my ($text, $noerr) = @_;
27
28 eval { parse_calendar_event($text); };
29 if (my $err = $@) {
30 return undef if $noerr;
31 die "invalid calendar event '$text' - $err\n";
32 }
33 return $text;
34 }
35
36 # The parser.
37 # returns a $calspec hash which can be passed to compute_next_event()
38 sub parse_calendar_event {
39 my ($event) = @_;
40
41 $event = trim($event);
42
43 if ($event eq '') {
44 die "unable to parse calendar event - event is empty\n";
45 }
46
47 return PVE::RS::CalendarEvent->new($event);
48 }
49
50 sub compute_next_event {
51 my ($calspec, $last) = @_;
52
53 return $calspec->compute_next_event($last);
54 }
55
56 1;