6 use POSIX
qw(strftime round);
13 render_fraction_as_percentage
17 sub render_timestamp
{
20 # ISO 8601 date format
21 return strftime
("%F %H:%M:%S", localtime($epoch));
24 sub render_timestamp_gmt
{
27 # ISO 8601 date format, standard Greenwich time zone
28 return strftime
("%F %H:%M:%S", gmtime($epoch));
32 my ($duration_in_seconds) = @_;
35 my $rest = round
($duration_in_seconds // 0);
37 return "0s" if !$rest;
40 my ($unit, $unitlength) = @_;
42 if ((my $v = int($rest/$unitlength)) > 0) {
43 $text .= " " if length($text);
44 $text .= "${v}${unit}";
45 $rest -= $v * $unitlength;
49 $step->('w', 7*24*3600);
50 $step->('d', 24*3600);
58 sub render_fraction_as_percentage
{
61 return sprintf("%.2f%%", $fraction*100);
65 my ($value, $precision) = @_;
67 $precision = $precision->{precision
} if ref($precision) eq 'HASH';
69 my @units = qw(B KiB MiB GiB TiB PiB);
73 $max_unit = int(log($value)/log(1024));
74 $value /= 1024**($max_unit);
76 my $unit = $units[$max_unit];
77 return sprintf "%." . ($precision || 2) . "f $unit", $value;