Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Finding Time Difference of Localtime

by monkfan (Curate)
on Oct 25, 2007 at 15:08 UTC ( #647197=perlquestion: print w/ replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code that list down the starting time of each process:
use Data::Dumper; use Carp; use Proc::ProcessTable; use Time::Local::Extended qw(:ALL); $FORMAT = "%-6s %-8s %-24s \n"; $t = new Proc::ProcessTable; my $now = localtime; print "NOW: $now\n"; printf( $FORMAT, "PID", "START", "TIME_DIFF" ); foreach $p ( @{ $t->table } ) { my $start = $p->start; #my $diff = $start - $now; #doesn't seem to work my $diff = 1000; # this is just a dummy printf( $FORMAT, $p->pid, scalar( localtime( $p->start ) ), $diff ); }
My problem is how can I find/print out the time difference (in seconds) for starting time and current time? Namely the time difference of:
my $now = localtime;
scalar( localtime( $p->start ) )


Comment on Finding Time Difference of Localtime
Select or Download Code
Re: Finding Time Difference of Localtime
by kyle (Abbot) on Oct 25, 2007 at 15:12 UTC
Re: Finding Time Difference of Localtime
by Corion (Pope) on Oct 25, 2007 at 15:13 UTC

    Don't use localtime in the first place. Use time or gmtime and POSIX::strftime instead.

    use POSIX qw(strftime); my $start = gmtime(); sleep 10; my $end = gmtime(); my $duration = $end - $start; print "It took me $duration seconds\n"; my $start_date = strftime "%Y-%m-%d %H:%M:%S", $start; my $end_date = strftime "%Y-%m-%d %H:%M:%S", $end; print "I was started on $start_date and ended on $end_date\n";
Re: Finding Time Difference of Localtime
by lorn (Monk) on Oct 25, 2007 at 15:16 UTC
      That's exactly what I did just recently:
      use Time::HiRes; ... my $start = Time::HiRes::time(); ... do your job ... my $stop = Time::HiRes::time(); my $duration = $stop-$start;
      It gives you the difference with a lot more precision that whole seconds, but if that's what you care about it should eb easy to round the number.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://647197]
Approved by kyle
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-10-21 18:53 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (106 votes), past polls