Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Finding Time Difference of Localtime

by monkfan (Curate)
on Oct 25, 2007 at 15:08 UTC ( [id://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

Replies are listed 'Best First'.
Re: Finding Time Difference of Localtime
by Corion (Patriarch) on Oct 25, 2007 at 15:13 UTC

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

    #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); my $start = time(); sleep 10; my $end = time(); my $duration = $end - $start; print "It took me $duration seconds\n"; my $start_date = strftime "%Y-%m-%d %H:%M:%S", localtime($start); my $end_date = strftime "%Y-%m-%d %H:%M:%S", localtime($end); print "I was started on $start_date and ended on $end_date\n";

    Updated after Aldebaran actually checked the code.

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

      I just happened on to this and wonder if this is the behavior that Corion was expecting out of this. This isn't "my thread" or anything; I was just searching (again) for ways to time programs. I'm hoping to find a native one that works on Termux.

      $ ./1.strftime.pl Argument "Sun Dec 1 00:29:42 2019" isn't numeric in subtraction (-) a +t ./1.strftime.pl line 9. Argument "Sun Dec 1 00:29:52 2019" isn't numeric in subtraction (-) a +t ./1.strftime.pl line 9. It took me 0 seconds Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1 +, yday = -1, isdst = -1) at ./1.strftime.pl line 11. $ cat 1.strftime.pl #!/usr/bin/perl use strict; use warnings; 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"; $

        Whoops! Thank you for checking this, that's not what I want.

        You want to use time, not gmtime, and use strftime( localtime( $start )) to turn the seconds back into strings.

        I'll fix the above post.

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

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found