Skip to content

Instantly share code, notes, and snippets.

@solson
Created September 24, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solson/c28d547b5d3445f2a04cc5ed905dc099 to your computer and use it in GitHub Desktop.
Save solson/c28d547b5d3445f2a04cc5ed905dc099 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
sub format-duration(Int $milliseconds where * >= 0) returns Str {
my ($ms, $s, $m, $h, $d) = $milliseconds.polymod(1000, 60, 60, 24);
my $ms-hundreds = ($ms / 100).round;
if $d {
"{$d}d {$h}h {$m}m {$s}s"
} elsif $h {
"{$h}h {$m}m {$s}s"
} elsif $m {
"{$m}m {$s}s"
} else {
"{$s}.{$ms-hundreds}s"
}
}
sub MAIN(Int $milliseconds where * >= 0) {
say format-duration($milliseconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment