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

localtime hour count

by ady (Deacon)
on Apr 08, 2006 at 13:44 UTC ( [id://542039]=perlquestion: print w/replies, xml ) Need Help??

ady has asked for the wisdom of the Perl Monks concerning the following question:

How come it's ...
Elapsed: 1:0:5
and not
Elapsed: 0:0:5
??
#!/usr/bin/perl -w use strict; use warnings; my $t1 = time(); print scalar localtime,"\n"; sleep 5; my $t2 = time(); print scalar localtime,"\n"; my ($h,$m,$s) = (localtime($t2-$t1))[2,1,0]; print "Elapsed: $h:$m:$s\n"; __DATA__ Sat Apr 8 15:40:17 2006 Sat Apr 8 15:40:22 2006 Elapsed: 1:0:5

Replies are listed 'Best First'.
Re: localtime hour count
by vkon (Curate) on Apr 08, 2006 at 14:09 UTC
    I see following:
    coli ~ # perl t.pl Sat Apr 8 14:09:05 2006 Sat Apr 8 14:09:10 2006 Elapsed: 0:0:5
    but on another machine:
    D:\TESTS>perl 3 Sat Apr 8 18:10:21 2006 Sat Apr 8 18:10:26 2006 Elapsed: 3:0:5
    Obviously, this is just conversion of time onto your local timezone settings (mine is GMT+3, yours is GMT+1? CET?)
      yep, I'm i GMT+1, so i'll buy your explanation here
      Tnx! -- allan
      update:
      my ($h,$m,$s) = (gmtime($t2-$t1))[2,1,0];
      will DWIM -- allan

        This is still confusing durations with dates. See my post below.

        If the epoch was at 00:03:35 instead of 00:00:00 (and it might be on some scary operating system that someone just happenned to port perl to ;-) then your time delta would be much harder to explain =)

        -nuffin
        zz zZ Z Z #!perl
Re: localtime hour count
by Herkum (Parson) on Apr 08, 2006 at 15:04 UTC

    Testing out your code, this is what happens. You subtracting the number of seconds from epoc for $t1 from the number of seconds from epoc for $2.

    $t1 = 1000 $t2 = 1005 $sec_diff = $t2 - $t1; print "Time elapsed $sec_diff\n";

    When you call localtime it returning a date time format for the seconds for epoc based upon the time for WHERE YOU ARE!

    print "GMT time is " . gmtime($sec_diff); # Thu Jan 1 00:00:02 1970 print "Local time is " . localtime($sec_diff); # Thu Jan 1 19:00:02 1970 - for me at least.

    Note: I realized someone else had mentioned the gmtime but I just wanted to explain it further.

Re: localtime hour count
by rjsaulakh (Beadle) on Apr 08, 2006 at 14:31 UTC
    <i have tried something very rudimentary hope u can refine and work on it let me know if you have something better

    #!/usr/lib/perl -w use strict; use warnings; my $t1 = time(); print "$t1 \n"; sleep 5; my $t2 = time(); ; print ($t2) ; my $diff = $t2 - $t1; my $mDiff = int($diff / 60); my $sDiff = sprintf("%02d", $diff - 60 * $mDiff); print "diff = $diff -> $mDiff\:$sDiff\n";
      Thanks rjsaulakh
      See my update above for another fix of the issue.
      Best regards,
      allan
Re: localtime hour count
by nothingmuch (Priest) on Apr 09, 2006 at 10:09 UTC
    The problem is that when you subtract $t1 from $t2 you wind up with a number, that is incidentially both the duration that passed, in seconds, *and* a valid epoch number representing a certain date, in this case 5 seconds after the "epoch", which is January 1st, 1970 GMT on UNIX systems. An epoch date is the number of seconds that passed since the epoch (essentially also a form of a duration, but with a well defined base value).

    localtime accepts an epoch number representing a date and then calculates that date, converts it to the local time zone, and provides the separate fields (something like the Jan 1, 1970, 1:00:05, that is 5 seconds after the epoch, converted to your timezone). Even if you use gmtime this would still not make sense, because you are talking about a duration, and not a date.

    Use DateTime to subtract the two dates, play with duration sanely, and all that, or use simple modular arithmetics:

    my ( $h, $m, $s ) = do { use integer; ( ($tdelta / (60 * 60)) % 24, ($ +tdelta / 60) % 60, $tdelta % 60 ) };
    but note that this conversion is lossy.

    You can also use Time::Duration::Object, which is convenient for formatting durations for users as '2 hours, 5 minutes' or '2 minutes, 10 seconds', that is - rounded to a certain number of units.

    -nuffin
    zz zZ Z Z #!perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://542039]
Approved by Corion
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found