Operating System - HP-UX
1846689 Members
3124 Online
110256 Solutions
New Discussion

daylight savings and localtime miscalculation

 
SOLVED
Go to solution
Roberto Martinez_2
Frequent Advisor

daylight savings and localtime miscalculation

Hi, does anybody ever used localtime for time calculations with DST? We have discovered that in our HP-UX 11.00 machine, when we add 1 second to "02:59:59" on the summer to winter change day, localtime returns incorrectly "03:00:00", instead of "02:00:00". Surprisingly, adding 3601 seconds to "01:59:59", the result is "02:00:00", which is correct. Weird...thanks in advance.
7 REPLIES 7
Ollie R
Respected Contributor
Solution

Re: daylight savings and localtime miscalculation

Hi Roberto,

This is the output I get on an 11.00 system:

$ perl -e 'print scalar localtime(1017536399)'
Sun Mar 31 01:59:59 2002[root@hpx47:/.root]
$ perl -e 'print scalar localtime(1017536400)'
Sun Mar 31 03:00:00 2002[root@hpx47:/.root]

$ perl -e 'print scalar localtime(1035680399)'
Sun Oct 27 02:59:59 2002[root@hpx47:/.root]
$ perl -e 'print scalar localtime(1035680400)'
Sun Oct 27 02:00:00 2002[root@hpx47:/.root]

Seems to be fine.

Exactly what commands are you running here?

Ollie.
To err is human but to not award points is unforgivable
Roberto Martinez_2
Frequent Advisor

Re: daylight savings and localtime miscalculation

try the same commands with this year's change in october (1067133599 for 26th october 03 at 02:59:59) and 1067133600) for 1 second later. I get "03:00:00", not "02:00:00". Thanks in advance.
Bill Hassell
Honored Contributor

Re: daylight savings and localtime miscalculation

What is $TZ set to? Calling localtime is just like the date command where the internal UTC (Zulu or GMT) time is translated using $TX to point to the rules found in /usr/lib/tztab. tztab can be customized and new timezone names and rules created bt the sysadmin. AS has always been the case, timezones have nothing to do with astronomy or science--they are always political which is why $TZ and tztab were created.


Bill Hassell, sysadmin
Adam J Markiewicz
Trusted Contributor

Re: daylight savings and localtime miscalculation

Hi


> man tztab
> echo $TZ
> more /usr/lib/tztab ; search for the definition of $TZ


and everything should become clear after.

Good luck
Adam
I do everything perfectly, except from my mistakes
Roberto Martinez_2
Frequent Advisor

Re: daylight savings and localtime miscalculation

ok, we're on the right path, but haven't still arrived... check the small program attached. it adds some seconds to a specified date. Apparently, localtime() doesn't seem to convert correctly on the first loop. Really weird. What am I doing wrong? Here's the output of the program on my machine:
------------------------

$TZ = MET-1METDST
From 26/10/03 02:59:56 , adding 0 to 7 seconds-------
20031026025956 + 0 sec = 2003102602595600 (1067133596) -> Sun Oct 26 02:59:56 2003
20031026025956 + 1 sec = 2003102602595700 (1067133597) -> Sun Oct 26 02:59:57 2003
20031026025956 + 2 sec = 2003102602595800 (1067133598) -> Sun Oct 26 02:59:58 2003
20031026025956 + 3 sec = 2003102602595900 (1067133599) -> Sun Oct 26 02:59:59 2003
20031026025956 + 4 sec = 2003102603000000 (1067133600) -> Sun Oct 26 03:00:00 2003
20031026025956 + 5 sec = 2003102603000100 (1067133601) -> Sun Oct 26 03:00:01 2003
20031026025956 + 6 sec = 2003102603000200 (1067133602) -> Sun Oct 26 03:00:02 2003
From 26/10/03 01:59:56 , adding 3600 to 3600 seconds-
20031026015956 + 3600 sec = 2003102602595601 (1067129996) -> Sun Oct 26 02:59:56 2003
20031026015956 + 3601 sec = 2003102602595701 (1067129997) -> Sun Oct 26 02:59:57 2003
20031026015956 + 3602 sec = 2003102602595801 (1067129998) -> Sun Oct 26 02:59:58 2003
20031026015956 + 3603 sec = 2003102602595901 (1067129999) -> Sun Oct 26 02:59:59 2003
20031026015956 + 3604 sec = 2003102602000000 (1067130000) -> Sun Oct 26 02:00:00 2003
20031026015956 + 3605 sec = 2003102602000100 (1067130001) -> Sun Oct 26 02:00:01 2003
20031026015956 + 3606 sec = 2003102602000200 (1067130002) -> Sun Oct 26 02:00:02 2003
Roberto Martinez_2
Frequent Advisor

Re: daylight savings and localtime miscalculation

I'm getting mad, could you please check the attached program and tell me what do you have? BTW, TZ=MET-1METSDT, and the corresponding paragraph in /usr/lib/tztab is:

# Middle European Time, Middle European Time Daylight Savings Time
MET-1METDST
0 3 25-31 3 1983-2038 0 METDST-2
0 2 24-30 9 1983-1995 0 MET-1
0 2 25-31 10 1996-2038 0 MET-1

Thank you very much in advance.

Roberto Martinez_2
Frequent Advisor

Re: daylight savings and localtime miscalculation

Got it! the problem was that, when I fill in the tm struct, I set tm_isdst=-1 because I don't neccessarily know whether or not the DST is active. The "wrong" behaviour of mktime()appears when it's passed a date which occurs twice the same year (any date within the 02:00 and 03:00 on the change date exists twice on the year), and -1 is set on tm_isdst. mktime() must choose a value for dst (0 or 1) because there are 2 instances of "right value". It chooses 0 and the problem appears.

Solution: set tm_isdst to 1 before calling mktime(), it will correct this value for itself. But beware!, doing this will affect the other date change...
thanks.