import time
DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
def date2int(strdate):
result = 0
try:
tuple = time.strptime(strdate, DATE_FORMAT)
result = time.mktime(time.strptime(strdate, DATE_FORMAT))
except:
pass
return (result, tuple)
def correctForDaytime(tup):
lst = list(tup)
lst[3] -= tup[-1] # if this is zero, then this is a no op
lst[-1] = 0
return tuple(lst)
def int2date(intdate):
result = ''
if intdate > 0:
try:
tuple = time.localtime(intdate)
tuple = correctForDaytime(tuple) # commenting this line out
will show you the bug
result = time.strftime(DATE_FORMAT, tuple)
except:
pass
return (result, tuple)
def f(t1str):
(t1int, tup1) = date2int(t1str)
(t1str2, tup2) = int2date(t1int)
print t1str, "->", t1int
print t1int, "->", t1str2
if tup1 == tup2:
print "SAME", tup1, tup2
else:
print "DIFF", tup1, tup2
if t1str != t1str2:
print "FAILED", t1str, t1str2
else:
print "OK"
f("2003/01/15 11:15:00")
f("2003/02/15 11:15:00")
f("2003/03/15 11:15:00")
f("2003/04/15 11:15:00")
f("2003/05/15 11:15:00")
f("2003/06/15 11:15:00")
f("2003/07/15 11:15:00")
f("2003/08/15 11:15:00")
f("2003/09/15 11:15:00")
f("2003/10/15 11:15:00")
f("2003/11/15 11:15:00")
f("2003/12/15 11:15:00")
f("2003/01/03 11:15:00")
f("2003/02/03 11:15:00")
f("2003/03/03 11:15:00")
f("2003/04/03 11:15:00")
f("2003/05/03 11:15:00")
f("2003/06/03 11:15:00")
f("2003/07/03 11:15:00")
f("2003/08/03 11:15:00")
f("2003/09/03 11:15:00")
f("2003/10/03 11:15:00")
f("2003/11/03 11:15:00")
f("2003/12/03 11:15:00")
f("2003/01/27 11:15:00")
f("2003/02/27 11:15:00")
f("2003/03/27 11:15:00")
f("2003/04/27 11:15:00")
f("2003/05/27 11:15:00")
f("2003/06/27 11:15:00")
f("2003/07/27 11:15:00")
f("2003/08/27 11:15:00")
f("2003/09/27 11:15:00")
f("2003/10/27 11:15:00")
f("2003/11/27 11:15:00")
f("2003/12/27 11:15:00")
Larry Bates
=--------------------
"La Uruguaya" <lacuidada...@yahoo.com> wrote in message
news:4f705b08.0304...@posting.google.com...
Interesting: "During some times of the year ...", "... differed from the
original datetime by one hour." Sounds to me like you're running into
daylight savings time issues.
Yes, looking at your code confirms it: you're subtracting the value of
the last element of the tuple (the DST flag, which is 1 if daylight
savings time is in effect) from the third element (the hours).
Did you figure this out from looking at the docs for the time module, or
did you do it by trial and error? Also, it would have been nice if you
had explained more clearly that daylight savings time issues were the
root of your problem; at first glance, your code looked like a "magic"
solution to a strange kind of problem.
--
Robin Munn <rm...@pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838 50FF 2478 CFFB 081A 8338 54F7 845D ACFD 6AFB 6838