Return timeframes when iterating over TimeRange objects

refs #4190
This commit is contained in:
Johannes Meyer 2013-10-24 17:46:28 +02:00
parent 40f70eb572
commit 34f25c6009
1 changed files with 19 additions and 20 deletions

View File

@ -128,41 +128,40 @@ class TimeRange implements Iterator
*/ */
public function findTimeframe(DateTime $dateTime, $asTimestamp = false) public function findTimeframe(DateTime $dateTime, $asTimestamp = false)
{ {
foreach ($this as $timeframeIdentifier => $timeframeStart) { foreach ($this as $timeframeIdentifier => $timeframe) {
$timeframeEnd = clone $timeframeStart;
if ($this->negative) { if ($this->negative) {
$timeframeEnd->sub($this->interval); if ($dateTime <= $timeframe->start && $dateTime > $timeframe->end) {
return $asTimestamp ? $timeframeIdentifier : $timeframe;
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);
} }
} 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 * @return StdClass An object with a ´start´ and ´end´ property
*/ */
public function getTimeframe($timestamp) public function getTimeframe($time)
{ {
if ($time instanceof DateTime) {
$startTime = $time;
} else {
$startTime = new DateTime(); $startTime = new DateTime();
$startTime->setTimestamp($timestamp); $startTime->setTimestamp($time);
}
$endTime = clone $startTime; $endTime = clone $startTime;
if ($this->negative) { if ($this->negative) {
$endTime->sub($this->interval); $endTime->sub($this->interval);
$endTime->add(new DateInterval('PT1S'));
} else { } else {
$endTime->add($this->interval); $endTime->add($this->interval);
$endTime->sub(new DateInterval('PT1S'));
} }
return $this->buildTimeframe($startTime, $endTime); return $this->buildTimeframe($startTime, $endTime);
@ -209,11 +208,11 @@ class TimeRange implements Iterator
/** /**
* Return the current value in the iteration * Return the current value in the iteration
* *
* @return DateTime * @return StdClass
*/ */
public function current() public function current()
{ {
return $this->current; return $this->getTimeframe($this->current);
} }
/** /**