CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Using Time::Seconds

 



tbone587
User

Dec 8, 2011, 7:49 AM

Post #1 of 5 (821 views)
Using Time::Seconds Can't Post

I was wondering if someone can point me in the right direction to take a large amount of seconds, and convert it to days, hours, minutes, seconds. I would think the Time::Seconds module can handle this request.

Example - If we have 1320167860 seconds, I want to format it in a matter like this: 1d 21h 25m 10s


BillKSmith
Veteran

Dec 8, 2011, 8:12 AM

Post #2 of 5 (819 views)
Re: [tbone587] Using Time::Seconds [In reply to] Can't Post

Time::Seconds is a good choice to extract the numbers. I would use sprintf (perldoc -f sprintf) to format the string. For an important program, it may be worth the effort to find a module to do that formatting. Perhaps someone else can provide a specific recommendation.
Good Luck,
Bill


tbone587
User

Dec 8, 2011, 10:30 AM

Post #3 of 5 (814 views)
Re: [BillKSmith] Using Time::Seconds [In reply to] Can't Post

I got something figured out in case it will be useful to others:


Code
my $second_input; 
my $days;
my $hours;
my $mins;
my $secs;
my $formatted_seconds;

system ("clear");


print "Please enter the seconds you want to convert to days, hours, mins, secs: ";
chomp ($second_input = <STDIN>);

$days = int($second_input/(24*60*60));

$hours = ($second_input/(60*60))%24;

$mins = ($second_input/60)%60;

$secs = $second_input%60;

$formatted_seconds = "${days}d ${hours}h ${mins}m ${secs}s";
print "$formatted_seconds\n";



BillKSmith
Veteran

Dec 10, 2011, 7:58 AM

Post #4 of 5 (786 views)
Re: [tbone587] Using Time::Seconds [In reply to] Can't Post

This is probably the way I would do it in code I am writing for my own use. However, it is worth the effort to find and use CPAN modules in more professional work because they are validated. (How certain are you that there are no special cases that your code will not do correctly?)
Good Luck,
Bill


tbone587
User

Dec 10, 2011, 8:06 AM

Post #5 of 5 (785 views)
Re: [BillKSmith] Using Time::Seconds [In reply to] Can't Post

I am not certain this will fail in weird scenarios, but It hasnt failed yet and has been heavily used since i deployed it..

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives