### Seconds conversion

by dda (Friar)
 on Jul 08, 2002 at 17:54 UTC Need Help??

dda has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I need to convert a value, given in seconds, to string like "1 day, 10 hours, 23 min, 44 sec". I wrote a simple script, but I'd like to check if there is any standard (?) ways to do it, or, at least, more elegant. Or, probably, the code has some bugs?

Code:

use strict; my \$idle = 1332443; my (\$days, \$hours, \$min, \$sec); \$days = round(\$idle/86400); \$idle -= \$days*86400; \$hours = round(\$idle/3600); \$idle -= \$hours*3600; \$min = round(\$idle/60); \$idle -= \$min*60; \$sec = \$idle; print "\$days days, \$hours hours, \$min min, \$sec sec\n"; sub round { my \$value = \$_[0]; return (sprintf('%.0f', 2 * \$value / 2)); }

Thanks.
--dda

Replies are listed 'Best First'.
Re: Seconds conversion
by Albannach (Prior) on Jul 08, 2002 at 18:36 UTC
Time::Duration is nice for this:
>perl -MTime::Duration -e "print duration_exact(1332443)" 15 days, 10 hours, 7 minutes, and 23 seconds
But even nicer in that it will supress the least significant units for larger durations:
>perl -MTime::Duration -e "print duration(1332443)" 15 days and 10 hours
If you'd rather do it yourself, you could avoid all the sprintfs with something like:
my \$d = int( \$seconds / 86400 ); my \$h = int( (\$seconds - \$d*86400) / 3600 ); my \$m = int( (\$seconds - \$d*86400 - \$h*3600) / 60 ); my \$s = \$seconds % 60; print "\$d days, \$h hours, \$m min, \$s sec\n";
and if you want to be neat you can always fix the pluralization with a few bits like my \$day_string = \$d > 1 ? 'days' : 'day';

--
I'd like to be able to assign to an luser

•Re: Seconds conversion
by merlyn (Sage) on Jul 08, 2002 at 18:40 UTC
Re: Seconds conversion
by ckohl1 (Hermit) on Jul 08, 2002 at 18:15 UTC
I am not sure about a standard, but this idea may work:
#!/usr/bin/perl use strict; print "\${\SecondsToTime(22)}\n"; print "\${\SecondsToTime(220)}\n"; print "\${\SecondsToTime(2200)}\n"; print "\${\SecondsToTime(22000)}\n"; exit; sub SecondsToTime { # Takes a number of seconds, and creates a string in the 'dd hh:mm +:ss' format. my ( \$time ) = shift; \$time = int( \$time ); my \$newTime = sprintf( "%.2d ", int(\$time / 86400) ); \$newTime .= sprintf( "%.2d", int((\$time % 86400) / 3600) ); \$newTime .= ':' . sprintf( "%.2d", int((\$time % 3600) / 60) ); \$newTime .= ':' . sprintf( "%.2d", int((\$time % 3600) % 60) ); return ( \$newTime ); }

Chris
Re: Seconds conversion
by Matts (Deacon) on Jul 09, 2002 at 09:21 UTC
Use Time::Seconds from the Time::Piece distribution. It allows you to do:

my \$secs = Time::Seconds->new(1332443); printf "%d days, %d hours, %d min, %d sec\n", \$secs->days, \$secs->hours, \$secs->mins, \$secs->secs;
Re: Seconds conversion
by dda (Friar) on Jul 09, 2002 at 10:27 UTC
You guys are great! Thanks a lot.

--dda

Create A New User
Node Status?
node history
Node Type: perlquestion [id://180252]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2020-11-26 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?