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

I am running a script converting a file containing specific times associated with data from one format to another. In the process I check that there is an equal amount of minutes between the times, since the file I convert from is more or less created manually and thus prone to errors. A typical time interval would be 20 minutes.

To check the update rate of the time I use timelocal to convert the date and time to seconds and then compare the previous value with the current value, checking that it is whatever timeinterval I expected.

This works fine for every date and time it has been used on apart from the infamous date and time 31.10.2004 01:00 compared with 31.10.2004 00:40. The number returned from this comparison is 4800 seconds == 80 minutes.

I checked the actual number output from timelocal and indeed, the number returned from 31.10.2004 01:00 is 1099180800 whereas the number returned from 31.10.2004 00:40 is 1099176000 leaving a difference of 4800. When feeding those numbers back into localtime it reveals the correct dates and time (same as the date and time that created them in timelocal).

Ideas anyone ? As I was typing this in it struck me that it is perhaps related to some kind of daylight saving time or some other kind of computer time issue like offset from UTC or other silliness.

I am running on IBM AIX v4.3, Perl version: 5.005_03

Replies are listed 'Best First'.
Re: timelocal problems
by davorg (Chancellor) on Jul 07, 2004 at 08:02 UTC

    We need to see the code that you are using to get from your date/time values to the epoch seconds. By my calculations, '31.10.2004 00:40' is 1099179600 - which gives the expected difference of 1200.

    Of course, the difference between your answer and the expected answer is one hour, so daylight saving time could be an issue. Which timezone are you in, and when do you change out of daylight saving time? Maybe you could try using 'timegm' instead of 'timelocal' and see what difference that makes.

    Here's the code I used.

    #!/usr/bin/perl use Time::Local; my $t1 = '31.10.2004 01:00'; my $t2 = '31.10.2004 00:40'; $t1 = time2epoch($t1); $t2 = time2epoch($t2); print "$t1 - $t2 = ", $t1 - $t2, "\n"; sub time2epoch { my $t = shift; my ($d, $m, $y, $H, $M) = split /[\.\s:]/, $t; --$m; $y -= 1900; return timegm 0, $M, $H, $d, $m, $y; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: timelocal problems
by mpeppler (Vicar) on Jul 07, 2004 at 08:26 UTC
    You're hitting the "duplicated" hour in the DST -> normal conversion. In my locale (MET) I had to set the time to 02:00 to get the error, and use localtime() instead of gmtime() that daveorg used in his example.

    The duplicated hour in local time during the switch back to "normal" time (from DST) makes the conversion to UTC ambiguous during that period (once a year.)

    If you need to avoid that issue you should probably store all dates and times in UTC, and only convert to localtime on display...

    Michael

Re: timelocal problems
by gellyfish (Monsignor) on Jul 07, 2004 at 08:31 UTC

    In the GMT timezone the day light saving will have ended in that period. If you want to have consistent calculation do not use timelocal - which will take that into account but use timegm() instead.

    /J\

Re: timelocal problems
by neeraj (Scribe) on Jul 07, 2004 at 08:21 UTC
    Try this , it gave correct result of 20 minutes
    #! /usr/bin/perl use Date::Calc qw(Delta_DHMS); ($days, $hours, $minutes, $seconds) = Delta_DHMS( 2004,10,31,00,40,00, # earlier 2004,10,31,01,00,00); # later print "$days days and - $hours:$minutes:$seconds \n";
    Neeraj
Re: timelocal problems
by olecs (Scribe) on Jul 07, 2004 at 08:34 UTC
    Thank you everyone - I switched timelocal with timegm and it works nicely now.

    Ole C.