From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Objects/IcingaTimePeriodRange.php | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 library/Director/Objects/IcingaTimePeriodRange.php (limited to 'library/Director/Objects/IcingaTimePeriodRange.php') diff --git a/library/Director/Objects/IcingaTimePeriodRange.php b/library/Director/Objects/IcingaTimePeriodRange.php new file mode 100644 index 0000000..55c1a3e --- /dev/null +++ b/library/Director/Objects/IcingaTimePeriodRange.php @@ -0,0 +1,88 @@ + null, + 'range_key' => null, + 'range_value' => null, + 'range_type' => 'include', + 'merge_behaviour' => 'set', + ); + + public function isActive($now = null) + { + if ($now === null) { + $now = time(); + } + + if (false === ($weekDay = $this->getWeekDay($this->get('range_key')))) { + // TODO, dates are not yet supported + return false; + } + + if ((int) date('w', $now) !== $weekDay) { + return false; + } + + $timeRanges = preg_split('/\s*,\s*/', $this->get('range_value'), -1, PREG_SPLIT_NO_EMPTY); + foreach ($timeRanges as $timeRange) { + if ($this->timeRangeIsActive($timeRange, $now)) { + return true; + } + } + + return false; + } + + protected function timeRangeIsActive($rangeString, $now) + { + $hBegin = $mBegin = $hEnd = $mEnd = null; + if (sscanf($rangeString, '%2d:%2d-%2d:%2d', $hBegin, $mBegin, $hEnd, $mEnd) === 4) { + if ($this->timeFromHourMin($hBegin, $mBegin, $now) <= $now + && $this->timeFromHourMin($hEnd, $mEnd, $now) >= $now + ) { + return true; + } + } else { + // TODO: throw exception? + } + + return false; + } + + protected function timeFromHourMin($hour, $min, $now) + { + return strtotime(sprintf('%s %02d:%02d:00', date('Y-m-d', $now), $hour, $min)); + } + + protected function getWeekDay($day) + { + switch ($day) { + case 'sunday': + return 0; + case 'monday': + return 1; + case 'tuesday': + return 2; + case 'wednesday': + return 3; + case 'thursday': + return 4; + case 'friday': + return 5; + case 'saturday': + return 6; + } + + return false; + } +} -- cgit v1.2.3