MySQL timediff and datetime.timedelta

199 views
Skip to first unread message

Jens Grivolla

unread,
Sep 2, 2008, 6:16:53 AM9/2/08
to Django users
Hi,

I am getting weird results using django.db to do a "select
timediff(a,b)..." query from MySQL. The result is a datetime.datetime
object instead of a datetime.timedelta. When using MySQLdb directly,
it returns the expected timedelta object. The code is exactly
identical in both cases other than using django.db.connection instead
of MySQLdb.connect(...)

I'm using default packages on Ubuntu 8.04 (django 0.96.1-2ubuntu2,
mysqldb 1.2.2-5ubuntu1, mysql 5.0.51a-3ubuntu5.1).

Any clues?

Thanks,
Jens

Malcolm Tredinnick

unread,
Sep 2, 2008, 6:55:13 AM9/2/08
to django...@googlegroups.com

On Tue, 2008-09-02 at 03:16 -0700, Jens Grivolla wrote:
> Hi,
>
> I am getting weird results using django.db to do a "select
> timediff(a,b)..." query from MySQL. The result is a datetime.datetime
> object instead of a datetime.timedelta. When using MySQLdb directly,
> it returns the expected timedelta object.

Are you really getting a datetime.datetime and not a datetime.time? I
would have expected you might get the latter, since we map any TIME type
of column to a datetime.time. I can't see how you would be getting a
datetime, though.

Regards,
Malcolm


Jens Grivolla

unread,
Sep 3, 2008, 1:05:14 PM9/3/08
to Django users
On Sep 2, 12:55 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> On Tue, 2008-09-02 at 03:16 -0700, JensGrivollawrote:
> > I am getting weird results using django.db to do a "select
> > timediff(a,b)..." query from MySQL. The result is a datetime.datetime
> > object instead of a datetime.timedelta. When using MySQLdb directly,
> > it returns the expected timedelta object.
>
> Are you really getting a datetime.datetime and not a datetime.time? I
> would have expected you might get the latter, since we map any TIME type
> of column to a datetime.time. I can't see how you would be getting a
> datetime, though.

You are right, it's a datetime.time. I'd still rather have a
datetime.timedelta (which seems more logical), mostly because I want
to use the timedelta.seconds attribute. I'm now using
time_to_seconds() in MySQL so I don't have to deal with the time vs.
timedelta issues.

Ciao,
Jens
Reply all
Reply to author
Forward
0 new messages