datetime problems select from MySQL

ZoombyWoof zoombywoofremove at thishotmail.com
Thu Aug 24 04:54:21 EDT 2006


Hi. I have ran into a weird thing I just can't find any solution for. I 
have googled and searched but no luck.

The problem is that when I select TIME values from MySQL from python, I 
get wrong results when the TIME values are negative.

 From mysql program:
mysql> select id,flex from Users where id=2;
+----+----------+
| id | flex     |
+----+----------+
|  2 | 00:30:00 |
+----+----------+

 From python script:
SELECT id,flex FROM Users WHERE id=2
Data back from MySQLdb:
((2L, datetime.timedelta(0, 1800)),)

This looks good and fine, but this :

 From mysql program:
mysql> select id,flex from Users where id=2;
+----+-----------+
| id | flex      |
+----+-----------+
|  2 | -00:30:00 |
+----+-----------+

 From python :
SELECT id,flex FROM Users WHERE id=2
Data back from MySQLdb:
((2L, datetime.timedelta(0, 1800)),)

Not good, python thinks this is a positive value. One more:

mysql> select id,flex from Users where id=2;
+----+-----------+
| id | flex      |
+----+-----------+
|  2 | -01:30:00 |
+----+-----------+

Python :
SELECT id,flex FROM Users WHERE id=2
Data back from MySQLdb:
((2L, datetime.timedelta(-1, 84600)),)

At least its negative but this looks to me as -00:30 rather than 
-01:30..... (86400-84600 = 1800, = 30 minutes)

And now a negative that works!:

mysql> select id,flex from Users where id=2;
+----+-----------+
| id | flex      |
+----+-----------+
|  2 | -10:00:00 |
+----+-----------+

Python:
SELECT id,flex FROM Users WHERE id=2
Data back from MySQLdb:
((2L, datetime.timedelta(-1, 50400)),)

This looks alright to me...(86400-50400 = 36000, = 10 hours)

Any ideas ? Have I missed something obvious here or ? It seems to be 
offset by an hour or something....

I run python2.4 on debian sarge, MySQL 5.0.18 and python-mysqldb version 
1.2.1-c2-1

Any help greatly appreciated. Thanx.

/ZW





More information about the Python-list mailing list