Issue 10102: mktime adding an hour in April (naive struct)? (original) (raw)

Just starting to dabble in the world of time and datetime objects. I was converting an array of datetime objects to an array of floats via mktime to do some data analysis. I have ran into an issue where mktime seems to be adding an hour on April 6, 2003. Attached is an example script which I used to create the below output:

Python version: (2, 6, 5, 'final', 0) Epoch: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0) First datetime: 2003-04-06 02:58:17 Second datetime: 2003-04-06 03:13:17 Is first datetime prior to second?: True First datetime, time struct: time.struct_time(tm_year=2003, tm_mon=4, tm_mday=6, tm_hour=2, tm_min=58, tm_sec=17, tm_wday=6, tm_yday=96, tm_isdst=-1) Second datetime, time struct: time.struct_time(tm_year=2003, tm_mon=4, tm_mday=6, tm_hour=3, tm_min=13, tm_sec=17, tm_wday=6, tm_yday=96, tm_isdst=-1) Is first time struct prior to second?: True First timestamp generated by mktime: 1049623097.0 Second timestamp generated by mktime: 1049620397.0 Is first timestamp less than second timestamp?: False First recovered datetime: 2003-04-06 03:58:17 Second recovered datetime: 2003-04-06 03:13:17 Is first recovered prior to second recovered?: False Is first recovered datetime same as original?: False Is second recovered datetime same as original?: True

It looks like you have just discovered the joys of daylight saving time. The problem with your example is that datetime(2003,4,6,2,58,17) is not a valid time in your timezone (most likely you are in the US). In the US, prior to 2007, the clocks moved 1 hour ahead at 2AM on the first Sunday in April. An thus 1:59 AM was followed by 3:00 AM and 2:58:17 never occurred.

See the following link for detailed discussion of these issues:

http://docs.python.org/dev/py3k/library/datetime.html#tzinfo-objects