]>
git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/TimeFrame.pm
1 package PMG
::RuleDB
::TimeFrame
;
9 use PMG
::RuleDB
::Object
;
11 use base
qw(PMG::RuleDB::Object);
25 my $hm_to_minutes = sub {
28 if ($hm =~ m/^(\d+):(\d+)$/) {
29 my @tmp = split(/:/, $hm);
30 return $tmp[0]*60+$tmp[1];
35 my $minutes_to_hm = sub {
38 my $hour = int($minutes/60);
39 my $rest = int($minutes%60);
41 return sprintf("%02d:%02d", $hour, $rest);
45 my ($type, $start, $end, $ogroup) = @_;
47 my $class = ref($type) || $type;
49 my $self = $class->SUPER::new
($class->otype(), $ogroup);
54 # Note: allow H:i or integer format
56 $start = $hm_to_minutes->($start);
59 $end = $hm_to_minutes->($end);
62 $self->{start
} = $start;
69 my ($type, $ruledb, $id, $ogroup, $value) = @_;
71 my $class = ref($type) || $type;
73 defined($value) || return undef;
75 my ($sh, $sm, $eh, $em) = $value =~ m/(\d+):(\d+)-(\d+):(\d+)/;
77 my $start = $sh*60+$sm;
80 my $obj = $class->new($start, $end, $ogroup);
83 $obj->{digest
} = Digest
::SHA
::sha1_hex
($id, $start, $end, $ogroup);
89 my ($self, $ruledb) = @_;
91 defined($self->{ogroup
}) || return undef;
92 defined($self->{start
}) || return undef;
93 defined($self->{end
}) || return undef;
95 my $start = $minutes_to_hm->($self->{start
});
96 my $end = $minutes_to_hm->($self->{end
});
98 my $v = "$start-$end";
100 if (defined ($self->{id
})) {
104 "UPDATE Object SET Value = ? WHERE ID = ?", undef, $v, $self->{id
});
109 my $sth = $ruledb->{dbh
}->prepare(
110 "INSERT INTO Object " .
111 "(Objectgroup_ID, ObjectType, Value) " .
112 "VALUES (?, ?, ?);");
114 $sth->execute($self->ogroup, $self->otype, $v);
116 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
125 my ($sec,$min,$hour) = localtime($t);
127 my $amin = $hour*60 + $min;
129 if ($self->{end
} >= $self->{start
}) {
131 return $amin >= $self->{start
} && $amin <= $self->{end
};
135 return ($amin <= $self->{end
}) || ($amin >= $self->{start
});
142 my $start = $minutes_to_hm->($self->{start
});
143 my $end = $minutes_to_hm->($self->{end
});
145 return "$start-$end";
153 description
=> "Start time in `H:i` format (00:00).",
155 pattern
=> '\d?\d:\d?\d',
158 description
=> "End time in `H:i` format (00:00).",
160 pattern
=> '\d?\d:\d?\d',
169 start
=> $minutes_to_hm->($self->{start
}),
170 end
=> $minutes_to_hm->($self->{end
}),
175 my ($self, $param) = @_;
177 $self->{start
} = $hm_to_minutes->($param->{start
});
178 $self->{end
} = $hm_to_minutes->($param->{end
});
186 =head1 PMG::RuleDB::TimeFrame
188 A WHEN object to check for a specific daytime.
194 Start time im minutes since 00:00.
198 End time im minutes since 00:00.
202 $obj = PMG::RuleDB::TimeFrame->new(8*60+15, 16*60+30);
204 Represent: 8:15 to 16:30
206 Note: End time is allowed to be smaller that start time.