Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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 ) )


Replies are listed 'Best First'.
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 kyle (Abbot) on Oct 25, 2007 at 15:12 UTC
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2018-02-20 14:44 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (271 votes). Check out past polls.