Strange results when comparing dates

Grant Beasley gbeasley at tsa.ac.za
Wed Oct 31 02:46:17 EST 2001


Hi

I'm using the cx_Oracle module to access an Oracle database, in doing so,
I'm retrieving date time variables. According to the Python Database API, I
can use Timestamp to convert a time tuple (year, month, day, hour, minute,
second) to a date time type variable as used by the cx_Oracle module. This
works, and I can create these variables, but when comparing them I get VERY
strange results.

Using the same programme, with no reference to "outside influences", I get
different results on different runs.

Here's the programme:

import cx_Oracle
cdt1 = cx_Oracle.Timestamp(2001, 10, 25, 11, 12, 42)
cdt2 = cx_Oracle.Timestamp(2001, 10, 31, 9, 4, 43)
cdt3 = cx_Oracle.Timestamp(2001, 10, 31, 9,5,3)

print "Start\n"
print cdt1, ">", cdt2, "=", (cdt1 > cdt2)
print cdt2, ">", cdt3, "=", (cdt2 > cdt3)
print cdt3, ">", cdt1, "=", (cdt3 > cdt1)


And the Results of 3 runs (one after the other in pretty quick succession),
all different

Start

2001-10-25 11:12:42 > 2001-10-31 09:04:43 = 0
2001-10-31 09:04:43 > 2001-10-31 09:05:03 = 0
2001-10-31 09:05:03 > 2001-10-25 11:12:42 = 1
Start

2001-10-25 11:12:42 > 2001-10-31 09:04:43 = 1
2001-10-31 09:04:43 > 2001-10-31 09:05:03 = 0
2001-10-31 09:05:03 > 2001-10-25 11:12:42 = 0
Start

2001-10-25 11:12:42 > 2001-10-31 09:04:43 = 0
2001-10-31 09:04:43 > 2001-10-31 09:05:03 = 1
2001-10-31 09:05:03 > 2001-10-25 11:12:42 = 1

Am I doing something wrong, is this a bug with the cx_Oracle module that I
should report to the author, or is it a python problem? What's wrong, and
how can I fix it. I could try to convert the oracle datetime type to a
python / Unix date format, but that would involve extra work and I'm very
curious as to why the above doesn't.

If it's important, the way I'm using it in practice is selecting a date
field from the database, then using Timestamp to convert the current time
(using localtime and time) to an oracle datetime type and then comparing the
two variables.

Thanks
Grant Beasley





More information about the Python-list mailing list