]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Format.pm
bump version to 7.3-2
[pve-common.git] / src / PVE / Format.pm
index 7c3c0622e1309f954561c7cd14786d2066080c5a..4c48f2f78cbc467e121bb7290e209240c8605bcf 100644 (file)
@@ -3,8 +3,7 @@ package PVE::Format;
 use strict;
 use warnings;
 
-use POSIX qw(strftime);
-use PVE::JSONSchema;
+use POSIX qw(strftime round);
 
 use base 'Exporter';
 our @EXPORT_OK = qw(
@@ -30,26 +29,30 @@ sub render_timestamp_gmt {
 }
 
 sub render_duration {
-    my ($duration_in_seconds) = @_;
+    my ($duration_in_seconds, $auto_limit_accuracy) = @_;
 
     my $text = '';
-    my $rest = $duration_in_seconds;
+    my $rest = round($duration_in_seconds // 0);
+
+    return "0s" if !$rest;
 
     my $step = sub {
        my ($unit, $unitlength) = @_;
 
-       if ((my $v = int($rest/$unitlength)) > 0) {
+       if ((my $v = int($rest / $unitlength)) > 0) {
            $text .= " " if length($text);
            $text .= "${v}${unit}";
            $rest -= $v * $unitlength;
+           return 1;
        }
+       return undef;
     };
 
-    $step->('w', 7*24*3600);
-    $step->('d', 24*3600);
+    my $weeks = $step->('w', 7 * 24 * 3600);
+    my $days = $step->('d', 24 * 3600) || $weeks;
     $step->('h', 3600);
-    $step->('m', 60);
-    $step->('s', 1);
+    $step->('m', 60) if !$auto_limit_accuracy || !$weeks;
+    $step->('s', 1) if !$auto_limit_accuracy || !$days;
 
     return $text;
 }
@@ -63,6 +66,8 @@ sub render_fraction_as_percentage {
 sub render_bytes {
     my ($value, $precision) = @_;
 
+    $precision = $precision->{precision} if ref($precision) eq 'HASH';
+
     my @units = qw(B KiB MiB GiB TiB PiB);
 
     my $max_unit = 0;