Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CPAN module for converting seconds to English?

Is there a CPAN module that can convert a number of seconds to a human-readable English description of the interval?

secToEng( 125 ); # => 2 min 5 sec
secToEng( 129_600 ); # => 1 day 12 h

The format isn't important as long as it's human-readable.

I know that it would be trivial to implement.

like image 935
Tim Avatar asked Sep 25 '11 13:09

Tim


1 Answers

It turns out that Time::Duration does exactly this.

$ perl -MTime::Duration -E 'say duration(125)'
2 minutes and 5 seconds
$ perl -MTime::Duration -E 'say duration(129_700)'
1 day and 12 hours

From the synopsis:

Time::Duration - rounded or exact English expression of durations

Example use in a program that ends by noting its runtime:

my $start_time = time();
use Time::Duration;
# then things that take all that time, and then ends:
print "Runtime ", duration(time() - $start_time), ".\n";

Example use in a program that reports age of a file:

use Time::Duration;
my $file = 'that_file';
my $age = $^T - (stat($file))[9];  # 9 = modtime
print "$file was modified ", ago($age);
like image 160
Tim Avatar answered Sep 19 '22 14:09

Tim