From 34f25c6009cded9fdac40c5576e7804150752ea7 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 24 Oct 2013 17:46:28 +0200 Subject: [PATCH] Return timeframes when iterating over TimeRange objects refs #4190 --- library/Icinga/Timeline/TimeRange.php | 39 +++++++++++++-------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/library/Icinga/Timeline/TimeRange.php b/library/Icinga/Timeline/TimeRange.php index 86797fc0b..c79419071 100644 --- a/library/Icinga/Timeline/TimeRange.php +++ b/library/Icinga/Timeline/TimeRange.php @@ -128,41 +128,40 @@ class TimeRange implements Iterator */ public function findTimeframe(DateTime $dateTime, $asTimestamp = false) { - foreach ($this as $timeframeIdentifier => $timeframeStart) { - $timeframeEnd = clone $timeframeStart; - + foreach ($this as $timeframeIdentifier => $timeframe) { if ($this->negative) { - $timeframeEnd->sub($this->interval); - - if ($dateTime <= $timeframeStart && $dateTime > $timeframeEnd) { - return $asTimestamp ? $timeframeIdentifier : $this->buildTimeframe($timeframeStart, $timeframeEnd); - } - } else { - $timeframeEnd->add($this->interval); - - if ($dateTime >= $timeframeStart && $dateTime < $timeframeEnd) { - return $asTimestamp ? $timeframeIdentifier : $this->buildTimeframe($timeframeStart, $timeframeEnd); + if ($dateTime <= $timeframe->start && $dateTime > $timeframe->end) { + return $asTimestamp ? $timeframeIdentifier : $timeframe; } + } elseif ($dateTime >= $timeframe->start && $dateTime < $timeframe->end) { + return $asTimestamp ? $timeframeIdentifier : $timeframe; } } } /** - * Return the appropriate timeframe for the given timestamp + * Return the appropriate timeframe for the given timeframe start * - * @param int $timestamp The timestamp for which to return the timeframe + * @param int|DateTime $time The timestamp or date and time for which to return the timeframe * @return StdClass An object with a ´start´ and ´end´ property */ - public function getTimeframe($timestamp) + public function getTimeframe($time) { - $startTime = new DateTime(); - $startTime->setTimestamp($timestamp); + if ($time instanceof DateTime) { + $startTime = $time; + } else { + $startTime = new DateTime(); + $startTime->setTimestamp($time); + } + $endTime = clone $startTime; if ($this->negative) { $endTime->sub($this->interval); + $endTime->add(new DateInterval('PT1S')); } else { $endTime->add($this->interval); + $endTime->sub(new DateInterval('PT1S')); } return $this->buildTimeframe($startTime, $endTime); @@ -209,11 +208,11 @@ class TimeRange implements Iterator /** * Return the current value in the iteration * - * @return DateTime + * @return StdClass */ public function current() { - return $this->current; + return $this->getTimeframe($this->current); } /**