time, strptime, daylight saving

Oleg Broytmann phd at phd.russ.ru
Mon Dec 20 04:32:20 EST 1999


Hello!

   While writing and debugging programs to dump/load netscape history
files, I found a problem. I reduced it to the following program:

----------
import time


def test(dtime):
   t = time.strptime(dtime, "%Y-%m-%d %T")
   m = time.mktime(t)
   print "[DEBUG]", m, t
   print time.strftime("%Y-%m-%d %T (localtime)", time.localtime(m))
   print time.strftime("%Y-%m-%d %T (GMT)", time.gmtime(m))


test("1999-09-21 21:44:33")
test("1999-11-21 21:44:33")
----------

   On linux the program prints

-----
[DEBUG] 937935873.0 (1999, 9, 21, 21, 44, 33, 6, 1, 0)
1999-09-21 21:44:33 (localtime)
1999-09-21 17:44:33 (GMT)
[DEBUG] 943209873.0 (1999, 11, 21, 21, 44, 33, 6, 1, 0)
1999-11-21 21:44:33 (localtime)
1999-11-21 18:44:33 (GMT)
-----

   what seems pretty good. But soon I found the program prints incorrect
results on Solaris and FreeBSD. It prints

-----
1999-09-21 22:44:33 (localtime)
-----

   certailnly wrong.

   Deeper investigation shows that the problem is in daylight saving. The
following variant prints correct result on all platforms:

----------
import time


def test(dtime, x=0):
   t = list(time.strptime(dtime, "%Y-%m-%d %T"))
   t[8] = x
   m = time.mktime(t)
   print "[DEBUG]", m, t
   print time.strftime("%Y-%m-%d %T (localtime)", time.localtime(m))
   print time.strftime("%Y-%m-%d %T (GMT)", time.gmtime(m))


test("1999-09-21 21:44:33", 1)
test("1999-11-21 21:44:33")
----------

   Well, this show that on Solaris and FreeBSD mktime() correctly uses
is_dst flag; linux ignores it (but produces correct result). In some sense
FreeBSD and Solaris are doing more correct job.

   The question is simple - how can I ask time module whether any given
date is under daylight saving rule or not? How can I get the value for x
(in my second variant of the program)?

Oleg.
---- 
    Oleg Broytmann      Foundation for Effective Policies      phd at phd.russ.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list