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 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;
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;
}
}

View File

@ -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:

View File

@ -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'));
}
}
}