MySQLdb SEC_TO_TIME function returns datetime.timedelta class

Chris Angelico rosuav at gmail.com
Sun May 15 23:50:05 EDT 2011


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



More information about the Python-list mailing list