Or could I entitle this, "A Wrinkle in time.py," with apologies to Madeleine L'Engle. Either I've found a bug (or rather room for improvement) in time.py, or I *really* need a time module that doesn't assume so much.
After the suggestions to try setting the dst flag to zero, I did some more experimentation, and here is what I came up with: >>> import os >>> import time >>> os.environ['TZ'] = 'GMT' >>> time.tzset() >>> time.tzname ('GMT', 'GMT') >>> time.mktime((2007, 3, 11, 2, 30, 0, 6, 70, 0)) 1173580200.0 >>> time.mktime((2007, 3, 11, 3, 30, 0, 6, 70, 0)) 1173583800.0 OK, so far I'm seeing a one hour difference. >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1173580200.0)) '2007-03-11 02:30:00' >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1173583800.0)) '2007-03-11 03:30:00' Yay! it works! But, let's do this: >>> import time >>> time.tzname ('AKST', 'AKDT') >>> int(time.mktime(time.strptime('2007-03-11 2:30:00','%Y-%m-%d %H:%M:%S' [0:8] + (0,))) 1173612600 >>> int(time.mktime(time.strptime('2007-03-11 3:30:00','%Y-%m-%d %H:%M:%S' [0:8] + (0,))) 1173616200 >>> OK, that looks good, a one hour difference, and I told it not to figure in dst (the last 0). BUT: >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1173612600)[0:8] + (0,)) '2007-03-11 03:30:00' >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1173616200)[0:8] + (0,)) '2007-03-11 04:30:00' So, apparently localtime assumes DST (since we're in DST right now) and there is no way to tell it the unixtime given to it was calculated under the assumption of no daylight savings. And would this particular test case work if I told it to use DST? Yes, but then I'd have several hundred other test cases failing. I think localtime needs to have a parameter to tell it not to assume DST, even if DST would normally be active in that date range. So, at any rate, my fix/workaround is to manually set time.py to use UTC to convert to and from, and then deal with the times I have from there. All I wanted to do was convert an 18 or 19 byte character string (ISO date/time) to a four byte character string (key in a dict, can't use ints in a shelve db), and I end up spending HOURS trying to figure out 1) why I was getting duplicate keys, then 2) why mktime was generating duplicate unix times, and finally 3) why localtime wasn't converting back correctly. Sigh. Some days I hate DST. :) Thanks to all for the pointers and help. In other news, setting time.tzname = ('GMT', 'GMT') does nothing. You have to set the environment variable, then call tzset. Is there a rational for this? Seems odd. j -- Joshua Kugler Lead System Admin -- Senior Programmer http://www.eeinternet.com PGP Key: http://pgp.mit.edu/ ID 0xDB26D7CE -- http://mail.python.org/mailman/listinfo/python-list