From 745059a6e6f7f90e532707c757a3d27cceade240 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Wed, 6 Jul 2022 14:30:29 +0200 Subject: [PATCH] Format.php: Handle null values (cherry picked from commit 21e4c68a581bb8547a34321c3fcbaf9dfb99ef5f) --- library/Icinga/Util/Format.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/Icinga/Util/Format.php b/library/Icinga/Util/Format.php index 7808af5cf8..11582080ce 100644 --- a/library/Icinga/Util/Format.php +++ b/library/Icinga/Util/Format.php @@ -54,6 +54,10 @@ public static function bytes($value, $standard = self::STANDARD_IEC) public static function seconds($value) { + if ($value === null) { + return ''; + } + $absValue = abs($value); if ($absValue < 60) { @@ -70,6 +74,10 @@ public static function seconds($value) protected static function formatForUnits($value, &$units, $base) { + if ($value === null) { + return ''; + } + $sign = ''; if ($value < 0) { $value = abs($value); @@ -105,6 +113,10 @@ protected static function formatForUnits($value, &$units, $base) */ public static function secondsByMonth($dateTimeOrTimestamp) { + if ($dateTimeOrTimestamp === null) { + return 0; + } + if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { $dt = new DateTime(); $dt->setTimestamp($dateTimeOrTimestamp); @@ -122,6 +134,10 @@ public static function secondsByMonth($dateTimeOrTimestamp) */ public static function secondsByYear($dateTimeOrTimestamp) { + if ($dateTimeOrTimestamp === null) { + return 0; + } + return (self::isLeapYear($dateTimeOrTimestamp) ? 366 : 365) * 24 * 3600; } @@ -134,6 +150,10 @@ public static function secondsByYear($dateTimeOrTimestamp) */ public static function isLeapYear($dateTimeOrTimestamp) { + if ($dateTimeOrTimestamp === null) { + return false; + } + if (!($dt = $dateTimeOrTimestamp) instanceof DateTime) { $dt = new DateTime(); $dt->setTimestamp($dateTimeOrTimestamp);