2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2015-02-03 15:49:34 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | http://www.gnu.org/licenses/gpl-2.0.txt */
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Util;
|
|
|
|
|
2014-04-17 15:33:57 +02:00
|
|
|
use DateTime;
|
2014-02-21 11:29:27 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
class Format
|
|
|
|
{
|
|
|
|
const STANDARD_IEC = 0;
|
|
|
|
const STANDARD_SI = 1;
|
|
|
|
protected static $instance;
|
|
|
|
|
|
|
|
protected static $bitPrefix = array(
|
|
|
|
array('bit', 'Kibit', 'Mibit', 'Gibit', 'Tibit', 'Pibit', 'Eibit', 'Zibit', 'Yibit'),
|
|
|
|
array('bit', 'kbit', 'Mbit', 'Gbit', 'Tbit', 'Pbit', 'Ebit', 'Zbit', 'Ybit'),
|
|
|
|
);
|
|
|
|
protected static $bitBase = array(1024, 1000);
|
|
|
|
|
|
|
|
protected static $bytePrefix = array(
|
|
|
|
array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'),
|
|
|
|
array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
|
|
|
|
);
|
|
|
|
protected static $byteBase = array(1024, 1000);
|
|
|
|
|
2014-05-05 16:16:45 +02:00
|
|
|
protected static $secondPrefix = array('s', 'ms', 'µs', 'ns', 'ps', 'fs', 'as');
|
|
|
|
protected static $secondBase = 1000;
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
public static function getInstance()
|
|
|
|
{
|
|
|
|
if (self::$instance === null) {
|
|
|
|
self::$instance = new Format;
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function bits($value, $standard = self::STANDARD_SI)
|
|
|
|
{
|
|
|
|
return self::formatForUnits(
|
|
|
|
$value,
|
|
|
|
self::$bitPrefix[$standard],
|
|
|
|
self::$bitBase[$standard]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function bytes($value, $standard = self::STANDARD_IEC)
|
|
|
|
{
|
|
|
|
return self::formatForUnits(
|
|
|
|
$value,
|
|
|
|
self::$bytePrefix[$standard],
|
|
|
|
self::$byteBase[$standard]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-05 16:16:45 +02:00
|
|
|
public static function seconds($value)
|
|
|
|
{
|
|
|
|
if ($value < 60) {
|
|
|
|
return self::formatForUnits($value, self::$secondPrefix, self::$secondBase);
|
|
|
|
} elseif ($value < 3600) {
|
|
|
|
return sprintf('0.2f m', $value / 60);
|
|
|
|
} elseif ($value < 86400) {
|
|
|
|
return sprintf('0.2f h', $value / 3600);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Do we need weeks, months and years?
|
|
|
|
return sprintf('0.2f d', $value / 86400);
|
|
|
|
}
|
|
|
|
|
2013-06-27 13:45:38 +02:00
|
|
|
public static function duration($duration)
|
|
|
|
{
|
2014-03-09 21:16:56 +01:00
|
|
|
if ($duration === null || $duration === false) {
|
2013-09-02 17:33:12 +02:00
|
|
|
return '-';
|
|
|
|
}
|
2013-06-27 13:45:38 +02:00
|
|
|
return self::showHourMin($duration);
|
|
|
|
}
|
|
|
|
|
2014-05-29 13:03:10 +02:00
|
|
|
protected static function showHourMin($sec, $includePrefix = false)
|
2013-06-27 13:45:38 +02:00
|
|
|
{
|
|
|
|
$min = floor($sec / 60);
|
|
|
|
if ($min < 60) {
|
2014-05-29 13:03:10 +02:00
|
|
|
return ($includePrefix ? t('for') . ' ' : '') . $min . 'm ' . ($sec % 60) . 's';
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
|
|
|
$hour = floor($min / 60);
|
|
|
|
if ($hour < 24) {
|
2014-05-29 13:03:10 +02:00
|
|
|
return ($includePrefix ? t('since') . ' ' : '') . date('H:i', time() - $sec);
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
2014-05-29 13:03:10 +02:00
|
|
|
return ($includePrefix ? t('for') . ' ' : '') . floor($hour / 24) . 'd ' . ($hour % 24) . 'h';
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:03:10 +02:00
|
|
|
protected static function smartTimeDiff($diff, $timestamp, $includePrefix = false)
|
2013-06-27 13:45:38 +02:00
|
|
|
{
|
2014-03-09 21:12:24 +01:00
|
|
|
if ($timestamp === null || $timestamp === false) {
|
2013-09-02 17:33:12 +02:00
|
|
|
return '-';
|
|
|
|
}
|
2014-02-21 11:29:27 +01:00
|
|
|
if (! preg_match('~^\d+$~', $timestamp)) {
|
2014-08-26 11:15:19 +02:00
|
|
|
throw new ProgrammingError(
|
|
|
|
'"%s" is not a number',
|
|
|
|
$timestamp
|
|
|
|
);
|
2014-02-21 11:29:27 +01:00
|
|
|
}
|
2013-06-27 13:45:38 +02:00
|
|
|
$prefix = '';
|
2014-03-09 21:12:24 +01:00
|
|
|
if ($diff < 0) {
|
2013-06-27 13:45:38 +02:00
|
|
|
$prefix = '-';
|
|
|
|
}
|
2014-05-29 11:26:02 +02:00
|
|
|
if (abs($diff) > 3600 * 24 * 3) {
|
2013-06-27 13:45:38 +02:00
|
|
|
if (date('Y') === date('Y', $timestamp)) {
|
2014-05-29 13:03:10 +02:00
|
|
|
return ($includePrefix ? t('since') . ' ' : '') . date('d.m.', $timestamp);
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
2014-05-29 13:03:10 +02:00
|
|
|
return ($includePrefix ? t('since') . ' ' : '') . date('m.Y', $timestamp);
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
2014-05-29 13:03:10 +02:00
|
|
|
return $prefix . self::showHourMin(abs($diff), $includePrefix);
|
2014-03-09 21:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function timeSince($timestamp)
|
|
|
|
{
|
|
|
|
return self::smartTimeDiff(time() - $timestamp, $timestamp);
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:03:10 +02:00
|
|
|
public static function prefixedTimeSince($timestamp, $ucfirst = false)
|
2014-05-29 11:26:02 +02:00
|
|
|
{
|
2014-05-29 13:03:10 +02:00
|
|
|
$result = self::smartTimeDiff(time() - $timestamp, $timestamp, true);
|
|
|
|
if ($ucfirst) {
|
|
|
|
$result = ucfirst($result);
|
|
|
|
}
|
|
|
|
return $result;
|
2014-05-29 11:26:02 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 13:45:38 +02:00
|
|
|
public static function timeUntil($timestamp)
|
|
|
|
{
|
2014-03-09 21:12:24 +01:00
|
|
|
return self::smartTimeDiff($timestamp - time(), $timestamp);
|
2013-06-27 13:45:38 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:03:10 +02:00
|
|
|
public static function prefixedTimeUntil($timestamp, $ucfirst)
|
2014-05-29 11:26:02 +02:00
|
|
|
{
|
2014-05-29 13:03:10 +02:00
|
|
|
$result = self::smartTimeDiff($timestamp - time(), $timestamp, true);
|
|
|
|
if ($ucfirst) {
|
|
|
|
$result = ucfirst($result);
|
|
|
|
}
|
|
|
|
return $result;
|
2014-05-29 11:26:02 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
protected static function formatForUnits($value, & $units, $base)
|
|
|
|
{
|
|
|
|
$sign = '';
|
|
|
|
if ($value < 0) {
|
|
|
|
$value = abs($value);
|
|
|
|
$sign = '-';
|
|
|
|
}
|
2014-05-06 08:32:42 +02:00
|
|
|
|
|
|
|
if ($value == 0) {
|
|
|
|
$pow = $result = 0;
|
|
|
|
} else {
|
|
|
|
$pow = floor(log($value, $base));
|
|
|
|
$result = $value / pow($base, $pow);
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
// 1034.23 looks better than 1.03, but 2.03 is fine:
|
|
|
|
if ($pow > 0 && $result < 2) {
|
2014-05-06 08:32:42 +02:00
|
|
|
$result = $value / pow($base, --$pow);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2014-05-06 08:32:42 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
return sprintf(
|
|
|
|
'%s%0.2f %s',
|
|
|
|
$sign,
|
|
|
|
$result,
|
2014-05-05 16:16:45 +02:00
|
|
|
$units[abs($pow)]
|
2013-06-07 11:44:37 +02:00
|
|
|
);
|
|
|
|
}
|
2014-04-17 15:33:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|