OCIDate getting mangled on the way into Oracle
I have some C code to populate an OCIDate
from the epoch time:
In my main program:
OCIDate ocidate;
epoch_to_ocidate(c.f, &ocidate);
And in a library:
void epoch_to_ocidate(double d, OCIDate* ocidate) {
time_t t = (time_t)d;
struct tm *ut = localtime(&t); /* convert to a Unix time */
OCIDateSetDate(ocidate, ut->tm_year + 1900, ut->tm_mon + 1, ut->tm_mday);
OCIDateSetTime(ocidate, ut->tm_hour + 1, ut->tm_min + 1, ut->tm_sec + 1);
}
I am pretty certain this is correct, because I have a check in the calling routine:
#ifdef DEBUG
char* fmt = "DD-MON-YYYY HH24:MI:SS开发者_开发问答";
ub4 dbufsize=255;
debug("testing converted OCIDate:");
OCIDateToText(h.err, (const OCIDate*)&ocidate, (text*)fmt, (ub1)strlen(fmt), (text*)0, (ub4)0, &dbufsize, (text*)dbuf);
debug(dbuf);
#endif
And I am binding it with:
OCIBindByPos(s, &bh, h.err, (ub4)p, (dvoid*)&ocidate, (sb4)sizeof(ocidate), SQLT_ODT, 0, 0, 0, 0, 0, OCI_DEFAULT);
(dbuf
is already defined). And that displays exactly what I would expect. But when it arrives in Oracle it's gibberish, resulting either in a nonsensical date (e.g. 65-JULY-7896 52:69:0
or a either ORA-1858 or ORA-1801). Has anyone seen anything like this before? Thanks!
Solved it - the problem was that ocidate
was stack allocated, and binding doesn't copy the value into the bindhandle, it merely sets a pointer, so when it went out of scope, that could have been pointing to anything. So I heap-allocated it instead. Now of course I have to bookkeep it, but I guess that's straightforward enough. Cheers!
精彩评论