Fix for datetime conversion to integer and back to string; was 1 hour off

Larry Bates lbates at swamisoft.com
Mon Apr 28 14:53:12 EDT 2003


mx.DateTime has function DateTimeFromCOMDate to
go from COM date (seconds since 1970) to mx.DateTime
instance.  DateTime class has a method COMDate to go
the other direction.

Larry Bates

=--------------------

"La Uruguaya" <lacuidadanadelmundo at yahoo.com> wrote in message
news:4f705b08.0304281031.ddd8f75 at posting.google.com...
> I searched this group for a solution to a problem I had converting
> dates from a string format to integer (in seconds since 1970) and back
> to strings.  During some times of the year, the conversion didn't do
> a perfect round trip and I ended up with a datetime that differed from
> the original datetime by one hour.  I found 2 postings with variations
> on the problem, but not a solution.  I implemented the following
> solution and wanted to share it in case somebody needs it later.
>
>
> 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")






More information about the Python-list mailing list