Added short form mode to humanise_seconds, and fixed 'ss' bug.

This commit is contained in:
Chris 2011-09-21 12:04:16 +01:00
parent 02caf90c4f
commit 37cc794892

View File

@ -563,16 +563,18 @@ sub bytes_to_human {
} }
## @fn $ humanise_seconds($seconds) ## @fn $ humanise_seconds($seconds, $short)
# Convert a number of seconds to days/hours/minutes/seconds. This will take # Convert a number of seconds to days/hours/minutes/seconds. This will take
# the specified number of seconds and output a string containing the number # the specified number of seconds and output a string containing the number
# of days, hours, minutes, and seconds it corresponds to. # of days, hours, minutes, and seconds it corresponds to.
# #
# @param seconds The number of seconds to convert. # @param seconds The number of seconds to convert.
# @param short If set, the generates string uses short forms of 'day', 'hour' etc.
# @return A string containing the seconds in a human readable form # @return A string containing the seconds in a human readable form
sub humanise_seconds { sub humanise_seconds {
my $self = shift; my $self = shift;
my $seconds = shift; my $seconds = shift;
my $short = shift;
my ($frac, $mins, $hours, $days); my ($frac, $mins, $hours, $days);
my $result = ""; my $result = "";
@ -583,22 +585,22 @@ sub humanise_seconds {
$seconds = $seconds % 60; $seconds = $seconds % 60;
if($days) { if($days) {
$result .= $days." day".($days > 1 ? "s" : ""); $result .= $days.($short ? "d" : " day").(!$short && $days > 1 ? "s" : "");
} }
if($hours) { if($hours) {
$result .= ", " if($result); $result .= ", " if($result);
$result .= $hours." hour".($hours > 1 ? "s" : ""); $result .= $hours.($short ? "h" : " hour").(!$short && $hours > 1 ? "s" : "");
} }
if($mins) { if($mins) {
$result .= ", " if($result); $result .= ", " if($result);
$result .= $mins." minute".($mins > 1 ? "s" : ""); $result .= $mins.($short ? "m" : " minute").(!$short && $mins > 1 ? "s" : "");
} }
if($seconds) { if($seconds) {
$result .= ", " if($result); $result .= ", " if($result);
$result .= $seconds.($frac ? ".$frac" : "")." seconds".($mins > 1 ? "s" : ""); $result .= $seconds.($frac ? ".$frac" : "").($short ? "s" : " second").(!$short && $mins > 1 ? "s" : "");
} }
return $result; return $result;