From 37cc79489238a61c72e13c5774a7def6a431c0b5 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 21 Sep 2011 12:04:16 +0100 Subject: [PATCH] Added short form mode to humanise_seconds, and fixed 'ss' bug. --- Template.pm | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Template.pm b/Template.pm index de67734..55d99eb 100644 --- a/Template.pm +++ b/Template.pm @@ -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 # the specified number of seconds and output a string containing the number # of days, hours, minutes, and seconds it corresponds to. # # @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 sub humanise_seconds { - my $self = shift; + my $self = shift; my $seconds = shift; + my $short = shift; my ($frac, $mins, $hours, $days); my $result = ""; @@ -583,22 +585,22 @@ sub humanise_seconds { $seconds = $seconds % 60; if($days) { - $result .= $days." day".($days > 1 ? "s" : ""); + $result .= $days.($short ? "d" : " day").(!$short && $days > 1 ? "s" : ""); } if($hours) { $result .= ", " if($result); - $result .= $hours." hour".($hours > 1 ? "s" : ""); + $result .= $hours.($short ? "h" : " hour").(!$short && $hours > 1 ? "s" : ""); } if($mins) { $result .= ", " if($result); - $result .= $mins." minute".($mins > 1 ? "s" : ""); + $result .= $mins.($short ? "m" : " minute").(!$short && $mins > 1 ? "s" : ""); } if($seconds) { $result .= ", " if($result); - $result .= $seconds.($frac ? ".$frac" : "")." seconds".($mins > 1 ? "s" : ""); + $result .= $seconds.($frac ? ".$frac" : "").($short ? "s" : " second").(!$short && $mins > 1 ? "s" : ""); } return $result;