/usr/share/icingaweb2/modules/monitoring/library/Monitoring/Timeline/TimeRange.php is in icingaweb2-module-monitoring 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Timeline;
use StdClass;
use Iterator;
use DateTime;
use DateInterval;
use Icinga\Util\Format;
/**
* A range of time split into a specific interval
*
* @see Iterator
*/
class TimeRange implements Iterator
{
/**
* The start of this time range
*
* @var DateTime
*/
protected $start;
/**
* The end of this time range
*
* @var DateTime
*/
protected $end;
/**
* The interval by which this time range is split
*
* @var DateInterval
*/
protected $interval;
/**
* The current date in the iteration
*
* @var DateTime
*/
protected $current;
/**
* Whether the date iteration is negative
*
* @var bool
*/
protected $negative;
/**
* Initialize a new time range
*
* @param DateTime $start When the time range should start
* @param DateTime $end When the time range should end
* @param DateInterval $interval The interval of the time range
*/
public function __construct(DateTime $start, DateTime $end, DateInterval $interval)
{
$this->interval = $interval;
$this->start = $start;
$this->end = $end;
$this->negative = $this->start > $this->end;
}
/**
* Return when this range of time starts
*
* @return DateTime
*/
public function getStart()
{
return $this->start;
}
/**
* Return when this range of time ends
*
* @return DateTime
*/
public function getEnd()
{
return $this->end;
}
/**
* Return the interval by which this time range is split
*
* @return DateInterval
*/
public function getInterval()
{
return $this->interval;
}
/**
* Return the appropriate timeframe for the given date and time or null if none could be found
*
* @param DateTime $dateTime The date and time for which to search the timeframe
* @param bool $asTimestamp Whether the start of the timeframe should be returned as timestamp
* @return StdClass|int An object with a ´start´ and ´end´ property or a timestamp
*/
public function findTimeframe(DateTime $dateTime, $asTimestamp = false)
{
foreach ($this as $timeframeIdentifier => $timeframe) {
if ($this->negative) {
if ($dateTime <= $timeframe->start && $dateTime >= $timeframe->end) {
return $asTimestamp ? $timeframeIdentifier : $timeframe;
}
} elseif ($dateTime >= $timeframe->start && $dateTime <= $timeframe->end) {
return $asTimestamp ? $timeframeIdentifier : $timeframe;
}
}
}
/**
* Return whether the given time is within this range of time
*
* @param int|DateTime $time The timestamp or date and time to check
*/
public function validateTime($time)
{
if ($time instanceof DateTime) {
$dateTime = $time;
} else {
$dateTime = new DateTime();
$dateTime->setTimestamp($time);
}
return ($this->negative && ($dateTime <= $this->start && $dateTime >= $this->end)) ||
(!$this->negative && ($dateTime >= $this->start && $dateTime <= $this->end));
}
/**
* Return the appropriate timeframe for the given timeframe start
*
* @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($time)
{
if ($time instanceof DateTime) {
$startTime = clone $time;
} else {
$startTime = new DateTime();
$startTime->setTimestamp($time);
}
return $this->buildTimeframe($startTime, $this->applyInterval(clone $startTime, 1));
}
/**
* Apply the current interval to the given date and time
*
* @param DateTime $dateTime The date and time to apply the interval to
* @param int $adjustBy By how much seconds the resulting date and time should be adjusted
*
* @return DateTime
*/
protected function applyInterval(DateTime $dateTime, $adjustBy)
{
if (!$this->interval->y && !$this->interval->m) {
if ($this->negative) {
return $dateTime->sub($this->interval)->add(new DateInterval('PT' . $adjustBy . 'S'));
} else {
return $dateTime->add($this->interval)->sub(new DateInterval('PT' . $adjustBy . 'S'));
}
} elseif ($this->interval->m) {
for ($i = 0; $i < $this->interval->m; $i++) {
if ($this->negative) {
$dateTime->sub(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S'));
} else {
$dateTime->add(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S'));
}
}
} elseif ($this->interval->y) {
for ($i = 0; $i < $this->interval->y; $i++) {
if ($this->negative) {
$dateTime->sub(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S'));
} else {
$dateTime->add(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S'));
}
}
}
$adjustment = new DateInterval('PT' . $adjustBy . 'S');
return $this->negative ? $dateTime->add($adjustment) : $dateTime->sub($adjustment);
}
/**
* Return an object representation of the given timeframe
*
* @param DateTime $start The start of the timeframe
* @param DateTime $end The end of the timeframe
* @return StdClass
*/
protected function buildTimeframe(DateTime $start, DateTime $end)
{
$timeframe = new StdClass();
$timeframe->start = $start;
$timeframe->end = $end;
return $timeframe;
}
/**
* Reset the iterator to its initial state
*/
public function rewind()
{
$this->current = clone $this->start;
}
/**
* Return whether the current iteration step is valid
*
* @return bool
*/
public function valid()
{
if ($this->negative) {
return $this->current > $this->end;
} else {
return $this->current < $this->end;
}
}
/**
* Return the current value in the iteration
*
* @return StdClass
*/
public function current()
{
return $this->getTimeframe($this->current);
}
/**
* Return a unique identifier for the current value in the iteration
*
* @return int
*/
public function key()
{
return $this->current->getTimestamp();
}
/**
* Advance the iterator position by one
*/
public function next()
{
$this->applyInterval($this->current, 0);
}
}
|