Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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:

Hi,
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;
and
scalar( localtime( $p->start ) )

Regards,
Edward

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-19 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (149 votes), past polls