Perlfunc: human_time()

This function will convert a time in seconds into a human readable time like "12 days 18 hours 22 minutes 33 seconds"

sub human_time {
    my $secs = shift();
    my $ret;

    if (int($secs / 31536000) > 0) { $ret .= int($secs / 31536000) . " years "; }
    if (int(($secs % 31536000) / 2628000) > 0) { $ret .= int(($secs % 31536000) / 2628000) . " months "; }
    if (int(($secs % 2628000) / 86400) > 0) { $ret .= int(($secs % 2628000) / 86400) . " days "; }
    if (int(($secs %  86400) / 3600) > 0) { $ret .= int(($secs %  86400) / 3600) . " hours "; }
    if (int(($secs %  3600) / 60) > 0) { $ret .= int(($secs %  3600) / 60) . " minutes "; }
    if (int($secs %  60) > 0) { $ret .= int($secs %  60) . " seconds "; }

    $ret =~ s/\s+$//;

    return $ret;
}
Leave A Reply
All content licensed under the Creative Commons License