6 use POSIX
qw(strftime round);
14 render_fraction_as_percentage
18 sub render_timestamp
{
21 # ISO 8601 date format
22 return strftime
("%F %H:%M:%S", localtime($epoch));
25 sub render_timestamp_gmt
{
28 # ISO 8601 date format, standard Greenwich time zone
29 return strftime
("%F %H:%M:%S", gmtime($epoch));
33 my ($duration_in_seconds) = @_;
36 my $rest = round
($duration_in_seconds // 0);
38 return "0s" if !$rest;
41 my ($unit, $unitlength) = @_;
43 if ((my $v = int($rest/$unitlength)) > 0) {
44 $text .= " " if length($text);
45 $text .= "${v}${unit}";
46 $rest -= $v * $unitlength;
50 $step->('w', 7*24*3600);
51 $step->('d', 24*3600);
59 sub render_fraction_as_percentage
{
62 return sprintf("%.2f%%", $fraction*100);
66 my ($value, $precision) = @_;
68 my @units = qw(B KiB MiB GiB TiB PiB);
72 $max_unit = int(log($value)/log(1024));
73 $value /= 1024**($max_unit);
75 my $unit = $units[$max_unit];
76 return sprintf "%." . ($precision || 2) . "f $unit", $value;