/** * Return a preload interval based on the chosen timeline interval and the given date and time * * @param DateTime $dateTime The date and time to use * * @return DateInterval The interval to pre-load */ private function getPreloadInterval(DateTime $dateTime) { switch ($this->view->intervalBox->getInterval()) { case '1d': return DateInterval::createFromDateString('1 week -1 second'); case '1w': return DateInterval::createFromDateString('8 weeks -1 second'); case '1m': $dateCopy = clone $dateTime; for ($i = 0; $i < 6; $i++) { $dateCopy->sub(new DateInterval('PT' . Format::secondsByMonth($dateCopy) . 'S')); } return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime); case '1y': $dateCopy = clone $dateTime; for ($i = 0; $i < 4; $i++) { $dateCopy->sub(new DateInterval('PT' . Format::secondsByYear($dateCopy) . 'S')); } return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime); default: return DateInterval::createFromDateString('1 day -1 second'); } }
/** * Format the given value depending on the currently used unit */ protected function format($value) { if ($this->isPercentage()) { return (string) $value . '%'; } if ($this->isBytes()) { return Format::bytes($value); } if ($this->isSeconds()) { return Format::seconds($value); } return number_format($value, 2); }
/** * 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); }
<?php /* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */ namespace Icinga\Web\View; use Icinga\Date\DateFormatter; use Icinga\Util\Format; $this->addHelperFunction('format', function () { return Format::getInstance(); }); $this->addHelperFunction('formatDate', function ($date) { if (!$date) { return ''; } return DateFormatter::formatDate($date); }); $this->addHelperFunction('formatDateTime', function ($dateTime) { if (!$dateTime) { return ''; } return DateFormatter::formatDateTime($dateTime); }); $this->addHelperFunction('formatDuration', function ($seconds) { if (!$seconds) { return ''; } return DateFormatter::formatDuration($seconds); }); $this->addHelperFunction('formatTime', function ($time) { if (!$time) { return '';
/** * Prepares benchmark data for output * * Use Benchmark::TIME and Benchmark::MEMORY to choose whether you prefer * to have either time or memory or both in your output * * @param int Whether to get time and/or memory summary * @return array */ protected static function prepareDataForRendering($what = null) { if ($what === null) { $what = self::TIME | self::MEMORY; } $columns = array((object) array('title' => 'Time', 'align' => 'left', 'maxlen' => 4), (object) array('title' => 'Description', 'align' => 'left', 'maxlen' => 11)); if ($what & self::TIME) { $columns[] = (object) array('title' => 'Off (ms)', 'align' => 'right', 'maxlen' => 11); $columns[] = (object) array('title' => 'Dur (ms)', 'align' => 'right', 'maxlen' => 13); } if ($what & self::MEMORY) { $columns[] = (object) array('title' => 'Mem (diff)', 'align' => 'right', 'maxlen' => 10); $columns[] = (object) array('title' => 'Mem (total)', 'align' => 'right', 'maxlen' => 11); } $bench = self::getInstance(); $last = $bench->start; $rows = array(); $lastmem = 0; foreach ($bench->measures as $m) { $micro = sprintf('%03d', round(($m->timestamp - floor($m->timestamp)) * 1000)); $vals = array(date('H:i:s', $m->timestamp) . '.' . $micro, $m->message); if ($what & self::TIME) { $m->relative = $m->timestamp - $bench->start; $m->offset = $m->timestamp - $last; $last = $m->timestamp; $vals[] = sprintf('%0.3f', $m->relative * 1000); $vals[] = sprintf('%0.3f', $m->offset * 1000); } if ($what & self::MEMORY) { $mem = $m->memory - $lastmem; $lastmem = $m->memory; $vals[] = Format::bytes($mem); $vals[] = Format::bytes($m->memory); } $row =& $rows[]; foreach ($vals as $col => $val) { $row[$col] = $val; $columns[$col]->maxlen = max(strlen($val), $columns[$col]->maxlen); } } return (object) array('columns' => $columns, 'rows' => $rows); }
<?php namespace Icinga\Web\View; use Icinga\Web\Url; use Icinga\Util\Format; $this->addHelperFunction('format', function () { return Format::getInstance(); }); $this->addHelperFunction('timeSince', function ($timestamp) { return '<span class="timesince">' . Format::timeSince($timestamp) . '</span>'; }); $this->addHelperFunction('prefixedTimeSince', function ($timestamp, $ucfirst = false) { return '<span class="timesince">' . Format::prefixedTimeSince($timestamp, $ucfirst) . '</span>'; }); $this->addHelperFunction('timeUntil', function ($timestamp) { if (!$timestamp) { return ''; } return '<span class="timeuntil">' . Format::timeUntil($timestamp) . '</span>'; }); $this->addHelperFunction('prefixedTimeUntil', function ($timestamp, $ucfirst = false) { if (!$timestamp) { return ''; } return '<span class="timeuntil">' . Format::prefixedTimeUntil($timestamp, $ucfirst) . '</span>'; });