A mysql database table has a column whose datatype is time ( http://dev.mysql.com/doc/refman/5.0/en/time.html ). When the table data is accessed, Python returns the value of this column as a datetime.timedelta object. How do I extract the time out of this? (I didn’t really understand what timedelta is for from the python manuals).
E.g. The column in the table contains the value ’18:00:00′ Python-MySQLdb returns this as datetime.timedelta(0, 64800)
Please ignore what is below (it does return different value) –
Added: Irrespective of the time value in the table, python-MySQLdb seems to only return datetime.timedelta(0, 64800).
Note: I use Python 2.4
It’s strange that Python returns the value as a
datetime.timedelta. It probably should return adatetime.time. Anyway, it looks like it’s returning the elapsed time since midnight (assuming the column in the table is 6:00 PM). In order to convert to adatetime.time, you can do the following::datetime.datetime.minanddatetime.time()are, of course, documented as part of the datetime module if you want more information.A
datetime.timedeltais, by the way, a representation of the difference between twodatetime.datetimevalues. So if you subtract onedatetime.datetimefrom another, you will get adatetime.timedelta. And if you add adatetime.datetimewith adatetime.timedelta, you’ll get adatetime.datetime. That’s how the code above works.