From: Stefan Reiter Date: Mon, 8 Feb 2021 11:15:08 +0000 (+0100) Subject: format: handle undef, 0, and decimals in render_duration X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=4997835b080f3657de99acd145ec64ad0ebb6fdc format: handle undef, 0, and decimals in render_duration Signed-off-by: Stefan Reiter --- diff --git a/src/PVE/Format.pm b/src/PVE/Format.pm index 7c3c062..b345129 100644 --- a/src/PVE/Format.pm +++ b/src/PVE/Format.pm @@ -3,7 +3,7 @@ package PVE::Format; use strict; use warnings; -use POSIX qw(strftime); +use POSIX qw(strftime round); use PVE::JSONSchema; use base 'Exporter'; @@ -33,7 +33,9 @@ sub render_duration { my ($duration_in_seconds) = @_; my $text = ''; - my $rest = $duration_in_seconds; + my $rest = round($duration_in_seconds // 0); + + return "0s" if !$rest; my $step = sub { my ($unit, $unitlength) = @_; diff --git a/test/format_test.pl b/test/format_test.pl index b6688ab..32c00f1 100755 --- a/test/format_test.pl +++ b/test/format_test.pl @@ -31,9 +31,11 @@ my $render_data = [ ["timestamp", 1612776831, undef, "2021-02-08 10:33:51"], ["timestamp_gmt", 0, undef, "1970-01-01 00:00:00"], ["timestamp_gmt", 1612776831, undef, "2021-02-08 09:33:51"], - ["duration", 0, undef, ""], + ["duration", undef, undef, "0s"], + ["duration", 0.3, undef, "0s"], + ["duration", 0, undef, "0s"], ["duration", 40, undef, "40s"], - ["duration", 60, undef, "1m"], + ["duration", 59.64432, undef, "1m"], ["duration", 110, undef, "1m 50s"], ["duration", 7*24*3829*2, undef, "2w 21h 22m 24s"], ["fraction_as_percentage", 0.412, undef, "41.20%"],