By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

odbc DbiDate date conversion

P: n/a
I'm using a solid DB and i'm accessing it via the odbc module
(activepython).
I get a DbiDate object returned but i don't find a way to decently print
it or get a format like %d/%m%/%y.

I found a few posts but the code doesn't work.
>>birthd = results[0][4] #info from db
birthd
<DbiDate object at 0x0087E040>
>>str(birthd)
'e\x00\x00d\x00\x00\x19d\x01\x00\x19Z\x01\x00d\x02 \x00S\x00[4]\n\x00'
>>t = time.strftime('%d %B %Y', birthd)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: argument must be 9-item sequence, not DbiDate

How can i convert this DbiDate to a string formated like '%d %B %Y'

I look at the DB API 2.0 but couldn't find the relevant info.

Thanks,
Benedict
Sep 26 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

flupke wrote:
I'm using a solid DB and i'm accessing it via the odbc module
(activepython).
I get a DbiDate object returned but i don't find a way to decently print
it or get a format like %d/%m%/%y.
I convert it to a datetime() instance, like this -

mydate = datetime.datetime.fromtimestamp(int(dbidate))

Then I can use all the functionality of the datetime module.

HTH

Frank Millman

Sep 26 '06 #2

P: n/a
Frank Millman schreef:
flupke wrote:
>I'm using a solid DB and i'm accessing it via the odbc module
(activepython).
I get a DbiDate object returned but i don't find a way to decently print
it or get a format like %d/%m%/%y.

I convert it to a datetime() instance, like this -

mydate = datetime.datetime.fromtimestamp(int(dbidate))

Then I can use all the functionality of the datetime module.

HTH

Frank Millman
Hi Frank,

i tried it and i end up with this:
>>mydate = datetime.datetime.fromtimestamp(int(birthd))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: timestamp out of range for platform localtime()/gmtime()
function

Regards,
Benedict
Sep 26 '06 #3

P: n/a

flupke wrote:
Frank Millman schreef:
flupke wrote:
I'm using a solid DB and i'm accessing it via the odbc module
(activepython).
I get a DbiDate object returned but i don't find a way to decently print
it or get a format like %d/%m%/%y.
I convert it to a datetime() instance, like this -

mydate = datetime.datetime.fromtimestamp(int(dbidate))

Then I can use all the functionality of the datetime module.

HTH

Frank Millman

Hi Frank,

i tried it and i end up with this:
>mydate = datetime.datetime.fromtimestamp(int(birthd))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: timestamp out of range for platform localtime()/gmtime()
function
Well waddyaknow - I get exactly the same, for dates earlier than
1970-01-02. Thanks for finding a bug that would have bitten me sooner
or later.

I will do some investigation. If I find an answer I will post it here,
unless some kind soul saves me the trouble and beats me to it.

Frank

Sep 26 '06 #4

P: n/a
flupke schreef:
<snip>
When i do the same sql from a client, i get this: 1961-02-15
Seems ok.
If i check the catalog, the native field type is listed as 10 positions
and of type date.

Yet when i print it after i got the values i get this which looks very
weird (print "value ",str(i)," type ",type(i)):

value "main_kamer"."nummer" m" type <type 'DbiDate'>

The type is ok but the result of str(i) looks wrong.
Or is it due to conversion by str?

Regards,
Benedict
Sep 26 '06 #5

P: n/a
Frank Millman schreef:
<snip>
Well waddyaknow - I get exactly the same, for dates earlier than
1970-01-02. Thanks for finding a bug that would have bitten me sooner
or later.

I will do some investigation. If I find an answer I will post it here,
unless some kind soul saves me the trouble and beats me to it.

Frank
Thanks for investigating it so far Frank.

Is there a workaround to parse that date and get the date info that i want?

Regards,
Benedict
Sep 26 '06 #6

P: n/a

flupke wrote:
Frank Millman schreef:
<snip>
Well waddyaknow - I get exactly the same, for dates earlier than
1970-01-02. Thanks for finding a bug that would have bitten me sooner
or later.

I will do some investigation. If I find an answer I will post it here,
unless some kind soul saves me the trouble and beats me to it.

Frank

Thanks for investigating it so far Frank.

Is there a workaround to parse that date and get the date info that i want?
Not that I know of. The results of my investigations so far seem to
indicate that we have a problem :-(

Here is a link to an article dated 1998 -
https://svn.python.org/www/trunk/pyd...OdbcHints.html

Among other interesting stuff, it states -

"Notice that result values are converted to Python objects. Dates in
particular are returned as dbiDate objects. This can be a serious
limitation, because dbiDate can not represent dates prior to the UNIX
epoch (1 Jan 1970 00:00:00 GMT). If you try to retrieve earlier dates,
you'll get garbage and may even provoke a crash."

I contacted Mark Hammond, author of the win32 extensions, to ask if
there was a solution and particularly to suggest a modification to
return a datetime.datetime object. This was his reply -

"I'd be happy with an option to use the datetime module - maybe it
could even be on the cursor? However, I wont have time to do this in
the short term.
You could consider using ADO via win32com too..."

It looks as if we will have to use ADO for now. There is an 'adodbapi'
module available which is DB-API 2.0 compliant -
adodbapi.sourceforge.net. I will give it a try.

Frank

Sep 27 '06 #7

P: n/a
Frank Millman schreef:
<snip>
Not that I know of. The results of my investigations so far seem to
indicate that we have a problem :-(

Here is a link to an article dated 1998 -
https://svn.python.org/www/trunk/pyd...OdbcHints.html

Among other interesting stuff, it states -

"Notice that result values are converted to Python objects. Dates in
particular are returned as dbiDate objects. This can be a serious
limitation, because dbiDate can not represent dates prior to the UNIX
epoch (1 Jan 1970 00:00:00 GMT). If you try to retrieve earlier dates,
you'll get garbage and may even provoke a crash."

I contacted Mark Hammond, author of the win32 extensions, to ask if
there was a solution and particularly to suggest a modification to
return a datetime.datetime object. This was his reply -

"I'd be happy with an option to use the datetime module - maybe it
could even be on the cursor? However, I wont have time to do this in
the short term.
You could consider using ADO via win32com too..."

It looks as if we will have to use ADO for now. There is an 'adodbapi'
module available which is DB-API 2.0 compliant -
adodbapi.sourceforge.net. I will give it a try.

Frank
Frank,

thanks for your effort. Looks indeed like it's not going to be solved in
the short term. I will give adodbapi a whirl then.

Thanks,
Benedict
Sep 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.