time, strptime, daylight saving 
Author Message
 time, strptime, daylight saving

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

           Programmers don't die, they just GOSUB without RETURN.



Fri, 07 Jun 2002 03:00:00 GMT  
 time, strptime, daylight saving
[... analysis snipped ...]

Quote:
>    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)?

OK ... I certainly learnt something out of that. Nice diagnostic work! :-)

To answer your question, if you pass a value of -1 as the last element of the
tuple to mktime(), it will fill in the right value from the system's time
zone. I guess that is what you want (use x = -1).

Cheers,
Malcolm Tredinnick

--
I intend to live forever -- so far, so good.



Fri, 07 Jun 2002 03:00:00 GMT  
 time, strptime, daylight saving

Quote:

> To answer your question, if you pass a value of -1 as the last element of the
> tuple to mktime(), it will fill in the right value from the system's time
> zone. I guess that is what you want (use x = -1).

   Yes, it seems to work for my simple tests! Nice, thank you.
   Will give it more tests in real programs...

Oleg.
----

           Programmers don't die, they just GOSUB without RETURN.



Fri, 07 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. time.daylight (Daylight Saving Time)

2. time zones, daylight saving time, and universal time

3. VisualWork, Time, and Daylight Savings TIme

4. Daylight saving Time issue...

5. Daylight Savings Time

6. Daylight saving time

7. gmt vs. labview daylight saving time XL

8. DayLight Saving Time in LabView

9. Daylight Saving Time flag in BIOS

10. gnat304 Daylight Saving Time problem (was: compiler errors in GNAT 304a (win95))

11. Disregard daylight saving time

12. Daylight Savings Time goof

 

 
Powered by phpBB® Forum Software