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. Advertising

  2. Daniel wrote:
    > the printout gives one hour more.


    <snip>

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


    Why not?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 16, 2005
    #2
    1. Advertising

  3. > 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.


    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 <>
    This email address will expire on 2005-11-23.
    Lars Kellogg-Stedman, Nov 16, 2005
    #3
  4. > > I'm in Germany so I've got GMT+1, but that shouldn't show.
    >
    > Why not?


    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. Daniel Cutter <> wrote in
    news:dlg5qg$vq$:

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

    > >
    > > Why not?

    >
    > I expect two calls to time() within 4 seconds to both be within the
    > same time zone.


    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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 16, 2005
    #5
  6. >>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.

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


    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. Daniel Cutter <> wrote in
    news:dlg6cc$27p$:

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

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

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


    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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 16, 2005
    #7
  8. >> allways done it - on all platforms with all languages. Basicly you're
    >> doing the same in your example.


    A superb programmer *and* a sense of humor. Excellent!

    -- Lars

    --
    Lars Kellogg-Stedman <>
    This email address will expire on 2005-11-23.
    Lars Kellogg-Stedman, Nov 16, 2005
    #8
  9. Lars Kellogg-Stedman <> wrote in
    news::

    >>> allways done it - on all platforms with all languages. Basicly you're
    >>> doing the same in your example.

    >
    > A superb programmer *and* a sense of humor. Excellent!


    Are you talking about me, or the OP?

    > --
    > Lars Kellogg-Stedman <>
    > This email address will expire on 2005-11-23.


    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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 16, 2005
    #9
  10. Daniel

    Guest

    Daniel wrote:
    > I'm getting unexpected results from localtime. The runtime of my
    > programm is about 4 seconds...


    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__
    , Nov 17, 2005
    #10
  11. > #!/usr/bin/perl
    > use strict; use warnings;
    >
    > sleep 4;
    > print time - $^T;
    > __END__


    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

    Daniel Cutter wrote:

    >> #!/usr/bin/perl
    >> use strict; use warnings;
    >>
    >> sleep 4;
    >> print time - $^T;
    >> __END__

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



    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
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Wakem, Nov 17, 2005
    #12
  13. Daniel

    Bart Lateur Guest

    Daniel wrote:

    >The runtime of my
    >programm is about 4 seconds but I'm expecting more so I'm having it
    >report how long it took.


    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];


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


    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.
    Bart Lateur, Nov 18, 2005
    #13
  14. On Fri, 18 Nov 2005, Bart Lateur wrote:

    > Daniel wrote:

    [...]
    > >my $now=time();
    > >print scalar localtime($now), "\n";


    [...]

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


    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

    Daniel Cutter schrieb:

    > > #!/usr/bin/perl
    > > use strict; use warnings;
    > >
    > > sleep 4;
    > > print time - $^T;
    > > __END__

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


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Lander

    Re: unexpected results

    Scott Lander, Jul 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,721
    Scott Lander
    Jul 7, 2003
  2. Dave
    Replies:
    1
    Views:
    330
    Leor Zolman
    Apr 8, 2004
  3. =?Utf-8?B?QXJ0?=

    Page inherting from .master - unexpected results

    =?Utf-8?B?QXJ0?=, May 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    348
    =?Utf-8?B?QXJ0?=
    May 26, 2006
  4. CK
    Replies:
    3
    Views:
    312
  5. Steven D'Aprano

    Unexpected timing results

    Steven D'Aprano, Feb 23, 2006, in forum: Python
    Replies:
    7
    Views:
    385
    Steven D'Aprano
    Feb 24, 2006
Loading...

Share This Page