Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert seconds to days, hours, minutes using Perl

Tags:

perl

I would like to take control of the output generated in seconds using perl. I would like to show the time (in seconds) in the form of days, hours, minutes and seconds to make it human readable format.

Code

my $start = time;
<some other perl codes>
my $duration = time - $start;
print "Total Execution time: $duration s\n";

Output is 311052 s

Required Output is 3 days 14 hours 24 minutes 12 seconds

Note: I am new to Perl. Before you flag this is as duplicate, I have done my research. I have gone through all the stackoverflow where similar solutions were discussed, none helped. My code is very small. What could be the few line changes that could be added.

like image 400
arka.b Avatar asked Sep 14 '15 11:09

arka.b


1 Answers

You could use the Time::Piece and Time::Seconds modules, which will do all of this for you

In this program I have simply added pi days onto the current date and time to get a representative duration. You will, of course, use an actual time

use strict;
use warnings;

use Time::Piece;
use Time::Seconds qw/ ONE_DAY /;

my $start = localtime;
my $end = $start + int(355/113 * ONE_DAY);

my $duration = ($end - $start)->pretty;
print "Total Execution time: $duration\n";

output

Total Execution time: 3 days, 3 hours, 23 minutes, 53 seconds
like image 62
Borodin Avatar answered Oct 12 '22 09:10

Borodin