From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id BFEDF62C8F for ; Mon, 8 Feb 2021 12:17:33 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B761420C37 for ; Mon, 8 Feb 2021 12:17:03 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 1F48220C08 for ; Mon, 8 Feb 2021 12:17:02 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DE7F845694 for ; Mon, 8 Feb 2021 12:17:01 +0100 (CET) From: Stefan Reiter To: pve-devel@lists.proxmox.com Date: Mon, 8 Feb 2021 12:15:08 +0100 Message-Id: <20210208111512.27281-2-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210208111512.27281-1-s.reiter@proxmox.com> References: <20210208111512.27281-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.036 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH v2 common 2/6] format: handle undef, 0, and decimals in render_duration X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Feb 2021 11:17:33 -0000 Signed-off-by: Stefan Reiter --- This might be a "breaking" change for some situations, but I believe this should be more correct in the long run... If this is not wanted, we must do a ceil() or similar on the value passed in from patch 4/6, otherwise it will not print a time value for the first iteration. src/PVE/Format.pm | 6 ++++-- test/format_test.pl | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) 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%"], -- 2.20.1