Skip to content

Commit

Permalink
Format.php: Handle null values
Browse files Browse the repository at this point in the history
(cherry picked from commit 21e4c68)
  • Loading branch information
sukhwinder33445 authored and nilmerg committed Jul 6, 2022
1 parent 958da16 commit 745059a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions library/Icinga/Util/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down

0 comments on commit 745059a

Please sign in to comment.