Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How do I get time from a datetime.timedelta object?

Tags:

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

like image 588
Sam Avatar asked Apr 18 '09 20:04

Sam


People also ask

How do I get hours from Timedelta object?

We can follow the same logic to convert a timedelta to hours. Instead of dividing the total_seconds() by the number of seconds in a minute, or dividing the timedelta object by timedelta(minutes=1) , we do it for hour.

What is Timedelta in datetime Python?

A timedelta object represents a duration, the difference between two dates or times. class datetime. timedelta (days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)


2 Answers

It's strange that Python returns the value as a datetime.timedelta. It probably should return a datetime.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 a datetime.time, you can do the following::

value = datetime.timedelta(0, 64800) (datetime.datetime.min + value).time() 

datetime.datetime.min and datetime.time() are, of course, documented as part of the datetime module if you want more information.

A datetime.timedelta is, by the way, a representation of the difference between two datetime.datetime values. So if you subtract one datetime.datetime from another, you will get a datetime.timedelta. And if you add a datetime.datetime with a datetime.timedelta, you'll get a datetime.datetime. That's how the code above works.

like image 149
Rick Copeland Avatar answered Sep 22 '22 01:09

Rick Copeland


It seems to me that the TIME type in MySQL is intended to represent time intervals as datetime.timedelta does in Python. From the docs you referenced:

TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

An alternative to converting from datetime.timedelta to datetime.time would be to change the column type to DATETIME and not using the date fields.

-Insert:

tIn = datetime.datetime(     year=datetime.MINYEAR,      month=1,      day=1,      hour=10,     minute=52,     second=10     ) cursor.execute('INSERT INTO TableName (TimeColumn) VALUES (%s)', [tIn]) 

-Select:

 cursor.execute('SELECT TimeColumn FROM TableName')  result = cursor.fetchone()  if result is not None:      tOut = result[0].time()      print 'Selected time: {0}:{1}:{2}'.format(tOut.hour, tOut.minute, tOut.second) 

datetime.time() is called on a datetime object to get a time object.

like image 28
nnguyen Avatar answered Sep 21 '22 01:09

nnguyen