diff --git a/library/Icinga/Util/DateTimeFactory.php b/library/Icinga/Util/DateTimeFactory.php index 17aa72948..9dd854aa7 100644 --- a/library/Icinga/Util/DateTimeFactory.php +++ b/library/Icinga/Util/DateTimeFactory.php @@ -91,39 +91,4 @@ class DateTimeFactory implements ConfigAwareFactory { return new DateTime($time, $timeZone !== null ? $timeZone : self::$timeZone); } - - /** - * Return the amount of seconds based on the given month - * - * @param DateTime $dateTime The date and time to use - * - * @return int - */ - public static function getSecondsByMonth(DateTime $dateTime) - { - return (int) $dateTime->format('t') * 24 * 3600; - } - - /** - * Return the amount of seconds based on the given year - * - * @param DateTime $dateTime The date and time to use - * - * @return int - */ - public static function getSecondsByYear(DateTime $dateTime) - { - return (self::isLeapYear($dateTime) ? 366 : 365) * 24 * 3600; - } - - /** - * Return whether the given year is a leap year - * - * @param DateTime $dateTime The date and time to check - * @return bool - */ - public static function isLeapYear(DateTime $dateTime) - { - return $dateTime->format('L') == 1; - } } diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php index b6f97ff98..8c55cbafa 100644 --- a/library/Icinga/Util/Format.php +++ b/library/Icinga/Util/Format.php @@ -29,6 +29,7 @@ namespace Icinga\Util; +use DateTime; use Icinga\Exception\ProgrammingError; class Format @@ -150,4 +151,50 @@ class Format $units[$pow] ); } + + /** + * Return the amount of seconds based on the given month + * + * @param DateTime|int $dateTimeOrTimestamp The date and time to use + * + * @return int + */ + public static function secondsByMonth($dateTimeOrTimestamp) + { + if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { + $dt = new DateTime(); + $dt->setTimestamp($dateTimeOrTimestamp); + } + + return (int) $dt->format('t') * 24 * 3600; + } + + /** + * Return the amount of seconds based on the given year + * + * @param DateTime|int $dateTimeOrTimestamp The date and time to use + * + * @return int + */ + public static function secondsByYear($dateTimeOrTimestamp) + { + return (self::isLeapYear($dateTimeOrTimestamp) ? 366 : 365) * 24 * 3600; + } + + /** + * Return whether the given year is a leap year + * + * @param DateTime|int $dateTimeOrTimestamp The date and time to use + * + * @return bool + */ + public static function isLeapYear($dateTimeOrTimestamp) + { + if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { + $dt = new DateTime(); + $dt->setTimestamp($dateTimeOrTimestamp); + } + + return $dt->format('L') == 1; + } } diff --git a/modules/monitoring/application/controllers/TimelineController.php b/modules/monitoring/application/controllers/TimelineController.php index 47917995d..48efe0845 100644 --- a/modules/monitoring/application/controllers/TimelineController.php +++ b/modules/monitoring/application/controllers/TimelineController.php @@ -6,6 +6,7 @@ use \DateTime; use \DateInterval; use \Zend_Config; use Icinga\Web\Url; +use Icinga\Util\Format; use Icinga\Application\Config; use Icinga\Util\DateTimeFactory; use Icinga\Web\Controller\ActionController; @@ -170,13 +171,13 @@ class Monitoring_TimelineController extends ActionController case '1m': $dateCopy = clone $dateTime; for ($i = 0; $i < 6; $i++) { - $dateCopy->sub(new DateInterval('PT' . DateTimeFactory::getSecondsByMonth($dateCopy) . 'S')); + $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' . DateTimeFactory::getSecondsByYear($dateCopy) . 'S')); + $dateCopy->sub(new DateInterval('PT' . Format::secondsByYear($dateCopy) . 'S')); } return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime); default: diff --git a/modules/monitoring/library/Monitoring/Timeline/TimeRange.php b/modules/monitoring/library/Monitoring/Timeline/TimeRange.php index 93c81f98f..7ea4787b8 100644 --- a/modules/monitoring/library/Monitoring/Timeline/TimeRange.php +++ b/modules/monitoring/library/Monitoring/Timeline/TimeRange.php @@ -4,11 +4,11 @@ namespace Icinga\Module\Monitoring\Timeline; -use \StdClass; -use \Iterator; -use \DateTime; -use \DateInterval; -use Icinga\Util\DateTimeFactory; +use StdClass; +use Iterator; +use DateTime; +use DateInterval; +use Icinga\Util\Format; /** * A range of time split into a specific interval @@ -172,17 +172,17 @@ class TimeRange implements Iterator } elseif ($this->interval->m) { for ($i = 0; $i < $this->interval->m; $i++) { if ($this->negative) { - $dateTime->sub(new DateInterval('PT' . DateTimeFactory::getSecondsByMonth($dateTime) . 'S')); + $dateTime->sub(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S')); } else { - $dateTime->add(new DateInterval('PT' . DateTimeFactory::getSecondsByMonth($dateTime) . 'S')); + $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' . DateTimeFactory::getSecondsByYear($dateTime) . 'S')); + $dateTime->sub(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S')); } else { - $dateTime->add(new DateInterval('PT' . DateTimeFactory::getSecondsByYear($dateTime) . 'S')); + $dateTime->add(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S')); } } }