diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:46:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:46:43 +0000 |
commit | 3e02d5aff85babc3ffbfcf52313f2108e313aa23 (patch) | |
tree | b01f3923360c20a6a504aff42d45670c58af3ec5 /library/Icinga/Util/Format.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-3e02d5aff85babc3ffbfcf52313f2108e313aa23.tar.xz icingaweb2-3e02d5aff85babc3ffbfcf52313f2108e313aa23.zip |
Adding upstream version 2.12.1.upstream/2.12.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Icinga/Util/Format.php')
-rw-r--r-- | library/Icinga/Util/Format.php | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php new file mode 100644 index 0000000..1158208 --- /dev/null +++ b/library/Icinga/Util/Format.php @@ -0,0 +1,197 @@ +<?php +/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */ + +namespace Icinga\Util; + +use DateTime; + +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); + + protected static $secondPrefix = array('s', 'ms', 'µs', 'ns', 'ps', 'fs', 'as'); + protected static $secondBase = 1000; + + 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] + ); + } + + public static function seconds($value) + { + if ($value === null) { + return ''; + } + + $absValue = abs($value); + + if ($absValue < 60) { + return self::formatForUnits($value, self::$secondPrefix, self::$secondBase); + } elseif ($absValue < 3600) { + return sprintf('%0.2f m', $value / 60); + } elseif ($absValue < 86400) { + return sprintf('%0.2f h', $value / 3600); + } + + // TODO: Do we need weeks, months and years? + return sprintf('%0.2f d', $value / 86400); + } + + protected static function formatForUnits($value, &$units, $base) + { + if ($value === null) { + return ''; + } + + $sign = ''; + if ($value < 0) { + $value = abs($value); + $sign = '-'; + } + + if ($value == 0) { + $pow = $result = 0; + } else { + $pow = floor(log($value, $base)); + $result = $value / pow($base, $pow); + } + + // 1034.23 looks better than 1.03, but 2.03 is fine: + if ($pow > 0 && $result < 2) { + $result = $value / pow($base, --$pow); + } + + return sprintf( + '%s%0.2f %s', + $sign, + $result, + $units[abs($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 ($dateTimeOrTimestamp === null) { + return 0; + } + + 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) + { + if ($dateTimeOrTimestamp === null) { + return 0; + } + + 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 ($dateTimeOrTimestamp === null) { + return false; + } + + if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { + $dt = new DateTime(); + $dt->setTimestamp($dateTimeOrTimestamp); + } + + return $dt->format('L') == 1; + } + + /** + * Unpack shorthand bytes PHP directives to bytes + * + * @param string $subject + * + * @return int + */ + public static function unpackShorthandBytes($subject) + { + $base = (int) $subject; + + if ($base <= -1) { + return INF; + } + + switch (strtoupper($subject[strlen($subject) - 1])) { + case 'K': + $multiplier = 1024; + break; + case 'M': + $multiplier = 1024 ** 2; + break; + case 'G': + $multiplier = 1024 ** 3; + break; + default: + $multiplier = 1; + break; + } + + return $base * $multiplier; + } +} |