Bug in timelocal?

Discussion in 'Perl Misc' started by yocoyote, Apr 6, 2005.

  1. yocoyote

    yocoyote Guest

    I'm writing scripts which make heavy use of the timelocal() fct.
    (Time::Local) to get epochtime from a MM/DD hh:mm:ss text date. I'm
    using activestate perl 5.8.4 build 810 on my pc. I've found one date,
    among the 10,000's I've successfully transformed, is offset by 3600
    sec. The 3600 (=1 hour) struck me as not likely a conincidence. I'm
    wondering if others have seen this before.

    syntax:

    use Time::Local
    $epochTime = timelocal($sec, $min, $hour, $day, $month - 1);

    Anyone seen instances in which a date, seemingly at random, has 3600
    added or subtracted from the correct output of timelocal?

    thanks
    yocoyote
    yocoyote, Apr 6, 2005
    #1
    1. Advertising

  2. yocoyote

    Mothra Guest

    "yocoyote" <> wrote in message
    news:...
    (snipped)
    > using activestate perl 5.8.4 build 810 on my pc. I've found one date,


    Which one? what was the date?

    > among the 10,000's I've successfully transformed, is offset by 3600
    > sec. The 3600 (=1 hour) struck me as not likely a conincidence. I'm
    > wondering if others have seen this before.

    Mothra
    Mothra, Apr 6, 2005
    #2
    1. Advertising

  3. yocoyote

    yocoyote Guest

    Here are the dates: the diff is negative, which highlighted the issue
    $MonthSTART = 04, $DaySTART = 02, $HourSTART = 02, $MinSTART = 47,
    $SecSTART = 07
    (04/02 02:47:07)
    $epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    $DaySTART, $MonthSTART-1);
    I get $epochTimeSTART = 954672427
    ---
    $MonthSTOP = 04, $DaySTOP = 02, $HourSTOP = 03, $MinSTOP = 09, $SecSTOP
    = 08
    (04/02 03:09:08)
    $epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    $DaySTART, $MonthSTART-1);
    I get $epochTimeSTOP = 954670148

    So the calculated diff ($epochTimeSTOP - $epochTimeSTART) = -2279
    The correct diff (I did by hand) = 1321

    Note that 2279 + 1321 = 3600 !

    So either the $epochTimeSTART was shifted up an hour or $epochTimeSTOP
    was shifted back an hour. These dates are close to daylight savings
    time change in PST (where I'm at) but off by about 23 hrs.
    yocoyote, Apr 6, 2005
    #3
  4. yocoyote wrote:

    > I'm writing scripts which make heavy use of the timelocal() fct.
    > (Time::Local) to get epochtime from a MM/DD hh:mm:ss text date. I'm
    > using activestate perl 5.8.4 build 810 on my pc. I've found one date,
    > among the 10,000's I've successfully transformed, is offset by 3600
    > sec. The 3600 (=1 hour) struck me as not likely a conincidence. I'm
    > wondering if others have seen this before.


    Was this is the week following the DST transion perhaps?

    I discovered last week that on some versions of the Win32 OS there's a
    but that sometimes causes it to report the wrong local time to some
    applications in the week following the DST transition.

    (Note: I didn't come across this in Perl but in another language).

    > Anyone seen instances in which a date, seemingly at random, has 3600
    > added or subtracted from the correct output of timelocal?


    I can't recall the details but I did many years ago note that
    Time::Local's algorithm was flawed. I don't know if it was ever fixed.
    Brian McCauley, Apr 6, 2005
    #4
  5. yocoyote

    Keith Keller Guest

    On 2005-04-06, yocoyote <> wrote:
    > $MonthSTART = 04, $DaySTART = 02, $HourSTART = 02, $MinSTART = 47,
    > $SecSTART = 07
    > (04/02 02:47:07)
    > $epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    > $DaySTART, $MonthSTART-1);
    > I get $epochTimeSTART = 954672427
    > ---
    > $MonthSTOP = 04, $DaySTOP = 02, $HourSTOP = 03, $MinSTOP = 09, $SecSTOP
    >= 08
    > (04/02 03:09:08)
    > $epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    > $DaySTART, $MonthSTART-1);
    > I get $epochTimeSTOP = 954670148
    >
    > So the calculated diff ($epochTimeSTOP - $epochTimeSTART) = -2279


    Please post a complete, working Perl script. The above makes people
    edit your code by hand in order to even start to help you.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
    Keith Keller, Apr 6, 2005
    #5
  6. yocoyote

    Mothra Guest

    "yocoyote" <> wrote in message
    news:...
    > So either the $epochTimeSTART was shifted up an hour or $epochTimeSTOP
    > was shifted back an hour. These dates are close to daylight savings
    > time change in PST (where I'm at) but off by about 23 hrs.


    I was unable to reproduce your problem.

    use strict;
    use warnings;
    use Time::Local;

    my @start = qw(07 47 02 02 03);
    my @end = qw(08 09 03 02 03);

    my $estart = timelocal(@start);
    my $estop = timelocal(@end);

    print "start time is: $estart\n";
    print "End time is: $estop\n";

    my $diff = $estop - $estart;

    print "the difference is: $diff\n";


    output is
    F:\scripts>me.pl
    start time is: 954672427
    End time is: 954673748
    the difference is: 1321
    Mothra, Apr 6, 2005
    #6
  7. In article <>,
    yocoyote <> wrote:
    >Here are the dates: the diff is negative, which highlighted the issue
    >$MonthSTART = 04, $DaySTART = 02, $HourSTART = 02, $MinSTART = 47,
    >$SecSTART = 07
    >(04/02 02:47:07)
    >$epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    >$DaySTART, $MonthSTART-1);
    >I get $epochTimeSTART = 954672427
    >---
    >$MonthSTOP = 04, $DaySTOP = 02, $HourSTOP = 03, $MinSTOP = 09, $SecSTOP
    >= 08
    >(04/02 03:09:08)
    >$epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    >$DaySTART, $MonthSTART-1);
    >I get $epochTimeSTOP = 954670148
    >
    >So the calculated diff ($epochTimeSTOP - $epochTimeSTART) = -2279
    >The correct diff (I did by hand) = 1321
    >
    >Note that 2279 + 1321 = 3600 !
    >
    >So either the $epochTimeSTART was shifted up an hour or $epochTimeSTOP
    >was shifted back an hour. These dates are close to daylight savings
    >time change in PST (where I'm at) but off by about 23 hrs.


    This might help clear up your confusion:
    print scalar localtime $epochTimeSTART;

    Why do you not specify a year in your call to timelocal()? Do you
    know what timelocal() uses when you don't specify a year?

    Yes, it is a DST time changeover issue. No, it's not a bug.
    "If the timelocal() function is given a non-existent local time,
    it will simply return an epoch value for the time one hour later."

    Gary Ansok
    --
    It's depressing that the words "secret agent"
    have become synonymous with "sex maniac."
    -- Sir James Bond, _Casino Royale_
    Gary E. Ansok, Apr 6, 2005
    #7
  8. In article <d316bj$em7$>,
    Gary E. Ansok <> wrote:
    >In article <>,
    >yocoyote <> wrote:
    >>Here are the dates: the diff is negative, which highlighted the issue
    >>$MonthSTART = 04, $DaySTART = 02, $HourSTART = 02, $MinSTART = 47,
    >>$SecSTART = 07
    >>(04/02 02:47:07)
    >>$epochTimeSTART = timelocal($SecSTART, $MinSTART, $HourSTART,
    >>$DaySTART, $MonthSTART-1);
    >>I get $epochTimeSTART = 954672427
    >>So either the $epochTimeSTART was shifted up an hour or $epochTimeSTOP
    >>was shifted back an hour. These dates are close to daylight savings
    >>time change in PST (where I'm at) but off by about 23 hrs.

    >
    >This might help clear up your confusion:
    >print scalar localtime $epochTimeSTART;
    >
    >Why do you not specify a year in your call to timelocal()? Do you
    >know what timelocal() uses when you don't specify a year?


    I might add that if you had had Perl's warnings turned on, you would
    have gotten messages that might have led you to the correct answer
    without needing outside help.

    Gary
    --
    New Year's Resolution: I will not sphroxify gullible people into looking up
    fictitious words in the dictionary. (Chuck Robey)
    Gary E. Ansok, Apr 6, 2005
    #8
  9. yocoyote

    yocoyote Guest

    Thank you. Thank you.

    By explicitly adding the year to the timelocal() call, I get the
    correct value of 1321.

    I am still trying to reconcile some of the comments above (clearly
    accurate) with the details of how this occurred, i.e., early on a Sat
    morning (not Sun) and the time ($epochStopTime was affected) seemed to
    move back, rather than forward ("spring forward")

    -yocoyote
    yocoyote, Apr 6, 2005
    #9
  10. yocoyote

    yocoyote Guest

    definitely good input and consistent with all the books i've read.
    what i don't understand is which i didnt get the warnings because i had
    "use warnings;" at the top of my code.
    i get no warnings with or without the -c at the command prompt ??

    at any rate, thanks again
    yocoyote, Apr 6, 2005
    #10
  11. yocoyote

    brian d foy Guest

    In article <>,
    yocoyote <> wrote:

    > Here are the dates: the diff is negative, which highlighted the issue
    > $MonthSTART = 04, $DaySTART = 02, $HourSTART = 02, $MinSTART = 47,
    > $SecSTART = 07


    You're missing an hour on the day that daylight savings starts?
    That's odd.

    --
    brian d foy,
    Subscribe to The Perl Review: http://www.theperlreview.com
    brian d foy, Apr 6, 2005
    #11
  12. In article <>,
    yocoyote <> wrote:
    >Thank you. Thank you.
    >
    >By explicitly adding the year to the timelocal() call, I get the
    >correct value of 1321.
    >
    >I am still trying to reconcile some of the comments above (clearly
    >accurate) with the details of how this occurred, i.e., early on a Sat
    >morning (not Sun) and the time ($epochStopTime was affected) seemed to
    >move back, rather than forward ("spring forward")


    Did you try the line I suggested:

    print scalar localtime $epochTimeSTART;

    I get
    Sun Apr 2 03:09:08 2000

    If you don't supply a year to timelocal(), it gets undef as the
    year value. When that is used numerically, it becomes a 0.
    If you read the docs for Time::Local, you'll find that 0 is
    treated as 2000 (until 2050, when 0 will be treated as 2100).

    While it might be useful in some cases for timelocal() to assume
    the current year if none is supplied, it doesn't. You could write
    to the maintainer and suggest the behavior you want.

    Gary
    --
    The recipe says "toss lightly," but I suppose that depends
    on how much you eat and how bad the cramps get.
    Gary E. Ansok, Apr 6, 2005
    #12
  13. In article <>,
    yocoyote <> wrote:
    >definitely good input and consistent with all the books i've read.
    >what i don't understand is which i didnt get the warnings because i had
    >"use warnings;" at the top of my code.
    >i get no warnings with or without the -c at the command prompt ??


    Ah. Since I was running this as a quick one-liner at the command prompt,
    I used -w (which turns on warnings everywhere). You used "use warnings",
    which turns on warnings in the file or block where it appears.

    These warnings are generated within Time::Local, which explains why I
    saw them and you didn't.

    Both have advantages and disadvantages, but "use warnings" is probably
    the better way to go overall.

    Gary
    --
    Quidquid latine dictum sit, altum viditur.
    Whatever is said in Latin sounds profound.
    Gary E. Ansok, Apr 6, 2005
    #13
  14. yocoyote

    Bart Lateur Guest

    yocoyote wrote:

    >I'm writing scripts which make heavy use of the timelocal() fct.
    >(Time::Local) to get epochtime from a MM/DD hh:mm:ss text date. I'm
    >using activestate perl 5.8.4 build 810 on my pc. I've found one date,
    >among the 10,000's I've successfully transformed, is offset by 3600
    >sec. The 3600 (=1 hour) struck me as not likely a conincidence. I'm
    >wondering if others have seen this before.


    Daylight Savings Time. It must be the difference in start/end dates,
    between the DST in 1970, and this year.

    Use gmtime()/timegm(), and you won't have that.

    --
    Bart.
    Bart Lateur, Apr 7, 2005
    #14
  15. yocoyote

    Joe Smith Guest

    brian d foy wrote:
    > In article <>,
    > yocoyote <> wrote:
    >
    >>Here are the dates: the diff is negative, which highlighted the issue
    >>$MonthSTART = 04, $DaySTART = 02, $HourSTART = 02, $MinSTART = 47,
    >>$SecSTART = 07

    >
    > You're missing an hour on the day that daylight savings starts?
    > That's odd.


    Actually, missing an hour on the day that daylight savings started
    four years ago, which is a different day of the month than this year.
    Joe Smith, Apr 7, 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. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    549
  2. Replies:
    16
    Views:
    179
    robic0
    Nov 16, 2005
  3. J Moreno
    Replies:
    14
    Views:
    245
    J Moreno
    Mar 9, 2006
  4. Datamon
    Replies:
    1
    Views:
    97
  5. John
    Replies:
    2
    Views:
    108
Loading...

Share This Page