Menu

#29 reading SQL dates before the epoch creates strange results

closed-out-of-date
nobody
None
5
2014-08-17
2003-06-28
No

Insert* a date before 1970-01-01 00:00:00.000 GMT
into a database and try reading it from Python using the
odbc driver.

* This will probably require using something other than
Python. I haven't tested inserting such dates, only
reading them.

The bug probably exists because mktime returns -1 for
dates that it can't represent. I whipped up the following
untested patch which will turn all unrepresentable dates
into the epoch. This is much better than returning
random data from memory.

I don't know enough about the driver to suggest a
better way to do this.

diff -ur win32-orig/src/odbc.cpp win32/src/odbc.cpp
--- win32-orig/src/odbc.cpp 1997-06-13
10:37:06.000000000 -0500
+++ win32/src/odbc.cpp 2003-06-28
17:00:06.000000000 -0500
@@ -427,7 +427,11 @@
gt.tm_hour = dt->hour;
gt.tm_min = dt->minute;
gt.tm_sec = dt->second;
- return dbiMakeDate(PyInt_FromLong(mktime(&gt)));
+ time_t t = mktime(&gt);
+ // Set dates outside of time_t's range to the epoch.
+ if (t == (time_t)-1)
+ t = 0;
+ return dbiMakeDate(PyInt_FromLong(t)));
}

static PyObject *rawCopy(const void *v, int sz)

Discussion

  • Vernon Cole

    Vernon Cole - 2010-05-03

    Use adodbapi to return datetime.datetime values. Use of the old Python Time format is known to have difficulties and is now obsolete.

     
  • Vernon Cole

    Vernon Cole - 2010-05-03
    • status: open --> closed-out-of-date
     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.