From 3e02d5aff85babc3ffbfcf52313f2108e313aa23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:46:43 +0200 Subject: Adding upstream version 2.12.1. Signed-off-by: Daniel Baumann --- library/Icinga/Util/Format.php | 197 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 library/Icinga/Util/Format.php (limited to 'library/Icinga/Util/Format.php') 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 @@ + 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; + } +} -- cgit v1.2.3