Move non-factory functions from DateTimeFactory to Icinga\Util\Format

This commit is contained in:
Johannes Meyer 2014-04-17 15:33:57 +02:00
parent 3e83854e04
commit 1db9b1ede4
4 changed files with 59 additions and 46 deletions

View File

@ -91,39 +91,4 @@ class DateTimeFactory implements ConfigAwareFactory
{ {
return new DateTime($time, $timeZone !== null ? $timeZone : self::$timeZone); 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;
}
} }

View File

@ -29,6 +29,7 @@
namespace Icinga\Util; namespace Icinga\Util;
use DateTime;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
class Format class Format
@ -150,4 +151,50 @@ class Format
$units[$pow] $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;
}
} }

View File

@ -6,6 +6,7 @@ use \DateTime;
use \DateInterval; use \DateInterval;
use \Zend_Config; use \Zend_Config;
use Icinga\Web\Url; use Icinga\Web\Url;
use Icinga\Util\Format;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Util\DateTimeFactory; use Icinga\Util\DateTimeFactory;
use Icinga\Web\Controller\ActionController; use Icinga\Web\Controller\ActionController;
@ -170,13 +171,13 @@ class Monitoring_TimelineController extends ActionController
case '1m': case '1m':
$dateCopy = clone $dateTime; $dateCopy = clone $dateTime;
for ($i = 0; $i < 6; $i++) { 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); return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime);
case '1y': case '1y':
$dateCopy = clone $dateTime; $dateCopy = clone $dateTime;
for ($i = 0; $i < 4; $i++) { 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); return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime);
default: default:

View File

@ -4,11 +4,11 @@
namespace Icinga\Module\Monitoring\Timeline; namespace Icinga\Module\Monitoring\Timeline;
use \StdClass; use StdClass;
use \Iterator; use Iterator;
use \DateTime; use DateTime;
use \DateInterval; use DateInterval;
use Icinga\Util\DateTimeFactory; use Icinga\Util\Format;
/** /**
* A range of time split into a specific interval * A range of time split into a specific interval
@ -172,17 +172,17 @@ class TimeRange implements Iterator
} elseif ($this->interval->m) { } elseif ($this->interval->m) {
for ($i = 0; $i < $this->interval->m; $i++) { for ($i = 0; $i < $this->interval->m; $i++) {
if ($this->negative) { if ($this->negative) {
$dateTime->sub(new DateInterval('PT' . DateTimeFactory::getSecondsByMonth($dateTime) . 'S')); $dateTime->sub(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S'));
} else { } else {
$dateTime->add(new DateInterval('PT' . DateTimeFactory::getSecondsByMonth($dateTime) . 'S')); $dateTime->add(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S'));
} }
} }
} elseif ($this->interval->y) { } elseif ($this->interval->y) {
for ($i = 0; $i < $this->interval->y; $i++) { for ($i = 0; $i < $this->interval->y; $i++) {
if ($this->negative) { if ($this->negative) {
$dateTime->sub(new DateInterval('PT' . DateTimeFactory::getSecondsByYear($dateTime) . 'S')); $dateTime->sub(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S'));
} else { } else {
$dateTime->add(new DateInterval('PT' . DateTimeFactory::getSecondsByYear($dateTime) . 'S')); $dateTime->add(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S'));
} }
} }
} }