MySQLdb SEC_TO_TIME function returns datetime.timedelta class

Jorge Romero jorgeromero178 at gmail.com
Mon May 16 00:06:21 EDT 2011


On Sun, May 15, 2011 at 11:50 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Mon, May 16, 2011 at 10:42 AM, Jorge Romero <jorgeromero178 at gmail.com>
> wrote:
> > Hi Pythonists,
> > I'm retrieving some time data from a MySQL database using Python's
> MySQLdb
> > library. Here's the situation, I got a time field on MySQL given in
> seconds,
> > I need it on HH:MM:SS format, so I'm SELECTING that field with
> SEC_TO_TIME
> > function, something like this:
> > query = "SELECT SEC_TO_TIME(SUM(seconds)) FROM table"
>
> You're summing a column, so presumably the values are actually deltas
> (it doesn't make sense, for instance, to add Tues March 16th to Sat
> Nov 2nd). The result exceeds a day; in what format do you actually
> want it?
>
> For maximum flexibility, you could ditch the SEC_TO_TIME call and
> simply work with the integer seconds in Python. You can then format
> that into HHHHH:MM:SS or whatever suits you.
>
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Yeah, I believe that's the way to go, retrieve seconds and deal with them
with Python, for flexibility as you pointed. I need the seconds to become
HH:MM:SS format.

What seems weird to me is why MySQLdb treats the result of the query as
deltas. Here's what I get if a query directly the database, output from PHP
MyAdmin:

SEC_TO_TIME(SUM(billsec))
*79:30:09*
*
*
*This value suits better the datetime.time type, instead of
datetime.deltatime.
*

Thanks Chris for your feedback.

-- 
Jorge Romero
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110516/fb1df33b/attachment.html>


More information about the Python-list mailing list