Unexpected results from localtime

Discussion in 'Perl Misc' started by Daniel, Nov 16, 2005.

  1. Daniel

    Daniel Guest

    I'm getting unexpected results from localtime. The runtime of my
    programm is about 4 seconds but I'm expecting more so I'm having it
    report how long it took. I reduced the code to the minimum and still
    get the unexpected result. Perhaps you can help. Here is the code:

    use strict;
    use warnings;
    my $now=time();
    print scalar localtime($now), "\n";
    sleep 4;
    print scalar localtime(time()-$now), "\n";

    That should result in printing todays date and time and then 4 seconds
    after midnight january 1st 1970. but the printout gives one hour more.

    Wed Nov 16 17:32:49 2005
    Thu Jan 1 01:00:04 1970

    It's a Windows XP Pro machine with perl 5.8.7 from ActiveState.

    I'm in Germany so I've got GMT+1, but that shouldn't show.

    So whats going on here?

    Daniel Cutter
    print chr--$g+ord for'KWVX%GUW]RP^-^Tb]2[UXa\j#'=~m$.$g;
     
    Daniel, Nov 16, 2005
    #1
    1. Advertisements

  2. Why not?
     
    Gunnar Hjalmarsson, Nov 16, 2005
    #2
    1. Advertisements

  3. print scalar localtime(time()-$now), "\n";
    Why are you even doing this? Why not just print out the elapsed number
    of seconds? Or use something like Time::Duration:

    use Time::Duration;

    my $start_time = time();
    sleep 4;
    print "Runtime: ", duration_exact(time() - $start_time), "\n";

    For a longer running process, this would print something like:

    Runtime: 2 hours, 25 minutes, and 44 seconds

    -- Lars
     
    Lars Kellogg-Stedman, Nov 16, 2005
    #3
  4. I'm in Germany so I've got GMT+1, but that shouldn't show.
    I expect two calls to time() within 4 seconds to both be within the same
    time zone. But I did mention the fact because it is, even if I can't see
    a connection, a possibility.

    Daniel Cutter

    print chr--$g+ord for'KWVX%GUW]RP^-^Tb]2[UXa\j#'=~m$.$g;
     
    Daniel Cutter, Nov 16, 2005
    #4
  5. Of course they are. What you are missing is the most basic of facts. The
    difference between the two calls to time is probably the number 4 which
    is four seconds after the epoch (probably 00:00:00 UTC, January 1,
    1970).

    What is the equivalent of that in your time zone? Why would you think,
    when the epoch is specified relative to a specific time zone, the
    interpretation of a delta from the epoch would not depend on the time
    zone in which localtime is called?

    If I remember the original issue, you are trying to report the run time
    of your program. Why not report the number of seconds it took, or the
    start versus end times? Why are you trying to interpret a time delta as
    a date?

    I am very puzzled.

    Sinan
     
    A. Sinan Unur, Nov 16, 2005
    #5
  6. print scalar localtime(time()-$now), "\n";
    Believe it or not: I do it that way because that is the way I've allways
    done it - on all platforms with all languages. Basicly you're doing the
    same in your example.

    Daniel Cutter

    print chr--$g+ord for'KWVX%GUW]RP^-^Tb]2[UXa\j#'=~m$.$g;
     
    Daniel Cutter, Nov 16, 2005
    #6
  7. So, you are saying that all your programs printed something like

    D:\Home> tt
    The program took: Wed Dec 31 19:00:04 1969

    "Elapsed time" is a change in time, not a given date.

    Sinan
     
    A. Sinan Unur, Nov 16, 2005
    #7
  8. allways done it - on all platforms with all languages. Basicly you're
    A superb programmer *and* a sense of humor. Excellent!

    -- Lars
     
    Lars Kellogg-Stedman, Nov 16, 2005
    #8
  9. Are you talking about me, or the OP?

    Consider using an unchanging identifier in the From: header, and use the
    expiring email address in the Reply-To: header only. That way, I can plonk
    you once and be done with it.

    Sinan
     
    A. Sinan Unur, Nov 16, 2005
    #9
  10. Daniel

    usenet Guest

    Perl keeps track of the time your program started in the variable $^T
    (or $BASETIME if you use::English).

    So you can simply do:

    #!/usr/bin/perl
    use strict; use warnings;

    sleep 4;
    print time - $^T;
    __END__
     
    usenet, Nov 17, 2005
    #10
  11. #!/usr/bin/perl
    Thanks everyone for your help. I had of course only used localtime()
    here for testing. I would have later replaced it with sprintf() to
    format it nicely in German. I'll use the suggested $^T instead of
    time(). That should then deliver the correct result.

    Daniel Cutter

    print chr--$g+ord for'KWVX%GUW]RP^-^Tb]2[UXa\j#'=~m$.$g;
     
    Daniel Cutter, Nov 17, 2005
    #11
  12. Daniel

    Brian Wakem Guest


    Measuring time in whole seconds is a bit crude if you ask me. I prefer
    greater precision.


    $ perl -MTime::HiRes=gettimeofday,sleep -e '$start =
    gettimeofday();sleep(rand);printf "%.3f seconds\n",gettimeofday()-$start'

    0.633 seconds
     
    Brian Wakem, Nov 17, 2005
    #12
  13. Daniel

    Bart Lateur Guest

    Don't depend on localtime(), that's much too coarse. Use the module
    Time::HiRes instead.

    use Time::HiRes 'time'; # overrides the builtin
    my @t = time;
    sleep 4;
    push @t, time;
    printf "My program took %.3f seconds to run.\n", $t[1]-$t[0];

    But it should. time==4 is for 4 seconds after midnight Jan 1st 1970, in
    GMT, *not* in your localtime.

    So the solution is simple: use gmtime(time()-$now) instead of localtime.

    BTW have a look at strftime() in the module POSIX, too. It's a
    formatting function, related to (s)printf, to format times and dates.
     
    Bart Lateur, Nov 18, 2005
    #13
  14. I can't resist mentioning that "$now" is an inappropriate
    choice of variable name for this.

    ("$then" ?).
     
    Alan J. Flavell, Nov 18, 2005
    #14
  15. Daniel

    Ingo Menger Guest

    You had the correct result already - the number 4 interpreted as that
    much seconds from Jan 1st 1970, 0am UTC (or 1. Januar 1970, 01:00:00
    CET), that is 01 Jan 1970 01:00:04 CET.
     
    Ingo Menger, Nov 18, 2005
    #15
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.